<?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: Execute part of macro based on whether macro value is in a list in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Execute-part-of-macro-based-on-whether-macro-value-is-in-a-list/m-p/953704#M372557</link>
    <description>&lt;P&gt;If you want the macro processor to treat IN as an operator and not just some random two byte string you need to set MINOPERATOR system option (and the MINDELIMITER option also).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you are doing it inside a macro then use the MINOPERATOR and MINDELIMITER options of the %MACRO statement so that your macro does not change behavior when users change the system options.&amp;nbsp; The parentheses are not needed, but usually do not cause any trouble.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro do_thing(var) / minoperator mindelimiter=',' ;
    &amp;lt;insert code doing things based on &amp;amp;var here&amp;gt;
    %if &amp;amp;var in &amp;amp;list_var %then %do;
         &amp;lt;insert &amp;amp;list_var-specific code here&amp;gt;
    %end;
%mend do_thing;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You could also just use the INDEXW() or FINDW() function to test if the value appears in the string.&amp;nbsp; Use %SYSFUNC() to use them in macro logic.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You coding will be much easier if the values are delimited by something other than comma in the list.&amp;nbsp; That way you could more easily pass the list of values into the macro as a parameter If you have commas in the value you will need to use macro quoting to pass the value as an argument.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro doit(value,list) / minoperator mindelimiter=' ';
%if &amp;amp;value in &amp;amp;list %then %put &amp;amp;=value IS in &amp;amp;=list;
%else %put &amp;amp;=value IS NOT in &amp;amp;=list;
%mend doit;

%doit(value=1,list=1 2 5 7 9);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Remember that in the SAS language you can use spaces instead of commas when using the IN operator.&amp;nbsp; So code like this in normal SAS code works:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if a in (1 3 5 7) then do;

end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 16 Dec 2024 20:01:42 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2024-12-16T20:01:42Z</dc:date>
    <item>
      <title>Execute part of macro based on whether macro value is in a list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Execute-part-of-macro-based-on-whether-macro-value-is-in-a-list/m-p/953699#M372555</link>
      <description>&lt;P&gt;Apologies if there's an obvious answer to this, but I haven't been able to find it.&lt;/P&gt;
&lt;P&gt;Let's say I have a macro variable:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro do_thing(var);
    &amp;lt;insert code doing things based on &amp;amp;var here&amp;gt;
%mend do_thing;

%do_thing(1);
%do_thing(2);
%do_thing(3);
....
%do_thing(9);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And I also have a list of values stored in a macro variable:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let list_var = 1,2,5,7,9;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If I wanted part of the code in %do_thing to execute only if the value of &amp;amp;var was one of the values in &amp;amp;list_var, how would I do that? My naive expectation would be something like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro do_thing(var);
    &amp;lt;insert code doing things based on &amp;amp;var here&amp;gt;
    %if &amp;amp;var %in (&amp;amp;list_var) %then %do;
         &amp;lt;insert &amp;amp;list_var-specific code here&amp;gt;
    %end;
%mend do_thing;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But %in doesn't seem to exist.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 16 Dec 2024 17:39:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Execute-part-of-macro-based-on-whether-macro-value-is-in-a-list/m-p/953699#M372555</guid>
      <dc:creator>scify</dc:creator>
      <dc:date>2024-12-16T17:39:57Z</dc:date>
    </item>
    <item>
      <title>Re: Execute part of macro based on whether macro value is in a list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Execute-part-of-macro-based-on-whether-macro-value-is-in-a-list/m-p/953704#M372557</link>
      <description>&lt;P&gt;If you want the macro processor to treat IN as an operator and not just some random two byte string you need to set MINOPERATOR system option (and the MINDELIMITER option also).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you are doing it inside a macro then use the MINOPERATOR and MINDELIMITER options of the %MACRO statement so that your macro does not change behavior when users change the system options.&amp;nbsp; The parentheses are not needed, but usually do not cause any trouble.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro do_thing(var) / minoperator mindelimiter=',' ;
    &amp;lt;insert code doing things based on &amp;amp;var here&amp;gt;
    %if &amp;amp;var in &amp;amp;list_var %then %do;
         &amp;lt;insert &amp;amp;list_var-specific code here&amp;gt;
    %end;
%mend do_thing;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You could also just use the INDEXW() or FINDW() function to test if the value appears in the string.&amp;nbsp; Use %SYSFUNC() to use them in macro logic.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You coding will be much easier if the values are delimited by something other than comma in the list.&amp;nbsp; That way you could more easily pass the list of values into the macro as a parameter If you have commas in the value you will need to use macro quoting to pass the value as an argument.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro doit(value,list) / minoperator mindelimiter=' ';
%if &amp;amp;value in &amp;amp;list %then %put &amp;amp;=value IS in &amp;amp;=list;
%else %put &amp;amp;=value IS NOT in &amp;amp;=list;
%mend doit;

%doit(value=1,list=1 2 5 7 9);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Remember that in the SAS language you can use spaces instead of commas when using the IN operator.&amp;nbsp; So code like this in normal SAS code works:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if a in (1 3 5 7) then do;

end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 16 Dec 2024 20:01:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Execute-part-of-macro-based-on-whether-macro-value-is-in-a-list/m-p/953704#M372557</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-12-16T20:01:42Z</dc:date>
    </item>
    <item>
      <title>Re: Execute part of macro based on whether macro value is in a list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Execute-part-of-macro-based-on-whether-macro-value-is-in-a-list/m-p/953712#M372558</link>
      <description>&lt;P&gt;%inm does exist (In-Macro) as a macro you can include in your code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&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;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example of use:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let list=1 2 3 5 7;
%let target = 22;
%let abc=%inm(&amp;amp;list,&amp;amp;target);
%put &amp;amp;=abc;

%let target = 5;
%let abc=%inm(&amp;amp;list,&amp;amp;target);
%put &amp;amp;=abc;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 16 Dec 2024 18:30:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Execute-part-of-macro-based-on-whether-macro-value-is-in-a-list/m-p/953712#M372558</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-12-16T18:30:38Z</dc:date>
    </item>
  </channel>
</rss>

