<?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 Multiple do and if else if understanding in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Multiple-do-and-if-else-if-understanding/m-p/712986#M219890</link>
    <description>&lt;P&gt;I would like to understand how to tackle the multilple conditions in do, if and else if. Any help to arrange the do blocks from below profram in right way? I guess first three do blocks are not in right way.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In the below program, if the flag value is 1, then I want &lt;STRONG&gt;target="&amp;amp;nl_path"||"/"||filename&lt;/STRONG&gt;; but after executing the program it produces &lt;STRONG&gt;target="&amp;amp;lh_path"||"/"||filename;. &lt;/STRONG&gt;It looks last else statement had statisfied instead of second if. Flag value is correctly appearing it as 1. So I want&amp;nbsp;&lt;STRONG&gt;target="&amp;amp;nl_path"||"/"||filename&lt;/STRONG&gt;;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data files ;
  length id 8 msg filename source target $256 ;
  did=dopen("source");
  if did&amp;lt;=0 then do;
      msg=sysmsg(); 
      put msg; 
      stop;
  end;
  do id=1 to dnum(did);
    filename=dread(did,id);

    if scan(lowcase(filename),-1,'.')='csv' then do;
      source="&amp;amp;inpath" ||"/"|| filename;
      flag = 0;

    do i = 1 to countw("&amp;amp;nl_rb.",",");
     if indexw(filename,scan("&amp;amp;nl_rb.",i,","),"_") then do;
     flag = 1;
     target="&amp;amp;nl_path"||"/"||filename;
   end;
   end;

     if indexw(filename,'_copied') and  "&amp;amp;function" EQ 'LH' then do;
        target="&amp;amp;lh_path"||"/"||transtrn(filename,'_copied',trimn(' '));
        end;

     else if indexw(filename,'_copied') and  "&amp;amp;function" EQ 'NL' then do;
        target="&amp;amp;nl_path"||"/"||transtrn(filename,'_copied',trimn(' '));
      end;

      else target="&amp;amp;lh_path"||"/"||filename;
    end;
    output;
  end;
  did=dclose(did);
  drop did msg;
run;
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 21 Jan 2021 07:12:56 GMT</pubDate>
    <dc:creator>David_Billa</dc:creator>
    <dc:date>2021-01-21T07:12:56Z</dc:date>
    <item>
      <title>Multiple do and if else if understanding</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Multiple-do-and-if-else-if-understanding/m-p/712986#M219890</link>
      <description>&lt;P&gt;I would like to understand how to tackle the multilple conditions in do, if and else if. Any help to arrange the do blocks from below profram in right way? I guess first three do blocks are not in right way.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In the below program, if the flag value is 1, then I want &lt;STRONG&gt;target="&amp;amp;nl_path"||"/"||filename&lt;/STRONG&gt;; but after executing the program it produces &lt;STRONG&gt;target="&amp;amp;lh_path"||"/"||filename;. &lt;/STRONG&gt;It looks last else statement had statisfied instead of second if. Flag value is correctly appearing it as 1. So I want&amp;nbsp;&lt;STRONG&gt;target="&amp;amp;nl_path"||"/"||filename&lt;/STRONG&gt;;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data files ;
  length id 8 msg filename source target $256 ;
  did=dopen("source");
  if did&amp;lt;=0 then do;
      msg=sysmsg(); 
      put msg; 
      stop;
  end;
  do id=1 to dnum(did);
    filename=dread(did,id);

    if scan(lowcase(filename),-1,'.')='csv' then do;
      source="&amp;amp;inpath" ||"/"|| filename;
      flag = 0;

    do i = 1 to countw("&amp;amp;nl_rb.",",");
     if indexw(filename,scan("&amp;amp;nl_rb.",i,","),"_") then do;
     flag = 1;
     target="&amp;amp;nl_path"||"/"||filename;
   end;
   end;

     if indexw(filename,'_copied') and  "&amp;amp;function" EQ 'LH' then do;
        target="&amp;amp;lh_path"||"/"||transtrn(filename,'_copied',trimn(' '));
        end;

     else if indexw(filename,'_copied') and  "&amp;amp;function" EQ 'NL' then do;
        target="&amp;amp;nl_path"||"/"||transtrn(filename,'_copied',trimn(' '));
      end;

      else target="&amp;amp;lh_path"||"/"||filename;
    end;
    output;
  end;
  did=dclose(did);
  drop did msg;
run;
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 21 Jan 2021 07:12:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Multiple-do-and-if-else-if-understanding/m-p/712986#M219890</guid>
      <dc:creator>David_Billa</dc:creator>
      <dc:date>2021-01-21T07:12:56Z</dc:date>
    </item>
    <item>
      <title>Re: Multiple do and if else if understanding</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Multiple-do-and-if-else-if-understanding/m-p/712988#M219892</link>
      <description>&lt;P&gt;You may want to add some text explaining what the program should do and what it is doing wrong.&lt;/P&gt;</description>
      <pubDate>Thu, 21 Jan 2021 07:05:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Multiple-do-and-if-else-if-understanding/m-p/712988#M219892</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2021-01-21T07:05:33Z</dc:date>
    </item>
    <item>
      <title>Re: Multiple do and if else if understanding</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Multiple-do-and-if-else-if-understanding/m-p/712989#M219893</link>
      <description>&lt;P&gt;updated my initial post.&lt;/P&gt;</description>
      <pubDate>Thu, 21 Jan 2021 07:13:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Multiple-do-and-if-else-if-understanding/m-p/712989#M219893</guid>
      <dc:creator>David_Billa</dc:creator>
      <dc:date>2021-01-21T07:13:25Z</dc:date>
    </item>
    <item>
      <title>Re: Multiple do and if else if understanding</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Multiple-do-and-if-else-if-understanding/m-p/712991#M219895</link>
      <description>&lt;P&gt;Check if&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;indexw(filename,'_copied')&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;actually results in the position you are looking for.&lt;/P&gt;</description>
      <pubDate>Thu, 21 Jan 2021 07:18:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Multiple-do-and-if-else-if-understanding/m-p/712991#M219895</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-01-21T07:18:30Z</dc:date>
    </item>
    <item>
      <title>Re: Multiple do and if else if understanding</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Multiple-do-and-if-else-if-understanding/m-p/712994#M219896</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;In the filename there is no string called '_copied' is available. So this condition&amp;nbsp;&lt;/P&gt;
&lt;PRE class="language-sas"&gt;&lt;CODE&gt;indexw(filename,'_copied')&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;should not statisfy.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 21 Jan 2021 07:37:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Multiple-do-and-if-else-if-understanding/m-p/712994#M219896</guid>
      <dc:creator>David_Billa</dc:creator>
      <dc:date>2021-01-21T07:37:40Z</dc:date>
    </item>
    <item>
      <title>Re: Multiple do and if else if understanding</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Multiple-do-and-if-else-if-understanding/m-p/712995#M219897</link>
      <description>&lt;P&gt;You can shorten the next code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  if indexw(filename,'_copied') and  "&amp;amp;function" EQ 'LH' then do;
        target="&amp;amp;lh_path"||"/"||transtrn(filename,'_copied',trimn(' '));
        end;

     else if indexw(filename,'_copied') and  "&amp;amp;function" EQ 'NL' then do;
        target="&amp;amp;nl_path"||"/"||transtrn(filename,'_copied',trimn(' '));
      end;

      else target="&amp;amp;lh_path"||"/"||filename;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;by replacing it to:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  if indexw(filename,'_copied') then do;
    if  "&amp;amp;function" EQ 'LH' then 
        target="&amp;amp;lh_path"||"/"||transtrn(filename,'_copied',trimn(' '));
    else if "&amp;amp;function" EQ 'NL' then 
        target="&amp;amp;nl_path"||"/"||transtrn(filename,'_copied',trimn(' '));
  end;
  else target="&amp;amp;lh_path"||"/"||filename;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 21 Jan 2021 07:47:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Multiple-do-and-if-else-if-understanding/m-p/712995#M219897</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2021-01-21T07:47:14Z</dc:date>
    </item>
    <item>
      <title>Re: Multiple do and if else if understanding</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Multiple-do-and-if-else-if-understanding/m-p/712999#M219898</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/292396"&gt;@David_Billa&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;In the filename there is no string called '_copied' is available. So this condition&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;indexw(filename,'_copied')&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;should not statisfy.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;If there is no '_copied' substring in the filename&amp;nbsp;it explains the output you get, as the last ELSE is&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;else target="&amp;amp;lh_path"||"/"||filename;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 21 Jan 2021 08:02:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Multiple-do-and-if-else-if-understanding/m-p/712999#M219898</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2021-01-21T08:02:18Z</dc:date>
    </item>
    <item>
      <title>Re: Multiple do and if else if understanding</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Multiple-do-and-if-else-if-understanding/m-p/713006#M219903</link>
      <description>&lt;P&gt;&lt;STRONG&gt;MAXIM 34.&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;MAXIM 34.&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;MAXIM 34.&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;WORK&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;IN&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;STEPS&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;!!!&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Test your conditions separately on an example string in a simple data step, so you can make sure they work BEFORE you use them in a larger piece of code where they are harder to debug.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Separate the directory read out into its own data step, so you get a dataset with all filenames to inspect first.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And make it a habit to use consistent indentation, so you can visually identify the blocks:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data files ;
length
  id 8
  msg filename source target $256
;
did = dopen("source");
if did &amp;lt;= 0
then do;
  msg = sysmsg(); 
  put msg; 
  stop;
end;
do id=1 to dnum(did);
  filename = dread(did,id);
  if scan(lowcase(filename),-1,'.') = 'csv'
  then do;
    source = "&amp;amp;inpath" ||"/"|| filename;
    flag = 0;
    do i = 1 to countw("&amp;amp;nl_rb.",",");
      if indexw(filename,scan("&amp;amp;nl_rb.",i,","),"_")
      then do;
        flag = 1;
        target = "&amp;amp;nl_path"||"/"||filename;
      end;
    end;
    if indexw(filename,'_copied') and "&amp;amp;function" eq 'LH'
    then do;
      target = "&amp;amp;lh_path"||"/"||transtrn(filename,'_copied',trimn(' '));
    end;
    else if indexw(filename,'_copied') and "&amp;amp;function" eq 'NL'
    then do;
      target="&amp;amp;nl_path"||"/"||transtrn(filename,'_copied',trimn(' '));
    end;
    else target = "&amp;amp;lh_path"||"/"||filename;
    /* this most likely causes your problems;
    if _copied is not present, this statement will override anything else */
  end;
  output;
end;
did = dclose(did);
drop did msg;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;See my comment remark. Making your code readable (Maxim 12) quickly let me see where the issue might be.&lt;/P&gt;
&lt;P&gt;Your final block should be simplified to this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;    if indexw(filename,'_copied')
    then do;
      if "&amp;amp;function" eq 'LH'
      then target = "&amp;amp;lh_path"||"/"||transtrn(filename,'_copied',trimn(' '));
      else if "&amp;amp;function" eq 'NL'
      then target="&amp;amp;nl_path"||"/"||transtrn(filename,'_copied',trimn(' '));
    end;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You might want to add a check for values of &amp;amp;function not in (LH,NL).&lt;/P&gt;</description>
      <pubDate>Thu, 21 Jan 2021 08:33:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Multiple-do-and-if-else-if-understanding/m-p/713006#M219903</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-01-21T08:33:23Z</dc:date>
    </item>
  </channel>
</rss>

