Dipesh Gupta
Dipesh Gupta

Reputation: 21

Arrays in Macros in SAS

%macro test ;
%array factors(7) &ordered_col ;
%array names(7) &ordered_col2 ;
%do i = 1 %to 7 ;
%let factorname = factors(&i) ;
%let output = names(&i) ;
proc sort data=actshare.policy out=&output nodupkey ;
by &factorname ;
quit ;
%end ;

%mend ;

%test

where ordered_col and ordered_col2 are my respective arrays. how can I run this program. please help with a suitable suggestion.

Upvotes: 1

Views: 7809

Answers (1)

Tom
Tom

Reputation: 51566

There is no such thing as a macro array or a %ARRAY statement. If you have a list of values in a macro variable then use the %SCAN() function to select items from the list.

%macro test(varlist,dslist);
%local factorname output i ;
%do i = 1 %to %sysfunc(countw(&varlist)) ;
  %let factorname = %scan(&varlist,&i) ;
  %let output = %scan(dslist,&i) ;
proc sort data=actshare.policy out=&output nodupkey ;
  by &factorname ;
run ;
%end ;
%mend test;

%test(var1 var2 var3,ds1 ds2 ds3)

If instead you have created a series of macro variables with same base name and a numeric suffix you will need to know number of them to make your %DO loop. So if you had done:

%let varname1=var1;
%let varname2=var2;
%let dsname1=ds1;
%let dsname2=ds2;
%let n=2;

Then your loop might look like this instead:

%do i=1 %to &N;
  %let factorname = &&varname&i ;
  %let output = &&dsname&i ;
...
%end;

Upvotes: 4

Related Questions