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; 

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 461 views
  • 3 likes
  • 3 in conversation