BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
MikeTurner
Calcite | Level 5

%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;

1 ACCEPTED SOLUTION

Accepted Solutions
Haikuo
Onyx | Level 15

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

View solution in original post

4 REPLIES 4
LinusH
Tourmaline | Level 20

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

Data never sleeps
MikeTurner
Calcite | Level 5

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.

Haikuo
Onyx | Level 15

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

MikeTurner
Calcite | Level 5

It sounds good. Thanks.

What is Bayesian Analysis?

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 4 replies
  • 1730 views
  • 0 likes
  • 3 in conversation