<?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 Same IF statement across different variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Same-IF-statement-across-different-variables/m-p/933727#M367228</link>
    <description>&lt;P&gt;I would like to use the same if statement across different variables. I have 31 of these variables and would like to avoid typing multiple IF statements. Is there a more elegant way to achieve this? Here is my code:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data want;
set have;

%let ab = ab1-ab31;

if a1 = '0' and b1='0' then ab1='1';
else ab1='0';
if a2 = '0' and b2='0' then ab2='1';
else ab2='0';
if a3= '0' and b3='0' then ab3='1';
else ab3='0';

...

if a31 = '0' and b31='0' then ab31='1';
else ab31='0';

n_ab=sum((countc(cats(of &amp;amp;ab),'1')));

drop &amp;amp;ab;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 25 Jun 2024 22:29:15 GMT</pubDate>
    <dc:creator>jmontefalcon</dc:creator>
    <dc:date>2024-06-25T22:29:15Z</dc:date>
    <item>
      <title>Same IF statement across different variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Same-IF-statement-across-different-variables/m-p/933727#M367228</link>
      <description>&lt;P&gt;I would like to use the same if statement across different variables. I have 31 of these variables and would like to avoid typing multiple IF statements. Is there a more elegant way to achieve this? Here is my code:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data want;
set have;

%let ab = ab1-ab31;

if a1 = '0' and b1='0' then ab1='1';
else ab1='0';
if a2 = '0' and b2='0' then ab2='1';
else ab2='0';
if a3= '0' and b3='0' then ab3='1';
else ab3='0';

...

if a31 = '0' and b31='0' then ab31='1';
else ab31='0';

n_ab=sum((countc(cats(of &amp;amp;ab),'1')));

drop &amp;amp;ab;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 25 Jun 2024 22:29:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Same-IF-statement-across-different-variables/m-p/933727#M367228</guid>
      <dc:creator>jmontefalcon</dc:creator>
      <dc:date>2024-06-25T22:29:15Z</dc:date>
    </item>
    <item>
      <title>Re: Same IF statement across different variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Same-IF-statement-across-different-variables/m-p/933729#M367230</link>
      <description>&lt;P&gt;Define arrays, and use an iterative DO loop.&lt;/P&gt;</description>
      <pubDate>Tue, 25 Jun 2024 22:33:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Same-IF-statement-across-different-variables/m-p/933729#M367230</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2024-06-25T22:33:54Z</dc:date>
    </item>
    <item>
      <title>Re: Same IF statement across different variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Same-IF-statement-across-different-variables/m-p/933730#M367231</link>
      <description>&lt;P&gt;Since you're only interested in the sum of true conditions, this should do it:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
n_ab = 0;
array a {*} a1-a31;
array b {*} b1-b31;
do i = 1 to 31;
  n_ab = n_ab + (a{i} = "0" and b{i} = "0");
end;
drop i;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 25 Jun 2024 22:40:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Same-IF-statement-across-different-variables/m-p/933730#M367231</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2024-06-25T22:40:26Z</dc:date>
    </item>
    <item>
      <title>Re: Same IF statement across different variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Same-IF-statement-across-different-variables/m-p/933731#M367232</link>
      <description>&lt;P&gt;This may be a silly question to you but why are you creating so many character variables with values of '1' and '0' instead of numeric variables? Especially considering you are dropping them ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My take assumes you don't already have AB variables so create numeric versions to SUM to get that count.&lt;/P&gt;
&lt;PRE&gt;data want;
   set have;
   array a(*) a1-a31;
   array b(*) b1-b31;
   array ab(*) ab1-ab31;

   do i=1 to dim(a);
      ab[i]=( a[i]='0' and b[i]='0');
   end;
   
   n_ab=sum(of ab(*));
   drop i ab1-ab31 ;
run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 25 Jun 2024 22:43:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Same-IF-statement-across-different-variables/m-p/933731#M367232</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-06-25T22:43:39Z</dc:date>
    </item>
    <item>
      <title>Re: Same IF statement across different variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Same-IF-statement-across-different-variables/m-p/933758#M367246</link>
      <description>Hello,&lt;BR /&gt;&lt;BR /&gt;The character variables values each represent tooth conditions (tc1_2 for tooth condition 1 for tooth #2 &amp;amp; tc2_2 for tooth condition 2 for tooth #2).&lt;BR /&gt;&lt;BR /&gt;There are 28 teeth total with tooth condition codes ranging from 0 to 9 and need to obtain sum variables based on different combinations of tooth conditions for each ID.&lt;BR /&gt;&lt;BR /&gt;Not sure if this answers your question, but this is what the code is supposed to be representing.</description>
      <pubDate>Wed, 26 Jun 2024 05:32:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Same-IF-statement-across-different-variables/m-p/933758#M367246</guid>
      <dc:creator>jmontefalcon</dc:creator>
      <dc:date>2024-06-26T05:32:53Z</dc:date>
    </item>
  </channel>
</rss>

