BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
David_Billa
Rhodochrosite | Level 12

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'.

1 ACCEPTED SOLUTION

Accepted Solutions
yabwon
Onyx | Level 15

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

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



View solution in original post

5 REPLIES 5
yabwon
Onyx | Level 15

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

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



David_Billa
Rhodochrosite | Level 12

Thank you. Can we implement this with only macros but without macro parameters?

yabwon
Onyx | Level 15

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

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



David_Billa
Rhodochrosite | Level 12

thanks again. May I know why it's the best idea to use macro parameters?

yabwon
Onyx | Level 15

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-)

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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
  • 5 replies
  • 1233 views
  • 2 likes
  • 2 in conversation