- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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?
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
%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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;