DATA Step, Macro, Functions and more

SAS MACROS LOOP

Reply
Frequent Contributor
Posts: 125

SAS MACROS LOOP

I have folders  as F_001 , F_002, F_003, F_004, F_005, F_011

and each folder has Data_1 , Data_2, Data_3, Data_4, Data_5, Data_10

if i give prompt x  as  1,3,4 it should count the number of arguments in the macro x now macro x has 3 arguments so it should loop

three times .

 

PROMPT FORMAT X= 1,3,4

%macro test(type);

 

%let type = c;
/* first  find whether the delimiter is comma seperated

%if index(&x,',') > 0 %then %do;

 

%let n = %sysfunc(countw(&x,',');

%do k=1 %to &n ; / * will loop three times one for x=1 and then x=3 and then x=4 */

%let start = %substr(&x,&k,1);
%let end = %substr(&x,&k,1);


%do pt = &start %to &end; / * In this case  start will be 1 and end will be 1 as its looping for first time  */
%let folder_name = F_00&pt;
%if &pt ge 10 %then %do;
  %let folder_name = F_0&pt; /* (here folder_name resolves to F001) */
  %end;
 %test2(&folder_name,&type);

%end;

%end;

 

Please Suggest me correct syntax as above code is not working but concept is same

Super User
Posts: 23,776

Re: SAS MACROS LOOP

Posted in reply to chennupriya

This seems really close to these questions: 

 

https://communities.sas.com/t5/Base-SAS-Programming/SAS-Scan/m-p/405858/highlight/false#M98754

 

I would also suggest looking at the Macro Appendix which now has a list of examples that may help you out. 

Super User
Super User
Posts: 9,599

Re: SAS MACROS LOOP

Posted in reply to chennupriya

To what end, what is it you want out at the end.  The reason being is that Base SAS is a really powerful language for data manipulation and can do this type of work far better than macro which only generates Base SAS code.  You you take this prompt and want to run a macro on all datasets within the given libraries.  You could go your way, or you could just say:

data _null_;
  do i=1 to countw("&x.",",");
    call execute(cats('%domacro (l=f_',put(input(scan("&x.",i,","),best.),z3.),');'));
  end;
run;

This will loop over each comma delimited value in &x, and for each one call the macro domacro, with the parameter f_ and the value from the list.  Far simpler.  Macro is not a replacement for Base SAS!

Ask a Question
Discussion stats
  • 2 replies
  • 132 views
  • 0 likes
  • 3 in conversation