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?
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;
%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;
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;
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!
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.