<?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 nested IF/ELSE conditions and previous observations in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Need-help-with-nested-IF-ELSE-conditions-and-previous/m-p/676384#M203938</link>
    <description>&lt;PRE&gt;&amp;nbsp;&lt;/PRE&gt;&lt;P&gt;Posting the output data snapshot:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;SVCDATE&lt;/TD&gt;&lt;TD&gt;DAYSUPP&lt;/TD&gt;&lt;TD&gt;INDEXDT&lt;/TD&gt;&lt;TD&gt;ENROLID&lt;/TD&gt;&lt;TD&gt;SVCDATE_NEW&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;7/25/16&lt;/TD&gt;&lt;TD&gt;30&lt;/TD&gt;&lt;TD&gt;7/25/16&lt;/TD&gt;&lt;TD&gt;128682503&lt;/TD&gt;&lt;TD&gt;7/25/16&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;8/22/16&lt;/TD&gt;&lt;TD&gt;30&lt;/TD&gt;&lt;TD&gt;7/25/16&lt;/TD&gt;&lt;TD&gt;128682503&lt;/TD&gt;&lt;TD&gt;8/24/16&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;9/13/16&lt;/TD&gt;&lt;TD&gt;6&lt;/TD&gt;&lt;TD&gt;7/25/16&lt;/TD&gt;&lt;TD&gt;128682503&lt;/TD&gt;&lt;TD&gt;9/13/16&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;9/15/16&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;7/25/16&lt;/TD&gt;&lt;TD&gt;128682503&lt;/TD&gt;&lt;TD&gt;9/15/16&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;9/20/16&lt;/TD&gt;&lt;TD&gt;30&lt;/TD&gt;&lt;TD&gt;7/25/16&lt;/TD&gt;&lt;TD&gt;128682503&lt;/TD&gt;&lt;TD&gt;10/15/16&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;10/19/16&lt;/TD&gt;&lt;TD&gt;30&lt;/TD&gt;&lt;TD&gt;7/25/16&lt;/TD&gt;&lt;TD&gt;128682503&lt;/TD&gt;&lt;TD&gt;11/14/16&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;11/17/16&lt;/TD&gt;&lt;TD&gt;30&lt;/TD&gt;&lt;TD&gt;7/25/16&lt;/TD&gt;&lt;TD&gt;128682503&lt;/TD&gt;&lt;TD&gt;12/14/16&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;DIV class="yj6qo"&gt;&amp;nbsp;&lt;/DIV&gt;</description>
    <pubDate>Thu, 13 Aug 2020 03:35:59 GMT</pubDate>
    <dc:creator>alaxman</dc:creator>
    <dc:date>2020-08-13T03:35:59Z</dc:date>
    <item>
      <title>Need help with nested IF/ELSE conditions and previous observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-help-with-nested-IF-ELSE-conditions-and-previous/m-p/676374#M203928</link>
      <description>&lt;P&gt;This is my code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="p1"&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data MS_AL.pers;
set MS_AL.PERSISTENCE;
by enrolid svcdate;

if first.enrolid then do;
svcdate_new=indexdt;
retain svcdate_new daysupp;
end;

else if svcdate_new+daysupp &amp;gt; svcdate then do;
svcdate_new = svcdate_new+daysupp;
end;&lt;BR /&gt;
else do; svcdate_new = svcdate; end;

format svcdate_new mmddyy10.;
run;&lt;/PRE&gt;&lt;P class="p1"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This gives the output like this:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="Screen Shot 2020-08-12 at 9.57.27 PM.jpeg" style="width: 327px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/48186i270AAFB38A13320D/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Screen Shot 2020-08-12 at 9.57.27 PM.jpeg" alt="Screen Shot 2020-08-12 at 9.57.27 PM.jpeg" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Essentially what I'm trying to do is to create a new column with shifted dates - &lt;EM&gt;svcdate_new.&amp;nbsp;&lt;/EM&gt;The condition for shifting is that:&lt;/P&gt;&lt;P&gt;1. First row is always indexdt&lt;/P&gt;&lt;P&gt;2. For all subsequent rows, if svcdate (for example in the 2nd row it is 8/22/16) &amp;lt; previous svcdate_new (in this case 7/25/16) + daysupp from previous row (30 in this case), then svcdate_new (for second row) = svcdate_new from previous row (7/25/16) + daysupp from previous row (30 in this case) which it computes correctly as &lt;STRONG&gt;8/25/16.&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, for the 3rd row, svcdate_new should be &lt;STRONG&gt;8/24/16 +30 = 9/23/2016.&amp;nbsp;&lt;/STRONG&gt;However, it is incorrectly computing as svcdate.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can someone help?&lt;/P&gt;</description>
      <pubDate>Thu, 13 Aug 2020 03:12:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-help-with-nested-IF-ELSE-conditions-and-previous/m-p/676374#M203928</guid>
      <dc:creator>alaxman</dc:creator>
      <dc:date>2020-08-13T03:12:34Z</dc:date>
    </item>
    <item>
      <title>Re: Need help with nested IF/ELSE conditions and previous observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-help-with-nested-IF-ELSE-conditions-and-previous/m-p/676379#M203933</link>
      <description>&lt;P&gt;Could you please post the sample data to copy and paste, in picture format it is not possible to copy and test.&lt;/P&gt;</description>
      <pubDate>Thu, 13 Aug 2020 03:23:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-help-with-nested-IF-ELSE-conditions-and-previous/m-p/676379#M203933</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2020-08-13T03:23:27Z</dc:date>
    </item>
    <item>
      <title>Re: Need help with nested IF/ELSE conditions and previous observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-help-with-nested-IF-ELSE-conditions-and-previous/m-p/676383#M203937</link>
      <description>SVCDATE DAYSUPP INDEXDT ENROLID SVCDATE_NEW&lt;BR /&gt;7/25/16 30 7/25/16 128682503 7/25/16&lt;BR /&gt;8/22/16 30 7/25/16 128682503 8/24/16&lt;BR /&gt;9/13/16 6 7/25/16 128682503 9/13/16&lt;BR /&gt;9/15/16 2 7/25/16 128682503 9/15/16&lt;BR /&gt;9/20/16 30 7/25/16 128682503 10/15/16&lt;BR /&gt;&lt;BR /&gt;Here you go. Thanks!&lt;BR /&gt;10/19/16 30 7/25/16 128682503 11/14/16&lt;BR /&gt;11/17/16 30 7/25/16 128682503 12/14/16&lt;BR /&gt;</description>
      <pubDate>Thu, 13 Aug 2020 03:34:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-help-with-nested-IF-ELSE-conditions-and-previous/m-p/676383#M203937</guid>
      <dc:creator>alaxman</dc:creator>
      <dc:date>2020-08-13T03:34:06Z</dc:date>
    </item>
    <item>
      <title>Re: Need help with nested IF/ELSE conditions and previous observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-help-with-nested-IF-ELSE-conditions-and-previous/m-p/676384#M203938</link>
      <description>&lt;PRE&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gt;&lt;TD&gt;30&lt;/TD&gt;&lt;TD&gt;7/25/16&lt;/TD&gt;&lt;TD&gt;128682503&lt;/TD&gt;&lt;TD&gt;11/14/16&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;11/17/16&lt;/TD&gt;&lt;TD&gt;30&lt;/TD&gt;&lt;TD&gt;7/25/16&lt;/TD&gt;&lt;TD&gt;128682503&lt;/TD&gt;&lt;TD&gt;12/14/16&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;DIV class="yj6qo"&gt;&amp;nbsp;&lt;/DIV&gt;</description>
      <pubDate>Thu, 13 Aug 2020 03:35:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-help-with-nested-IF-ELSE-conditions-and-previous/m-p/676384#M203938</guid>
      <dc:creator>alaxman</dc:creator>
      <dc:date>2020-08-13T03:35:59Z</dc:date>
    </item>
    <item>
      <title>Re: Need help with nested IF/ELSE conditions and previous observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-help-with-nested-IF-ELSE-conditions-and-previous/m-p/676388#M203940</link>
      <description>&lt;P&gt;Try next not tested code.&lt;/P&gt;
&lt;P&gt;To add previous row daysupp to need to save it (remain) otherwise&lt;/P&gt;
&lt;P&gt;the current row overrides it.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data MS_AL.pers;
set MS_AL.PERSISTENCE;
by enrolid svcdate;
retain svcdate_new svdaysupp;

if first.enrolid then do;
   svcdate_new=indexdt;
   svdaysupp = daysupp;
end;

else if svcdate_new+svdaysupp &amp;gt; svcdate then do;
   svcdate_new = svcdate_new+svdaysupp;
end;

else do; svcdate_new = svcdate; end;
svdaysupp = daysupp;

drop svdaysupp; /* ? */
format svcdate_new mmddyy10.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 13 Aug 2020 04:42:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-help-with-nested-IF-ELSE-conditions-and-previous/m-p/676388#M203940</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2020-08-13T04:42:42Z</dc:date>
    </item>
    <item>
      <title>Re: Need help with nested IF/ELSE conditions and previous observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-help-with-nested-IF-ELSE-conditions-and-previous/m-p/676391#M203942</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/341454"&gt;@alaxman&lt;/a&gt;: Posting data in usable form, not in screenshots or tables helps us to provide working solutions.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The retain-statement is non-conditional, meaning that it is always executed, even if it is part of an if- or else-block. The retain-statement marks variables, so that in during the automatic iterations values are not reset to missing, but this does not affect variables loaded from a dataset or read with input-statement. So retaining daysup is not possible, you have to create a new variable to be retained (see the solution posted by &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/88384"&gt;@Shmuel&lt;/a&gt;)&lt;/P&gt;</description>
      <pubDate>Thu, 13 Aug 2020 04:45:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-help-with-nested-IF-ELSE-conditions-and-previous/m-p/676391#M203942</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2020-08-13T04:45:25Z</dc:date>
    </item>
    <item>
      <title>Re: Need help with nested IF/ELSE conditions and previous observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-help-with-nested-IF-ELSE-conditions-and-previous/m-p/676422#M203953</link>
      <description>&lt;P&gt;You can take advantage of the fact that variables read by a SET statement are automatically retained until that SET statement is encountered again.&amp;nbsp; Usually a set statement executes once per incoming record.&amp;nbsp; But below there are two sets.&amp;nbsp; The second is executed the same number of times as the first, but it executes in batches - once per group.&amp;nbsp; As a result the variables svcdate_new and _svdaysupp are retained until over the course of each ENROLID group:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=_:);
  set have;
  by enrolid;

  if first.enrolid then do until (first.enrolid);
    set have (rename=(indexdt=svcdate_new daysupp=_svdaysupp));
	by enrolid ;
  end;
  else if svcdate_new + _svdaysupp&amp;gt;svcdate then svcdate_new=svcdte_new + _svdaysupp;
  else svcdate_new=svcdate;

  _svdaysupp=daysupp;
  format svcdate_new mmddyy10. ;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I think the interesting feature here is the&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  if first.enrolid then do until (first.enrolid);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;statement.&amp;nbsp; The "if first.enrolid" condition refers to the PRECEDING set ... by statements.&amp;nbsp; But the "do until (first.enrolid)" refers to the FOLLOWING&amp;nbsp; set ... by statements.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 13 Aug 2020 07:59:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-help-with-nested-IF-ELSE-conditions-and-previous/m-p/676422#M203953</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-08-13T07:59:43Z</dc:date>
    </item>
    <item>
      <title>Re: Need help with nested IF/ELSE conditions and previous observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-help-with-nested-IF-ELSE-conditions-and-previous/m-p/676479#M203984</link>
      <description>Thanks mkeintz! This worked. I definitely learned about the do until. That indeed was the interesting part here. Appreciate the help</description>
      <pubDate>Thu, 13 Aug 2020 13:23:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-help-with-nested-IF-ELSE-conditions-and-previous/m-p/676479#M203984</guid>
      <dc:creator>alaxman</dc:creator>
      <dc:date>2020-08-13T13:23:46Z</dc:date>
    </item>
  </channel>
</rss>

