Code below is working fine if the macro variable 'number' has only one value. I would like to know how to tweak the code, if the macro variable 'number' has value like, 1,2,3. and 'name' has value like filename1.xls, filename2.xls and filename3.xls
%let name=filename1.xls ; %let number=1 ; %if %index(&name,&number) %then %do; %put &number is contained in &name.. ; %end;
Assume the macro variable 'number' resolves to 1,2,3 and the macro variable 'name' resolves to filename1.xls, filename2.xls and filename3.xls
Now how to tweak the index function in the above code such that %if clause runs as many as the number of times based on the value of macro variable 'name' and 'number'.
I think it is not the best idea, but if you have to try this:
%let name=filename1.xls, filename2.xls, filename3.xls ;
%let number=1,2,3 ;
%macro test();
%local wc i nm nr;
%let wc = %qsysfunc(countw((&name.),%str((, ))));
%put &=wc.;
%do i = 1 %to &wc.;
%let nm = %scan((&name.),&i.,%str((, )));
%let nr = %scan((&number.),&i.,%str((, )));
%if %index(&nm.,&nr.) %then %do;
%put &nr. is contained in &nm.. ;
%end;
%end;
%mend test;
%test()
Bart
Try like this:
%let name=filename1.xls, filename2.xls, filename3.xls ;
%let number=1,2,3 ;
%macro test(names,numbers);
%local wc i name number;
%let wc = %qsysfunc(countw(&names.,%str((, ))));
/*%put &=wc.;*/
%do i = 1 %to &wc.;
%let name = %scan(&names.,&i.,%str((, )));
%let number = %scan(&numbers.,&i.,%str((, )));
%if %index(&name,&number) %then %do;
%put &number is contained in &name.. ;
%end;
%end;
%mend test;
%test((&name.),(&number.))
additional brackets in macro call are just to mask commas as separators. I recommend you to replace commas with spaces.
All the best
Bart
Thank you. Can we implement this with only macros but without macro parameters?
I think it is not the best idea, but if you have to try this:
%let name=filename1.xls, filename2.xls, filename3.xls ;
%let number=1,2,3 ;
%macro test();
%local wc i nm nr;
%let wc = %qsysfunc(countw((&name.),%str((, ))));
%put &=wc.;
%do i = 1 %to &wc.;
%let nm = %scan((&name.),&i.,%str((, )));
%let nr = %scan((&number.),&i.,%str((, )));
%if %index(&nm.,&nr.) %then %do;
%put &nr. is contained in &nm.. ;
%end;
%end;
%mend test;
%test()
Bart
thanks again. May I know why it's the best idea to use macro parameters?
Simply because: 1) you know what is the input, 2) you know that when you run the macro you will get the input, 3) there are no "hidden" parameters so easier to debug, etc.
but you know - "who is without the sin let the first throw the stone" 😉
B-)
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.
Ready to level-up your skills? Choose your own adventure.