Hi SAS users,
I'm working on a macro where I'd like to 1) re-code values and 2)convert those values from character to numeric. As I have over 20 variables that require re-coding and convert, I'm hoping to use a macro to make it as efficient as possible. Thus far, I've only been able to figure out how to do one variable at a time within the data step. Any suggestions?
Thanks!
data have;
input StudyID $1-2 CaseID $4-6 m2blue $8-12 q1green $14-18 k5orange $20-24;
datalines;
AA AA1 0-No 0-No 1-Yes
BB BB1 0-No 0-No 0-No
CC CC1 1-Yes 0-No 1-Yes
DD DD1 0-No 1-Yes 1-Yes
EE EE1 1-Yes 1-Yes 1-Yes
FF FF1 0-No 0-No 0-No
GG GG1 0-No 0-No 0-No
HH HH1 0-No 1-Yes 0-No
II II1 1-Yes 0-No 0-No
JJ JJ1 1-Yes 0-No 1-Yes
;;
run;
data bugn;
input vd $ 1-40;
datalines;
m2blue
q1green
k5orange
;
run;
data _null_;
set bugn end=last;
call symput('vd'||left(put(_n_,3.)),vd);
if last then call symput('bugn',_n_);
run;
options mprint;
%macro loop;
%do i=1 %to &bugn;
%let ti=%cmpres(&bug&i);
data two;
vd="&&vd&i";
run;
%let vd=&&vd&i;
data want;
set have (rename=(&vd=&vd._old));
format &vd 1.;
%do i=1 %to &bugn;
if substr(&vd._old,1,1) = '0' then &vd=0;
else if substr(&vd._old,1,1) = '1' then &vd=1;
retain &vd;
keep &vd;
%end;
run;
%end;
%mend loop;
%loop;
With a text file you can specify a custom format on reading the data.
I am guessing that you want 1 for Yes and 0 for No in your final form.
Proc format;
invalue MyYesNo
'1-Yes' = 1
'0-No' = 0
;
run;
data want;
infile "your file name to read" <options to read>; /* if you used proc import you can look at the resulting code in the log to get this */
informat m2blue q1green k5orange MyYesNo. ;
input StudyID $1-2 CaseID $4-6 m2blue 8-12 q1green 14-18 k5orange 20-24; /* what ever the input statement needs*/
run;
Are you originally reading from a text file or a SAS data set?
The data is originally from a text file. What would be the difference if it was from a SAS data set?
Fix it in the original import step rather than try and do it afterwards. i.e. fix it before it's broken.
Can you post a small sample of the text file or is the data step above representative? If so, what would you want the output to be.
With a text file you can specify a custom format on reading the data.
I am guessing that you want 1 for Yes and 0 for No in your final form.
Proc format;
invalue MyYesNo
'1-Yes' = 1
'0-No' = 0
;
run;
data want;
infile "your file name to read" <options to read>; /* if you used proc import you can look at the resulting code in the log to get this */
informat m2blue q1green k5orange MyYesNo. ;
input StudyID $1-2 CaseID $4-6 m2blue 8-12 q1green 14-18 k5orange 20-24; /* what ever the input statement needs*/
run;
Thanks! This worked great and was a lot easier than a macro.
Please mark the question as answered so people know we don't need to check whether this still needs an answer.
You could just change your INPUT statement and avoid writing macros entirely:
length dummy $ 1;
drop dummy;
input StudyID $1-2 CaseID $4-6 m2blue : 1. dummy q1green : 1. dummy k5orange : 1.;
Hi Astounding,
what does it means the ':' carachter in the input statement, before the number?
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.