Hello everyone,
I was wondering if there is a way to write dataset variable into a macro variable and use the macro variable within the same datastep.
I have tried different approaches to do the following; but have been running into call symput, excute so far. Thanks for any help that might help to solve the situation...
data atmp;
input anID surfix $;
datalines;
1 3_1
2 2_2
3 2_3
;
run;
data atmpdt;
input anID A_2_1 A_2_2 A_2_3 B_2_1 B_2_2 B_2_3;
datalines;
1 0.1 0.2 0.3 0.4 0.5 0.6
2 1.1 1.2 1.3 1.4 1.5 1.6
3 2.1 2.2 2.3 2.4 2.5 2.6
;
run;
%let prefixlist=A B;
data atmp2;
Merge atmp atmpdt; by anID;
/*Intention: read current row variable surfix into the macro variable asurfix*/
call symput('Asurfix',surfix);
nbvar = countw("&prefixlist"," ");
/*Intention: for each name in the macro variable "prefixlist"
create a dataset set variable initiliazed with the value in the var &aprefix._&Asurfix */
do count = 1 to nbvar;
call symput('aprefix',scan("&prefixlist",count," "));
&aprefix=&aprefix._&Asurfix;
end;
run;
Why?
Look at SYMGET and RESOLVE functions.
To tell you the truth, I wouldn't do this with macros at all, I would use PROC TRANSPOSE.
proc transpose data=atmpdt out=a;
by anID;
run;
data aa;
merge a atmp;
by anID;
run;
Now you can assign A and B to the proper variables by matching text strings in data set AA, and throw out the observations in aa that don't match;
SAS variables get created during the compilation phase of a data step. It is not possible to create additional SAS variables during execution phase.
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.