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!

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 625 views
  • 0 likes
  • 3 in conversation