%let varlist=index1 index2 index3...........indexn;
I want to write one macro and assign index1-indexn to var_1-var-n.
I wrote one as follows. Anyone can help me correct it? Greatly appreciated.
%MACRO test(varlist=);
%let i=1;
%do %while (xxxxxxx);
%let var_%left(&i)=%scan(&varlist,i,"");
%let i=%eval(&i+1);
%end;
%mend;
Mike,
I can be aware of two problems in your code:
1. Using %do %while (&var_name ne); when 'var_name' is not available until next step. %do %while evaluates expression at the top of the loop, instead, %do %until evaluates on the bottom.
2. Misused %scan(). "" should be removed from %scan(&varlist,&i,"");
So the working code looks like:
%let varlist=yy zz aa;
%MACRO test(varlist=);
%let i=1;
%do %until (&var_name eq);
%let var_name=%scan(&varlist,&i);
%put &var_name;
%let i=%eval(&i+1);
%end;
%mend;
%test(varlist=&varlist)
As an alternative, you can also try:
%MACRO test(varlist=);
%do i=1 %to %sysfunc(countw(&varlist));
%let var_name&i=%scan(&varlist,&i);
%put &&var_name&i;
%end;
%mend;
Kindly Regards,
Haikuo
I'm not sure if I got the logic in this.
Is it a coincidence that the variable in you varlist is named using an unbroken sequence no?
If not, you coulöd much simpler do:
%let n = 12;
...
%do i = 1 to &N.;
%let var_&I. = index&I.
%end;
...
/Linus
no,no. It's not what I meant.
try to think in the way.
%let varlist=yy zz aa qq;
I wrote one macro to display yy zz aa qq respectively as follows:
%MACRO test(varlist=);
%let i=1;
%do %while (&var_name ne);
%let var_name=%scan(&varlist,&i,"");
%put &var_name;
%let i=%eval(&i+1);
%end;
%mend;
%test(varlist=&varlist)
Anything wrong with this macro? And If I want to get the number of string seperated by blank in &varlist, which function will work?
Thanks.
Mike,
I can be aware of two problems in your code:
1. Using %do %while (&var_name ne); when 'var_name' is not available until next step. %do %while evaluates expression at the top of the loop, instead, %do %until evaluates on the bottom.
2. Misused %scan(). "" should be removed from %scan(&varlist,&i,"");
So the working code looks like:
%let varlist=yy zz aa;
%MACRO test(varlist=);
%let i=1;
%do %until (&var_name eq);
%let var_name=%scan(&varlist,&i);
%put &var_name;
%let i=%eval(&i+1);
%end;
%mend;
%test(varlist=&varlist)
As an alternative, you can also try:
%MACRO test(varlist=);
%do i=1 %to %sysfunc(countw(&varlist));
%let var_name&i=%scan(&varlist,&i);
%put &&var_name&i;
%end;
%mend;
Kindly Regards,
Haikuo
It sounds good. Thanks.
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 the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.