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.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.