<?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: Need help with %do in a macro in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Need-help-with-do-in-a-macro/m-p/986992#M380011</link>
    <description>&lt;P&gt;Thank you Tom!&amp;nbsp; You are absolutely right, I just need to check the value of the variables.&amp;nbsp; I thought cycling through them via a macro would be the best way but I see I was wrong.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;One question - doesn't WHICHC look for the string (kind of like an index)?&amp;nbsp; If so, it would consider values of 'Not Related' as a match, would it not, since it contains the string 'Related'?&lt;/P&gt;</description>
    <pubDate>Wed, 29 Apr 2026 18:44:31 GMT</pubDate>
    <dc:creator>cbig</dc:creator>
    <dc:date>2026-04-29T18:44:31Z</dc:date>
    <item>
      <title>Need help with %do in a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-help-with-do-in-a-macro/m-p/986989#M380009</link>
      <description>&lt;P&gt;Hello community,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;There is something not working right in this macro and I can't figure out where my issue is.&amp;nbsp; I have 11 variables (var, var2, var3, etc.&amp;nbsp; The first var doesn't have a '1') that I need to look through.&amp;nbsp; If any of the variables&amp;nbsp; equal the value of "Related" then flag should be set to "keep" for the observation.&amp;nbsp; The flag variable is retained, so I set it to missing at the beginning of the processing of an observation.&amp;nbsp; The result I am getting is that the flag = "keep" even for some rows where none of the variables equal "Related".&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would love a 2nd pair of eyes on this if someone has time.&amp;nbsp; Thanks!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV&gt;&lt;SPAN&gt;%macro check(varlist=,value=);&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;flag = "";&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;if var= "Related" then flag = "keep";&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;%do i = 2 %to 11;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;%if %cmpres(%scan(&amp;amp;varlist,&amp;amp;i)) = "&amp;amp;value" %then flag = "keep";&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;%end;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;%mend check;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;%check(varlist=var2 var3 var4 var5 var6 var7 var8 var9 var10 var11,value=Related);&lt;/SPAN&gt;&lt;/DIV&gt;</description>
      <pubDate>Wed, 29 Apr 2026 18:08:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-help-with-do-in-a-macro/m-p/986989#M380009</guid>
      <dc:creator>cbig</dc:creator>
      <dc:date>2026-04-29T18:08:25Z</dc:date>
    </item>
    <item>
      <title>Re: Need help with %do in a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-help-with-do-in-a-macro/m-p/986991#M380010</link>
      <description>&lt;P&gt;You probably do not need to generate any SAS code at all, so you probably don't need a macro at all.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It appears that FLAG should be set to keep when Related appears in any of those variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So you probably want something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  if whichc("Related",of var var2-var10) then FLAG="keep";
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you do need to make a macro to help generate that code then perhaps you want something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro mymac(in,out,value,varlist);
data &amp;amp;out;
  set &amp;amp;in;
  if whichc("&amp;amp;value",of &amp;amp;varlist) then FLAG="keep";
run;
%mend mymac;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Which you can use to generate the above data step by calling it like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%mymac(in=have,out=want,value=Related,varlist=var var2-var10);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 29 Apr 2026 18:38:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-help-with-do-in-a-macro/m-p/986991#M380010</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2026-04-29T18:38:40Z</dc:date>
    </item>
    <item>
      <title>Re: Need help with %do in a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-help-with-do-in-a-macro/m-p/986992#M380011</link>
      <description>&lt;P&gt;Thank you Tom!&amp;nbsp; You are absolutely right, I just need to check the value of the variables.&amp;nbsp; I thought cycling through them via a macro would be the best way but I see I was wrong.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;One question - doesn't WHICHC look for the string (kind of like an index)?&amp;nbsp; If so, it would consider values of 'Not Related' as a match, would it not, since it contains the string 'Related'?&lt;/P&gt;</description>
      <pubDate>Wed, 29 Apr 2026 18:44:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-help-with-do-in-a-macro/m-p/986992#M380011</guid>
      <dc:creator>cbig</dc:creator>
      <dc:date>2026-04-29T18:44:31Z</dc:date>
    </item>
    <item>
      <title>Re: Need help with %do in a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-help-with-do-in-a-macro/m-p/986993#M380012</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/481405"&gt;@cbig&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thank you Tom!&amp;nbsp; You are absolutely right, I just need to check the value of the variables.&amp;nbsp; I thought cycling through them via a macro would be the best way but I see I was wrong.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One question - doesn't WHICHC look for the string (kind of like an index)?&amp;nbsp; If so, it would consider values of 'Not Related' as a match, would it not, since it contains the string 'Related'?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;No.&amp;nbsp; You are thinking of INDEX() or FIND().&amp;nbsp; The WHICHC() function is doing an EQUALITY test.&amp;nbsp; It returns the index number of the first value that matches.&amp;nbsp; So if VAR2 is the first variable with that value then it returns 2. When none match then it returns zero.&amp;nbsp; The IF statement will treat zero as FALSE and any other actual number as TRUE.&lt;/P&gt;</description>
      <pubDate>Wed, 29 Apr 2026 18:58:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-help-with-do-in-a-macro/m-p/986993#M380012</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2026-04-29T18:58:14Z</dc:date>
    </item>
    <item>
      <title>Re: Need help with %do in a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-help-with-do-in-a-macro/m-p/986995#M380013</link>
      <description>&lt;P&gt;So&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;&amp;nbsp;has provided a very good solution. Let me try to explain why your original code doesn't work.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The %IF statement cannot access data set variable values. So you want to test to see if, for example, VAR2, has a value that is equal to 'Related'. %IF cannot do this, it has no idea what the values of VAR2 at any point in time. Data step tools, such as ARRAYs, the WHICHC function, and the good old IF statement, can do this.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So had the code been written with data step tools it might have worked. Please keep this in mind in the future when you try to write macros.&lt;/P&gt;</description>
      <pubDate>Wed, 29 Apr 2026 19:39:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-help-with-do-in-a-macro/m-p/986995#M380013</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2026-04-29T19:39:05Z</dc:date>
    </item>
    <item>
      <title>Re: Need help with %do in a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-help-with-do-in-a-macro/m-p/986996#M380014</link>
      <description>&lt;P&gt;In addition, your %DO loop started with &lt;FONT face="courier new,courier"&gt;&amp;amp;i=2&lt;/FONT&gt; and thus skipped the first element of &lt;FONT face="courier new,courier"&gt;&amp;amp;varlist&lt;/FONT&gt;, i.e., &lt;FONT face="courier new,courier"&gt;var2&lt;/FONT&gt;&amp;nbsp;in your example. It ended with &lt;FONT face="courier new,courier"&gt;&amp;amp;i=11&lt;/FONT&gt;, but there is no 11th element in the variable list you supplied to the macro. (You were probably thinking of code like&amp;nbsp;&lt;FONT face="courier new,courier"&gt;if var&amp;amp;i = "&amp;amp;value" then&lt;/FONT&gt; ...)&lt;/P&gt;</description>
      <pubDate>Wed, 29 Apr 2026 19:53:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-help-with-do-in-a-macro/m-p/986996#M380014</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2026-04-29T19:53:20Z</dc:date>
    </item>
  </channel>
</rss>

