Hello
Lets say I have a macro variable with values separated by '+'
Lets say that I want to pass the values in macro var into sas data set.
Please note that number of arguments in the macro var can be different so I am looking for dynamic solution that can do it for each number of arguments in macro var.
Here is an example for the wanted data set based on the macro var list
%let List=Health+Wealth+age+Education_in_Years+Ind_SPort_Lover;
Data Want;
input VAR_Name $20.;
cards;
Health
Wealth
age
Education_in_Years
Ind_Sport_Lover
;
Run;
Dead simple. A DO loop from 1 to countw("&list.","+"), extract single values with scan("&list.",i,"+"), and OUTPUT.
As long as the length of the macro variable is less than 32K bytes you can use normal data step.
data want;
do i=1 to countw(symget('list'),'+');
length var_name $32 ;
var_name = scan(symget('list'),i,'+');
output;
end;
run;
If the string is (or could be) longer then you might want to use a macro %DO loop instead. Which will require defining a macro.
%macro list2ds(dsname,list);
%local i ;
data &dsname;
length var_name $32 ;
%do i=1 %to %sysfunc(countw(&list,+));
var_name = "%scan(&list,&i,+)"; output;
%end;
run;
%mend list2ds;
%list2ds(dsname=want,list=&list)
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.