How to copy labels over to new variables after recoding using macros?

Accepted Solution Solved
Reply
Contributor
Posts: 58
Accepted Solution

How to copy labels over to new variables after recoding using macros?

[ Edited ]

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?

 

So in the example below. the label for olditem6_3_5 is "In the last 4 weeks, I ate apples."

 

Thus I want the label for item6_3_5 to be ""In the last 4 weeks, I ate apples. (recoded). 

 

My current code:

%macro recode (indata=,var=);
DATA _null_;
	SET &indata;
	array vars(*) &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 &mvNB;
data &indata;
set &indata;

array oldtest(*) old&&mvNAME&b;
array newtest(*) &&mvNAME&b;
call symput("mvLAB"||strip(i),strip(vname(old&&mvNAME&b));

	DO z=1 TO dim(oldtest);
		newtest(z)=6-oldtest(z);
	END;

label &&mvNAME&b="&mvLAB (recoded)";

RUN;
%end;
%mend recode; 

%recode (indata=temp,var=item6_3_5);

 

 


Accepted Solutions
Solution
‎11-26-2016 02:25 AM
Super User
Posts: 19,167

Re: How to copy labels over to new variables after recoding using macros?

This ONLY copies labels. Recoding requires no macros. 

 

You generally cant create and use a macro variable in the same step so your label logic is incorrect. 

 

The example here (#3) demonstrates how to recode a variable using an array. 

http://www.ats.ucla.edu/stat/sas/seminars/SAS_arrays/#new_vars

 

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. 

The SQL query creates n macro variables of the form

variableName = 'label value' 

 

In PROC datasets we loop through these macro variables to get the appropriate label statement. 

 

A label statement is of the form, where you can specify multiple variables at once. 

 

LABEL variableName = 'label value' ... ;

 

with macro variavles this becomes

 

LABEL Macrovariable1 macrovariable2 ...macrovariableN 

View solution in original post


All Replies
Super User
Posts: 19,167

Re: How to copy labels over to new variables after recoding using macros?

You're better off hitting the SASHELP.VCOLUMN to read the labels and create the new labels. 

Untested 

 

Proc SQL;

select catx('=', name, quote(label||' (recoded).')) into :new_label1-

from SASHELP.vcolumn 

where libname = 'WORK' and memname='CLASS';

quit;

%let num_vars=&sqlobs;

 

 

%macro rename;

 

proc datasets libname=work;

modify class;

label 

%do I = 1 %to &num_vars;

   %put &&new_label&i

%end;

;

run;

quit;

 

%mend;

Contributor
Posts: 58

Re: How to copy labels over to new variables after recoding using macros?

Hmm I am a bit confused on this. I'm quite new to macros. Can you clarify more on this?

 

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.

Solution
‎11-26-2016 02:25 AM
Super User
Posts: 19,167

Re: How to copy labels over to new variables after recoding using macros?

This ONLY copies labels. Recoding requires no macros. 

 

You generally cant create and use a macro variable in the same step so your label logic is incorrect. 

 

The example here (#3) demonstrates how to recode a variable using an array. 

http://www.ats.ucla.edu/stat/sas/seminars/SAS_arrays/#new_vars

 

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. 

The SQL query creates n macro variables of the form

variableName = 'label value' 

 

In PROC datasets we loop through these macro variables to get the appropriate label statement. 

 

A label statement is of the form, where you can specify multiple variables at once. 

 

LABEL variableName = 'label value' ... ;

 

with macro variavles this becomes

 

LABEL Macrovariable1 macrovariable2 ...macrovariableN 

Super User
Posts: 9,874

Re: How to copy labels over to new variables after recoding using macros?


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;
 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 484 views
  • 1 like
  • 3 in conversation