<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How to copy labels over to new variables after recoding using macros? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-copy-labels-over-to-new-variables-after-recoding-using/m-p/314432#M270823</link>
    <description>&lt;P&gt;This ONLY copies labels. Recoding requires no macros.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You generally cant create and use a macro variable in the same step so your label logic is incorrect.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The example here (#3) demonstrates how to recode a variable using an array.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="http://www.ats.ucla.edu/stat/sas/seminars/SAS_arrays/#new_vars" target="_blank"&gt;http://www.ats.ucla.edu/stat/sas/seminars/SAS_arrays/#new_vars&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The logic to create the label statement is to query the Sashelp.vcolumn dataset. If you're no familiar with it, I would suggest opening it up and perusing it.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The SQL query creates n macro variables of the form&lt;/P&gt;
&lt;P&gt;variableName = 'label value'&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In PROC datasets we loop through these macro variables to get the appropriate label statement.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A label statement is of the form, where you can specify multiple variables at once.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;LABEL variableName = 'label value' ... ;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;with macro variavles this becomes&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;LABEL Macrovariable1 macrovariable2 ...macrovariableN&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sat, 26 Nov 2016 06:46:14 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2016-11-26T06:46:14Z</dc:date>
    <item>
      <title>How to copy labels over to new variables after recoding using macros?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-copy-labels-over-to-new-variables-after-recoding-using/m-p/314426#M270819</link>
      <description>&lt;P&gt;This is my code. I am recoding a bunch of variables (200 to be exact). I am using this code, because I was hoping to use the vlabel function, but sadly vlabel doesn't support labels with spaces and "-" in them.. it only supports numbers,digits and underscores. I am trying to figure out the easiest way to add the labels to my recoded variables. Can someone suggest something?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So in the example below. the label for olditem6_3_5 is "In the last 4 weeks, I ate apples."&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thus I &lt;STRONG&gt;want&lt;/STRONG&gt; the label for&amp;nbsp;item6_3_5 to be "&lt;SPAN&gt;"In the last 4 weeks, I ate apples. (recoded).&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My current code:&lt;/P&gt;
&lt;PRE&gt;%macro recode (indata=,var=);
DATA _null_;
	SET &amp;amp;indata;
	array vars(*) &amp;amp;var;
	DO i=1 TO dim(vars);
		call symput("mvNAME"||strip(i),strip(vname(vars(i))));
		
	END;
	call symput("mvNB",strip(dim(vars)));
RUN;

%DO b=1 %TO &amp;amp;mvNB;
data &amp;amp;indata;
set &amp;amp;indata;

array oldtest(*) old&amp;amp;&amp;amp;mvNAME&amp;amp;b;
array newtest(*) &amp;amp;&amp;amp;mvNAME&amp;amp;b;
call symput("mvLAB"||strip(i),strip(vname(old&amp;amp;&amp;amp;mvNAME&amp;amp;b));

	DO z=1 TO dim(oldtest);
		newtest(z)=6-oldtest(z);
	END;

label &amp;amp;&amp;amp;mvNAME&amp;amp;b="&amp;amp;mvLAB (recoded)";

RUN;
%end;
%mend recode; 

%recode (indata=temp,var=item6_3_5);&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 26 Nov 2016 06:07:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-copy-labels-over-to-new-variables-after-recoding-using/m-p/314426#M270819</guid>
      <dc:creator>Tpham</dc:creator>
      <dc:date>2016-11-26T06:07:22Z</dc:date>
    </item>
    <item>
      <title>Re: How to copy labels over to new variables after recoding using macros?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-copy-labels-over-to-new-variables-after-recoding-using/m-p/314429#M270820</link>
      <description>&lt;P&gt;You're better off hitting the SASHELP.VCOLUMN to read the labels and create the new labels.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Untested&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Proc SQL;&lt;/P&gt;
&lt;P&gt;select catx('=', name, quote(label||' (recoded).')) into :new_label1-&lt;/P&gt;
&lt;P&gt;from SASHELP.vcolumn&amp;nbsp;&lt;/P&gt;
&lt;P&gt;where libname = 'WORK' and memname='CLASS';&lt;/P&gt;
&lt;P&gt;quit;&lt;/P&gt;
&lt;P&gt;%let num_vars=&amp;amp;sqlobs;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%macro rename;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc datasets libname=work;&lt;/P&gt;
&lt;P&gt;modify class;&lt;/P&gt;
&lt;P&gt;label&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%do I = 1 %to &amp;amp;num_vars;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;%put &amp;amp;&amp;amp;new_label&amp;amp;i&lt;/P&gt;
&lt;P&gt;%end;&lt;/P&gt;
&lt;P&gt;;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;quit;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%mend;&lt;/P&gt;</description>
      <pubDate>Sat, 26 Nov 2016 06:33:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-copy-labels-over-to-new-variables-after-recoding-using/m-p/314429#M270820</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-11-26T06:33:45Z</dc:date>
    </item>
    <item>
      <title>Re: How to copy labels over to new variables after recoding using macros?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-copy-labels-over-to-new-variables-after-recoding-using/m-p/314430#M270821</link>
      <description>&lt;P&gt;Hmm I am a bit confused on this. I'm quite new to macros. Can you clarify more on this?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am unsure how can I create a new variable via recoding an old variable and copying the label from the old variable over to the new one.&lt;/P&gt;</description>
      <pubDate>Sat, 26 Nov 2016 06:37:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-copy-labels-over-to-new-variables-after-recoding-using/m-p/314430#M270821</guid>
      <dc:creator>Tpham</dc:creator>
      <dc:date>2016-11-26T06:37:44Z</dc:date>
    </item>
    <item>
      <title>Re: How to copy labels over to new variables after recoding using macros?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-copy-labels-over-to-new-variables-after-recoding-using/m-p/314431#M270822</link>
      <description>&lt;PRE&gt;

data have;
 set sashelp.class(keep=weight height);
 label weight='xxxxxx' height='yyyyyy';
run;
data new;
 set have;
 new_weight=weight+10;
 new_height=height+10;
run;
proc transpose data=have(obs=0) out=temp;
var weight height;
run;

data _NULL_;
 set temp end=last;
 if _n_=1 then call execute('proc datasets library=work nolist nodetails; modify new;label');
 call execute(cats('new_',_name_,'="',_label_,'"'));
 if last then call execute(';quit;');
run;
 

&lt;/PRE&gt;</description>
      <pubDate>Sat, 26 Nov 2016 06:44:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-copy-labels-over-to-new-variables-after-recoding-using/m-p/314431#M270822</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-11-26T06:44:17Z</dc:date>
    </item>
    <item>
      <title>Re: How to copy labels over to new variables after recoding using macros?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-copy-labels-over-to-new-variables-after-recoding-using/m-p/314432#M270823</link>
      <description>&lt;P&gt;This ONLY copies labels. Recoding requires no macros.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You generally cant create and use a macro variable in the same step so your label logic is incorrect.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The example here (#3) demonstrates how to recode a variable using an array.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="http://www.ats.ucla.edu/stat/sas/seminars/SAS_arrays/#new_vars" target="_blank"&gt;http://www.ats.ucla.edu/stat/sas/seminars/SAS_arrays/#new_vars&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The logic to create the label statement is to query the Sashelp.vcolumn dataset. If you're no familiar with it, I would suggest opening it up and perusing it.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The SQL query creates n macro variables of the form&lt;/P&gt;
&lt;P&gt;variableName = 'label value'&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In PROC datasets we loop through these macro variables to get the appropriate label statement.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A label statement is of the form, where you can specify multiple variables at once.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;LABEL variableName = 'label value' ... ;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;with macro variavles this becomes&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;LABEL Macrovariable1 macrovariable2 ...macrovariableN&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 26 Nov 2016 06:46:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-copy-labels-over-to-new-variables-after-recoding-using/m-p/314432#M270823</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-11-26T06:46:14Z</dc:date>
    </item>
  </channel>
</rss>

