%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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.