BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Tpham
Quartz | Level 8

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);

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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

4 REPLIES 4
Reeza
Super User

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;

Tpham
Quartz | Level 8

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.

Reeza
Super User

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 

Ksharp
Super User

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;
 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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