BookmarkSubscribeRSS Feed
chennupriya
Quartz | Level 8

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

2 REPLIES 2
Reeza
Super User

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. 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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!

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 2 replies
  • 1001 views
  • 0 likes
  • 3 in conversation