<?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: Multiple &amp;quot;if then else&amp;quot; with do loop? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Multiple-quot-if-then-else-quot-with-do-loop/m-p/503854#M134725</link>
    <description>&lt;P&gt;You should probably take a step back and explain more about the overall process.&lt;/P&gt;
&lt;P&gt;Why not use the WHICHC() function to find out which is the first empty variable?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;
 input id y n (code1-code3) ($);
cards;
1 1 0 . xxx yyy
2 1 0 aaa bbb .
3 1 0 ccc . .
4 1 0 a b c
5 0 1 d e .
6 1 1 f . .
7 0 0 g . .
;

%let code=NEW;

data want ;
  set have ;
  array code (3);
  array flag (3);
  loc = whichc(' ',of code(*));
  if y and ^n then do;
    if loc then do;
      code(loc)="&amp;amp;code";
      flag(loc)=1;
    end;
    else put "WARNING: No room to add '&amp;amp;code'." id= ;
  end;
  drop loc;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image.png" style="width: 380px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/23980i758E744EE93E71B9/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;But perhaps you just want one "flag" variable? In which case why not have it store which position was used to add the new code?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  set have ;
  array code (3);
  loc = whichc(' ',of code(*));
  if y and ^n then do;
    if loc then code(loc)="&amp;amp;code";
  end;
  else loc=-1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image.png" style="width: 289px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/23981i47864B3CB4195948/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
    <pubDate>Fri, 12 Oct 2018 19:21:32 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2018-10-12T19:21:32Z</dc:date>
    <item>
      <title>Multiple "if then else" with do loop?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Multiple-quot-if-then-else-quot-with-do-loop/m-p/503807#M134693</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there possible to do multiple "if then else" using do loops or array?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is what I want to do : I have a list of about 20,000 patients and I need to assign a code according to their diagnosis (these 350 codes are contained in macro variables and each of them run with a do-loop). Patients can have up to 10 codes assignment and instead of doing if-else-then 10 times, I would like to code something that is more efficient.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is what I have done :&lt;/P&gt;&lt;P&gt;FYI : "y" means that the patient has the corresponding diagnosis and "n" means that the patient does not have word such as "not, no sign of, etc.).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want; set have;

if (y=1) and (n=0) and code1 = ' ' then do;
code = "&amp;amp;code";
flag_1 = 1;
end;

if (y=1) and (n=0) and code2 = ' ' then do;
code = "&amp;amp;code";
flag_2 = 1;
end;

if (y=1) and (n=0) and code3 = ' ' then do;
code = "&amp;amp;code";
flag_3 = 1;
end;

/* And so on until code10 */&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;My actual code verify if y=1, if n=0 and if the code1 variable is empty. If so, it assigns the corresponding code. If the code1 cariable is not empty, it goes to code2 variable and so on.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can anyone help me making this code more efficient? I am looking for something that has a couple lines with a do-loop.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you in advance.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 12 Oct 2018 17:36:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Multiple-quot-if-then-else-quot-with-do-loop/m-p/503807#M134693</guid>
      <dc:creator>dera</dc:creator>
      <dc:date>2018-10-12T17:36:02Z</dc:date>
    </item>
    <item>
      <title>Re: Multiple "if then else" with do loop?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Multiple-quot-if-then-else-quot-with-do-loop/m-p/503811#M134696</link>
      <description>&lt;P&gt;You could&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. concatenate code1-code10&lt;/P&gt;
&lt;P&gt;2. check if the concatentated value is missing&amp;nbsp;&lt;/P&gt;
&lt;P&gt;3. if missing populate flag1-flag10 array as 1 and populate code1-code10 as &amp;amp;code&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 12 Oct 2018 17:43:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Multiple-quot-if-then-else-quot-with-do-loop/m-p/503811#M134696</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-10-12T17:43:29Z</dc:date>
    </item>
    <item>
      <title>Re: Multiple "if then else" with do loop?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Multiple-quot-if-then-else-quot-with-do-loop/m-p/503816#M134700</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/236159"&gt;@dera&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is there possible to do multiple "if then else" using do loops or array?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is what I want to do : I have a list of about 20,000 patients and I need to assign a code according to their diagnosis (these 350 codes are contained in macro variables and each of them run with a do-loop). Patients can have up to 10 codes assignment and instead of doing if-else-then 10 times, I would like to code something that is more efficient.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is what I have done :&lt;/P&gt;
&lt;P&gt;FYI : "y" means that the patient has the corresponding diagnosis and "n" means that the patient does not have word such as "not, no sign of, etc.).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want; set have;

if (y=1) and (n=0) and code1 = ' ' then do;
code = "&amp;amp;code";
flag_1 = 1;
end;

if (y=1) and (n=0) and code2 = ' ' then do;
code = "&amp;amp;code";
flag_2 = 1;
end;

if (y=1) and (n=0) and code3 = ' ' then do;
code = "&amp;amp;code";
flag_3 = 1;
end;

/* And so on until code10 */&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;My actual code verify if y=1, if n=0 and if the code1 variable is empty. If so, it assigns the corresponding code. If the code1 cariable is not empty, it goes to code2 variable and so on.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can anyone help me making this code more efficient? I am looking for something that has a couple lines with a do-loop.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you in advance.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Your description might need some more details, such as possibly some actual values.&lt;/P&gt;
&lt;P&gt;The code you show assigns the exact &lt;STRONG&gt;same&lt;/STRONG&gt; of value of "&amp;amp;code" to the same variable multiple times.&lt;/P&gt;
&lt;P&gt;Does this actually do what you want for code1? I see nothing that looks like a "corresponding code" assignment or detection.&lt;/P&gt;
&lt;P&gt;This is somewhat more efficient code and at least more compact of the steps you demonstrate:&lt;/P&gt;
&lt;PRE&gt;data want;
   set have;
   array c code1-code10; /*or however many code variables you have*/
   array flag_ {10}; /* number matches the number above*/
   if y=1 then do i= 1 to dim(c);
      if c[i]=' ' then do;
         code="&amp;amp;code.";
         flag_[i]=1;
      end;
   end;
run;&lt;/PRE&gt;</description>
      <pubDate>Fri, 12 Oct 2018 17:55:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Multiple-quot-if-then-else-quot-with-do-loop/m-p/503816#M134700</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-10-12T17:55:30Z</dc:date>
    </item>
    <item>
      <title>Re: Multiple "if then else" with do loop?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Multiple-quot-if-then-else-quot-with-do-loop/m-p/503827#M134706</link>
      <description>Hi ballardw,&lt;BR /&gt;&lt;BR /&gt;Your code almost work! The only problem with it is that the assigned code is assign to code1, code2, until code10. Is there a way to tell SAS that when the code is assigned in the code1 variable, don't assign it again to the code2 variable?</description>
      <pubDate>Fri, 12 Oct 2018 18:20:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Multiple-quot-if-then-else-quot-with-do-loop/m-p/503827#M134706</guid>
      <dc:creator>dera</dc:creator>
      <dc:date>2018-10-12T18:20:44Z</dc:date>
    </item>
    <item>
      <title>Re: Multiple "if then else" with do loop?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Multiple-quot-if-then-else-quot-with-do-loop/m-p/503839#M134714</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
array c(*) code:;
array flag {10};
if y=1 and n=0;
do until(k=0);
k=whichc(' ',of c(*));
if k=0 then leave;
c(k)="&amp;amp;code";
flag(k)=1;
end;&lt;BR /&gt;drop k;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 12 Oct 2018 18:49:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Multiple-quot-if-then-else-quot-with-do-loop/m-p/503839#M134714</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-10-12T18:49:00Z</dc:date>
    </item>
    <item>
      <title>Re: Multiple "if then else" with do loop?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Multiple-quot-if-then-else-quot-with-do-loop/m-p/503854#M134725</link>
      <description>&lt;P&gt;You should probably take a step back and explain more about the overall process.&lt;/P&gt;
&lt;P&gt;Why not use the WHICHC() function to find out which is the first empty variable?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;
 input id y n (code1-code3) ($);
cards;
1 1 0 . xxx yyy
2 1 0 aaa bbb .
3 1 0 ccc . .
4 1 0 a b c
5 0 1 d e .
6 1 1 f . .
7 0 0 g . .
;

%let code=NEW;

data want ;
  set have ;
  array code (3);
  array flag (3);
  loc = whichc(' ',of code(*));
  if y and ^n then do;
    if loc then do;
      code(loc)="&amp;amp;code";
      flag(loc)=1;
    end;
    else put "WARNING: No room to add '&amp;amp;code'." id= ;
  end;
  drop loc;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image.png" style="width: 380px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/23980i758E744EE93E71B9/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;But perhaps you just want one "flag" variable? In which case why not have it store which position was used to add the new code?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  set have ;
  array code (3);
  loc = whichc(' ',of code(*));
  if y and ^n then do;
    if loc then code(loc)="&amp;amp;code";
  end;
  else loc=-1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image.png" style="width: 289px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/23981i47864B3CB4195948/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 12 Oct 2018 19:21:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Multiple-quot-if-then-else-quot-with-do-loop/m-p/503854#M134725</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-10-12T19:21:32Z</dc:date>
    </item>
    <item>
      <title>Re: Multiple "if then else" with do loop?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Multiple-quot-if-then-else-quot-with-do-loop/m-p/503879#M134742</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/236159"&gt;@dera&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Hi ballardw,&lt;BR /&gt;&lt;BR /&gt;Your code almost work! The only problem with it is that the assigned code is assign to code1, code2, until code10. Is there a way to tell SAS that when the code is assigned in the code1 variable, don't assign it again to the code2 variable?&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Without input data, values such as the macro variable and what the result is supposed to actually look like it's pretty hard to tell what to suggest.&lt;/P&gt;
&lt;P&gt;As I mentioned, it only shortens the code duplicating what you had. That is why I asked if what you wrote was actually doing what you intended.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 12 Oct 2018 19:48:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Multiple-quot-if-then-else-quot-with-do-loop/m-p/503879#M134742</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-10-12T19:48:20Z</dc:date>
    </item>
  </channel>
</rss>

