Help using Base SAS procedures

how to program like this?

Accepted Solution Solved
Reply
Contributor
Posts: 49
Accepted Solution

how to program like this?

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


Accepted Solutions
Solution
‎02-02-2012 07:37 AM
Respected Advisor
Posts: 3,124

Re: how to program like this?

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


All Replies
Super User
Posts: 5,260

how to program like this?

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
Contributor
Posts: 49

how to program like this?

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.

Solution
‎02-02-2012 07:37 AM
Respected Advisor
Posts: 3,124

Re: how to program like this?

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

Contributor
Posts: 49

Re: how to program like this?

It sounds good. Thanks.

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

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