DATA Step, Macro, Functions and more

Iterating sas do to loop for specific variables

Reply
Occasional Contributor
Posts: 8

Iterating sas do to loop for specific variables

Hi,

 

so below is the structure of my program (pesudo code)

 

list = 

[1,2,5],

[2,4,6];

 

%macro someMacro (list)

 

%do list=1 to list=<lastIndex>

when list = 1 

access 1,2,5 in loop

when list = 2

access 2,4,6 in loop

 

%mend someMacro()

 

someMacro(list)


how do I impliment a sas macro with passing a list or array as a parameter to the macro?

My problem is similar to this topic.

http://support.sas.com/kb/26/155.html

Only difference is that I need to pass more than 1 variable in one iteration.

Super User
Posts: 5,503

Re: Iterating sas do to loop for specific variables

Posted in reply to captainprice0

It would be less messy if you could change the format of the incoming list.  Are you able to get the equivalent of:

 

%let list = 1 2 5 # 2 4 6;

 

The choice of delimiter isn't important, but removing the brackets and commas is helpful.

Super User
Super User
Posts: 7,042

Re: Iterating sas do to loop for specific variables

Posted in reply to captainprice0

Don't use comma as the delimiter. In that sense the advice from SAS in that posting should be first to use spaces or some other character besides comma as your delmiter. This will eliminate the need for a lot of macro quoting that makes the code hard to read and also end up confusing the SAS parser.

 

To generate your type of multi-dimensional list just need to use more than one type of delimiter. Typically I would use space first and add | as the next delimiter.

 

%macro someMacro (list);
%local i sentence j word;
%do i = 1 %to %sysfunc(countw(&list,|));
  %let sentence = %scan(&list,&i,|);
  %do j = 1 %to %sysfunc(countw(&sentence));
    %let word = %scan(&sentence,&j);
    %put (&i,&j) = &word;
  %end;
%end;
%mend someMacro;

%someMacro(1 2 3|4 5 6);
Ask a Question
Discussion stats
  • 2 replies
  • 167 views
  • 0 likes
  • 3 in conversation