BookmarkSubscribeRSS Feed
Ronein
Onyx | Level 15

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;

 

 

2 REPLIES 2
Tom
Super User Tom
Super User

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)

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 256 views
  • 2 likes
  • 3 in conversation