<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Macro function - a binary output if a value is in a list in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Macro-function-a-binary-output-if-a-value-is-in-a-list/m-p/753884#M237677</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro inm(slist,s);
    /* SAS Macro %inm to see if &amp;amp;s is contained in a string or list &amp;amp;slist                 */
    /* Borrowed from https://groups.google.com/forum/#!topic/comp.soft-sys.sas/fWcSDgg11tE */
    %if %sysfunc(indexw(&amp;amp;slist,&amp;amp;s)) gt 0 %then 1 ;
    %else 0;
%mend;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 13 Jul 2021 19:06:18 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2021-07-13T19:06:18Z</dc:date>
    <item>
      <title>Macro function - a binary output if a value is in a list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-function-a-binary-output-if-a-value-is-in-a-list/m-p/753882#M237676</link>
      <description>&lt;P&gt;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.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Pseudo code:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%Macro MyMacro(country, list = USA UK FIN GER) \minoperator; 
	%if &amp;amp;country. in (&amp;amp;list) %then %do; answer= 1; 
	%end; 
	%else %do; answer= 0; 
	%end;
	&amp;amp;answer;  
%mend; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now for instance, creating a macro variable:&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let MyBinaryVariable = %MyMacro(USA, list = (GER NOR) ); 

* Now MyBinaryVariable should be 0, since USA is not in the list provided. ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I hope it is clear what I want to achieve. Any advice on how to do this properly?&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 13 Jul 2021 19:02:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-function-a-binary-output-if-a-value-is-in-a-list/m-p/753882#M237676</guid>
      <dc:creator>SasStatistics</dc:creator>
      <dc:date>2021-07-13T19:02:52Z</dc:date>
    </item>
    <item>
      <title>Re: Macro function - a binary output if a value is in a list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-function-a-binary-output-if-a-value-is-in-a-list/m-p/753884#M237677</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro inm(slist,s);
    /* SAS Macro %inm to see if &amp;amp;s is contained in a string or list &amp;amp;slist                 */
    /* Borrowed from https://groups.google.com/forum/#!topic/comp.soft-sys.sas/fWcSDgg11tE */
    %if %sysfunc(indexw(&amp;amp;slist,&amp;amp;s)) gt 0 %then 1 ;
    %else 0;
%mend;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 13 Jul 2021 19:06:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-function-a-binary-output-if-a-value-is-in-a-list/m-p/753884#M237677</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-07-13T19:06:18Z</dc:date>
    </item>
    <item>
      <title>Re: Macro function - a binary output if a value is in a list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-function-a-binary-output-if-a-value-is-in-a-list/m-p/753888#M237680</link>
      <description>&lt;P&gt;Close.&amp;nbsp; You need to use %LET to assign values to you new ANSWER macro variable.&amp;nbsp; You should also define it a %LOCAL so calling your macro does not accidently modify some already existing macro variable with that name.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Plus you have the option switch character wrong.&amp;nbsp; You should tell it what delimiter you want to use. Do not include a semi-colon in the text the macro emits.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro MyMacro(country, list = USA UK FIN GER) /minoperator mindelimiter=' ';
%local answer;
%if &amp;amp;country. in &amp;amp;list %then %do; 
  %let answer= 1; 
%end; 
%else %do;
  %let answer= 0; 
%end;
&amp;amp;answer  
%mend; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Examples:&lt;/P&gt;
&lt;PRE&gt;819   %put %mymacro(UK);
1
820   %put %mymacro(uk);
0

&lt;/PRE&gt;
&lt;P&gt;Of course the macro body could be a lot simpler.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro MyMacro(country, list = USA UK FIN GER) /minoperator mindelimiter=' ';
%local answer;
%let answer=%eval(&amp;amp;country. in &amp;amp;list);
&amp;amp;answer.
%mend; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 13 Jul 2021 19:14:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-function-a-binary-output-if-a-value-is-in-a-list/m-p/753888#M237680</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-07-13T19:14:07Z</dc:date>
    </item>
  </channel>
</rss>

