<?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: Using retain to preserve a value for subsequent observations in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Using-retain-to-preserve-a-value-for-subsequent-observations/m-p/481897#M71627</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16961"&gt;@ChrisNZ&lt;/a&gt;'s is an improvement over my suggestion, in that it handles consecutive CONDITION=1 cases more appropriately.&amp;nbsp; Namely it takes the most recent VALUE of the two qualifying records, not the maximum valuje.&lt;/P&gt;</description>
    <pubDate>Fri, 27 Jul 2018 15:25:29 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2018-07-27T15:25:29Z</dc:date>
    <item>
      <title>Using retain to preserve a value for subsequent observations</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Using-retain-to-preserve-a-value-for-subsequent-observations/m-p/481563#M71613</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm a sas beginner struggling quite a bit with retain.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have&amp;nbsp;daily data where when a specific condition is met, I want to preserve that value for the subsequent two days, so in essence create the following value_lagged variable.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Date&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Condition&amp;nbsp; &amp;nbsp; Value&amp;nbsp; &amp;nbsp; &amp;nbsp;Value_lagged&lt;/P&gt;&lt;P&gt;1/1/2005&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&lt;/P&gt;&lt;P&gt;1/2/2005&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;5&amp;nbsp;&lt;/P&gt;&lt;P&gt;1/3/2005&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;5&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;1/4/2005&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;1/5/2005&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;1/6/2005&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 26 Jul 2018 15:43:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Using-retain-to-preserve-a-value-for-subsequent-observations/m-p/481563#M71613</guid>
      <dc:creator>wwuuuww</dc:creator>
      <dc:date>2018-07-26T15:43:26Z</dc:date>
    </item>
    <item>
      <title>Re: Using retain to preserve a value for subsequent observations</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Using-retain-to-preserve-a-value-for-subsequent-observations/m-p/481574#M71614</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Date:mmddyy10. Condition Value;
format Date mmddyy10.;
datalines;
1/1/2005 1 5
1/2/2005 0 0
1/3/2005 0 0
1/4/2005 0 2
1/5/2005 0 3
1/6/2005 0 0
;

data want;
   set have;
   Value_lagged=0;
   lag1Condition=lag1(Condition);
   lag2Condition=lag2(Condition);
   lag1Value=lag1(Value);
   lag2Value=lag2(Value);
   if lag1Condition=1 then do;
      Value_lagged=lag1Value;
   end;
   if lag2Condition=1 then do;
      Value_lagged=lag2Value;
   end;
   drop lag:;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 26 Jul 2018 16:10:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Using-retain-to-preserve-a-value-for-subsequent-observations/m-p/481574#M71614</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2018-07-26T16:10:49Z</dc:date>
    </item>
    <item>
      <title>Re: Using retain to preserve a value for subsequent observations</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Using-retain-to-preserve-a-value-for-subsequent-observations/m-p/481575#M71615</link>
      <description>&lt;P&gt;Try this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
retain count 99 value_lagged;
output; * this output is necessary because we don't want value_lagged set immediately;
if condition then do;
  count = 0;
  value_lagged = value;
end;
count + 1;
if count &amp;gt; 3 then value_lagged = 0;
drop count;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;&lt;FONT size="1 2 3 4 5 6 7"&gt;Edit: added drop statement.&lt;/FONT&gt;&lt;/EM&gt; &lt;/P&gt;</description>
      <pubDate>Thu, 26 Jul 2018 16:17:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Using-retain-to-preserve-a-value-for-subsequent-observations/m-p/481575#M71615</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-07-26T16:17:55Z</dc:date>
    </item>
    <item>
      <title>Re: Using retain to preserve a value for subsequent observations</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Using-retain-to-preserve-a-value-for-subsequent-observations/m-p/481756#M71620</link>
      <description>&lt;P&gt;This is a case where LAG functions embedded in an IFN function works neatly:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Date:mmddyy10. Condition Value;
format Date mmddyy10.;
datalines;
1/1/2005 1 5
1/2/2005 0 0
1/3/2005 0 0
1/4/2005 0 2
1/5/2005 0 3
1/6/2005 0 0
run;

data want;
  set have;
  value_lagged=ifn(lag(condition)=1 or lag2(condition)=1
                  ,max(lag(value),lag2(value))
                  ,0);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This program assumes that for all records in which condition is not a 1, value is a zero.&amp;nbsp; Otherwise the MAX function might choose the wrong lagged value.&lt;/P&gt;</description>
      <pubDate>Fri, 27 Jul 2018 04:11:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Using-retain-to-preserve-a-value-for-subsequent-observations/m-p/481756#M71620</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2018-07-27T04:11:41Z</dc:date>
    </item>
    <item>
      <title>Re: Using retain to preserve a value for subsequent observations</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Using-retain-to-preserve-a-value-for-subsequent-observations/m-p/481774#M71621</link>
      <description>&lt;P&gt;Or:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data WANT ;
  set HAVE;
  VALUE_LAGGED=ifn(lag1(CONDITION), lag1(VALUE)
              ,ifn(lag2(CONDITION), lag2(VALUE)
              ,                     0          ));
run ;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 27 Jul 2018 05:38:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Using-retain-to-preserve-a-value-for-subsequent-observations/m-p/481774#M71621</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2018-07-27T05:38:34Z</dc:date>
    </item>
    <item>
      <title>Re: Using retain to preserve a value for subsequent observations</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Using-retain-to-preserve-a-value-for-subsequent-observations/m-p/481897#M71627</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16961"&gt;@ChrisNZ&lt;/a&gt;'s is an improvement over my suggestion, in that it handles consecutive CONDITION=1 cases more appropriately.&amp;nbsp; Namely it takes the most recent VALUE of the two qualifying records, not the maximum valuje.&lt;/P&gt;</description>
      <pubDate>Fri, 27 Jul 2018 15:25:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Using-retain-to-preserve-a-value-for-subsequent-observations/m-p/481897#M71627</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2018-07-27T15:25:29Z</dc:date>
    </item>
    <item>
      <title>Re: Using retain to preserve a value for subsequent observations</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Using-retain-to-preserve-a-value-for-subsequent-observations/m-p/489712#M72078</link>
      <description>Sorry about the late response, but thanks! All responses were great but this was the simplest way to get at what I needed.</description>
      <pubDate>Fri, 24 Aug 2018 17:28:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Using-retain-to-preserve-a-value-for-subsequent-observations/m-p/489712#M72078</guid>
      <dc:creator>wwuuuww</dc:creator>
      <dc:date>2018-08-24T17:28:30Z</dc:date>
    </item>
  </channel>
</rss>

