<?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 how to keep previous value in variable with conditions in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/how-to-keep-previous-value-in-variable-with-conditions/m-p/915790#M360821</link>
    <description>&lt;P&gt;I want to fill missing observation in status variable on 2 condition&lt;/P&gt;&lt;P&gt;1. if bucket =4 then status= "bad"&lt;/P&gt;&lt;P&gt;2. if previous bucket ="bad" and current bucket &amp;lt;4 then status = "recover"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;so based on these two condition all missing obs should be "recover" but could not apply this logic on sas. Please help&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;id&amp;nbsp; bucket status&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; 0&amp;nbsp; &amp;nbsp;good&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; 4&amp;nbsp; &amp;nbsp; bad&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; 0&amp;nbsp; &amp;nbsp; ""&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; 0&amp;nbsp; &amp;nbsp;""&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; ""&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; 2&amp;nbsp; &amp;nbsp;""&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; 3&amp;nbsp; &amp;nbsp;""&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; 4&amp;nbsp; &amp;nbsp; bad&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 13 Feb 2024 11:00:57 GMT</pubDate>
    <dc:creator>ash3</dc:creator>
    <dc:date>2024-02-13T11:00:57Z</dc:date>
    <item>
      <title>how to keep previous value in variable with conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-keep-previous-value-in-variable-with-conditions/m-p/915790#M360821</link>
      <description>&lt;P&gt;I want to fill missing observation in status variable on 2 condition&lt;/P&gt;&lt;P&gt;1. if bucket =4 then status= "bad"&lt;/P&gt;&lt;P&gt;2. if previous bucket ="bad" and current bucket &amp;lt;4 then status = "recover"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;so based on these two condition all missing obs should be "recover" but could not apply this logic on sas. Please help&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;id&amp;nbsp; bucket status&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; 0&amp;nbsp; &amp;nbsp;good&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; 4&amp;nbsp; &amp;nbsp; bad&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; 0&amp;nbsp; &amp;nbsp; ""&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; 0&amp;nbsp; &amp;nbsp;""&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; ""&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; 2&amp;nbsp; &amp;nbsp;""&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; 3&amp;nbsp; &amp;nbsp;""&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; 4&amp;nbsp; &amp;nbsp; bad&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 13 Feb 2024 11:00:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-keep-previous-value-in-variable-with-conditions/m-p/915790#M360821</guid>
      <dc:creator>ash3</dc:creator>
      <dc:date>2024-02-13T11:00:57Z</dc:date>
    </item>
    <item>
      <title>Re: how to keep previous value in variable with conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-keep-previous-value-in-variable-with-conditions/m-p/915793#M360823</link>
      <description>&lt;P&gt;2 points:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Your desired result indicate that the logic should not be 'if previous bucket ..' . It should be 'if &lt;EM&gt;any&amp;nbsp;&lt;/EM&gt;of the previous bucket values within the same id group equals 4.&lt;/LI&gt;
&lt;LI&gt;I take it that the logic should hold for different by-groups.&amp;nbsp;&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try the code below.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id bucket;
datalines;
1 0 
1 4 
1 0 
1 0 
1 1 
1 2 
1 3 
1 4 
2 0 
2 4 
2 0 
2 0 
2 1 
2 2 
2 3 
2 4 
;

data want(drop = prev_4);
   set have;
   by id;

   if first.id then prev_4 = 0;
   if bucket = 4 then prev_4 = 1;

   length status $20;

   status = ifc(bucket = 4, 'bad', 'good');

   if prev_4 = 1 &amp;amp; bucket &amp;lt; 4 then status = 'recover';

   retain prev_4;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;U&gt;&lt;STRONG&gt;Result:&lt;/STRONG&gt;&lt;/U&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;id  bucket  status
1   0       good
1   4       bad
1   0       recover
1   0       recover
1   1       recover
1   2       recover
1   3       recover
1   4       bad
2   0       good
2   4       bad
2   0       recover
2   0       recover
2   1       recover
2   2       recover
2   3       recover
2   4       bad&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 13 Feb 2024 11:48:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-keep-previous-value-in-variable-with-conditions/m-p/915793#M360823</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2024-02-13T11:48:30Z</dc:date>
    </item>
    <item>
      <title>Re: how to keep previous value in variable with conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-keep-previous-value-in-variable-with-conditions/m-p/915796#M360825</link>
      <description>&lt;P&gt;What should happen in this case?&lt;/P&gt;
&lt;PRE&gt;id  bucket status
42    0   good
42    0   ?
42    0   ?
42    1   ?
42    2   ?
42    3   ?
42    4    bad&lt;/PRE&gt;
&lt;P&gt;Or is such a case not possible?&lt;/P&gt;</description>
      <pubDate>Tue, 13 Feb 2024 11:54:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-keep-previous-value-in-variable-with-conditions/m-p/915796#M360825</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2024-02-13T11:54:44Z</dc:date>
    </item>
  </channel>
</rss>

