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

I would like to check wether a variable is in a list of values. If so, I want the macro to return the value 1, else return the value 0. 

Pseudo code: 

 

 

%Macro MyMacro(country, list = USA UK FIN GER) \minoperator; 
	%if &country. in (&list) %then %do; answer= 1; 
	%end; 
	%else %do; answer= 0; 
	%end;
	&answer;  
%mend; 

Now for instance, creating a macro variable: 

%let MyBinaryVariable = %MyMacro(USA, list = (GER NOR) ); 

* Now MyBinaryVariable should be 0, since USA is not in the list provided. ;

I hope it is clear what I want to achieve. Any advice on how to do this properly? 


 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Close.  You need to use %LET to assign values to you new ANSWER macro variable.  You should also define it a %LOCAL so calling your macro does not accidently modify some already existing macro variable with that name.

 

Plus you have the option switch character wrong.  You should tell it what delimiter you want to use. Do not include a semi-colon in the text the macro emits.

 

%macro MyMacro(country, list = USA UK FIN GER) /minoperator mindelimiter=' ';
%local answer;
%if &country. in &list %then %do; 
  %let answer= 1; 
%end; 
%else %do;
  %let answer= 0; 
%end;
&answer  
%mend; 

Examples:

819   %put %mymacro(UK);
1
820   %put %mymacro(uk);
0

Of course the macro body could be a lot simpler.

%macro MyMacro(country, list = USA UK FIN GER) /minoperator mindelimiter=' ';
%local answer;
%let answer=%eval(&country. in &list);
&answer.
%mend; 

 

View solution in original post

2 REPLIES 2
PaigeMiller
Diamond | Level 26
%macro inm(slist,s);
    /* SAS Macro %inm to see if &s is contained in a string or list &slist                 */
    /* Borrowed from https://groups.google.com/forum/#!topic/comp.soft-sys.sas/fWcSDgg11tE */
    %if %sysfunc(indexw(&slist,&s)) gt 0 %then 1 ;
    %else 0;
%mend;
--
Paige Miller
Tom
Super User Tom
Super User

Close.  You need to use %LET to assign values to you new ANSWER macro variable.  You should also define it a %LOCAL so calling your macro does not accidently modify some already existing macro variable with that name.

 

Plus you have the option switch character wrong.  You should tell it what delimiter you want to use. Do not include a semi-colon in the text the macro emits.

 

%macro MyMacro(country, list = USA UK FIN GER) /minoperator mindelimiter=' ';
%local answer;
%if &country. in &list %then %do; 
  %let answer= 1; 
%end; 
%else %do;
  %let answer= 0; 
%end;
&answer  
%mend; 

Examples:

819   %put %mymacro(UK);
1
820   %put %mymacro(uk);
0

Of course the macro body could be a lot simpler.

%macro MyMacro(country, list = USA UK FIN GER) /minoperator mindelimiter=' ';
%local answer;
%let answer=%eval(&country. in &list);
&answer.
%mend; 

 

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 595 views
  • 3 likes
  • 3 in conversation