<?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: Use Retain to correct values based on the previous record in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Use-Retain-to-correct-values-based-on-the-previous-record/m-p/338660#M77165</link>
    <description>&lt;P&gt;Better post the output too.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; 
data have; input PID 1 HospID $ 3 admission_year 5-8 discharge_year 10-13 reason_to_leave $ 15-21;
datalines;
1 A 2012 2013 tempout
1 A 2013 2014 moved
1 B 2015 2016 death
2 A 2012 2013 tempout
2 A 2013 2014 left
2 B 2015
3 A 2012 2013 death
3 A 2013 2014 moved
3 B 2015 2016 death
4 A 2012 2013 moved
4 B 2013 2014 death
4 B 2014 2015 tempout
4 B 2015 2016 death
5 A 2012 2014 tempout
5 A 2015 2016 death
5 B 2013 2014 moved
5 C 2015
;
run;
proc sort data=have;
 by pid descending discharge_year;
run;
data want;
 set have;
 by pid;
 retain found;
 if first.pid then found=0;
 if reason_to_leave='death' and found then reason_to_leave='DK';
 if reason_to_leave='death' then found=1;
 drop found;
run;
proc sort data=want;
 by pid discharge_year;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 07 Mar 2017 02:52:40 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2017-03-07T02:52:40Z</dc:date>
    <item>
      <title>Use Retain to correct values based on the previous record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-Retain-to-correct-values-based-on-the-previous-record/m-p/338648#M77161</link>
      <description>&lt;P&gt;I have vertical data recording a person 's in and out of hospital and reason for leaving.&amp;nbsp;I'm trying to fix people who have multiple deaths. The data is vertical: A person (PID) moved from one hospital to another (HospID), and within the same hospital, multiple records to show people's leaving and returning. See below for a sample of 5 cases. Also the actual data is date, not year, and there is no date comflict).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Naturally, &lt;BR /&gt;1. a person can only die once (so records for ID 3 &amp;amp; 4 are wrong) &lt;BR /&gt;2. if there is subsequent records after death, death should reset as unknown (DK). (so record for #5 is wrong).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So ID 1 and 2 are OK. The ^1st record of ID 3^, the ^2nd record of ID 4^ and the ^2nd record of ID 5^ should be set as DK.&lt;BR /&gt;I was thinking to use lag function but it doesn't do the work (or can be messy).&amp;nbsp;&lt;BR /&gt;It seems I should use retain to retain value from the previous record (aftert sorting the data and set discharge year missing to be say 2019)&amp;nbsp;and then reset the value as I move from one record to the next.&amp;nbsp;But how do I execute?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks in advance for any help you can give me.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have; input PID 1 HospID $ 3 admission_year 5-8 discharge_year 10-13 reason_to_leave $ 15-21;&lt;BR /&gt;datalines;&lt;BR /&gt;1 A 2012 2013 tempout&lt;BR /&gt;1 A 2013 2014 moved&lt;BR /&gt;1 B 2015 2016 death&lt;BR /&gt;2 A 2012 2013 tempout&lt;BR /&gt;2 A 2013 2014 left&lt;BR /&gt;2 B 2015&lt;BR /&gt;3 A 2012 2013 death&lt;BR /&gt;3 A 2013 2014 moved&lt;BR /&gt;3 B 2015 2016 death&lt;BR /&gt;4 A 2012 2013 moved&lt;BR /&gt;4 B 2013 2014 death&lt;BR /&gt;4 B 2014 2015 tempout&lt;BR /&gt;4 B 2015 2016 death&lt;BR /&gt;5 A 2012 2014 tempout&lt;BR /&gt;5 A 2015 2016 death&lt;BR /&gt;5 B 2013 2014 moved&lt;BR /&gt;5 C 2015&lt;BR /&gt;;&lt;BR /&gt;run;&lt;BR /&gt;proc print noobs; run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 07 Mar 2017 02:15:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-Retain-to-correct-values-based-on-the-previous-record/m-p/338648#M77161</guid>
      <dc:creator>Solph</dc:creator>
      <dc:date>2017-03-07T02:15:00Z</dc:date>
    </item>
    <item>
      <title>Re: Use Retain to correct values based on the previous record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-Retain-to-correct-values-based-on-the-previous-record/m-p/338653#M77163</link>
      <description>&lt;P&gt;Well, this does what you are asking for:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
by pid;
if not last.pid and reason_to_leave = 'death' then
   reason_to_leave = 'DK';
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Is it as simple as that?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 07 Mar 2017 02:30:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-Retain-to-correct-values-based-on-the-previous-record/m-p/338653#M77163</guid>
      <dc:creator>LaurieF</dc:creator>
      <dc:date>2017-03-07T02:30:31Z</dc:date>
    </item>
    <item>
      <title>Re: Use Retain to correct values based on the previous record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-Retain-to-correct-values-based-on-the-previous-record/m-p/338660#M77165</link>
      <description>&lt;P&gt;Better post the output too.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; 
data have; input PID 1 HospID $ 3 admission_year 5-8 discharge_year 10-13 reason_to_leave $ 15-21;
datalines;
1 A 2012 2013 tempout
1 A 2013 2014 moved
1 B 2015 2016 death
2 A 2012 2013 tempout
2 A 2013 2014 left
2 B 2015
3 A 2012 2013 death
3 A 2013 2014 moved
3 B 2015 2016 death
4 A 2012 2013 moved
4 B 2013 2014 death
4 B 2014 2015 tempout
4 B 2015 2016 death
5 A 2012 2014 tempout
5 A 2015 2016 death
5 B 2013 2014 moved
5 C 2015
;
run;
proc sort data=have;
 by pid descending discharge_year;
run;
data want;
 set have;
 by pid;
 retain found;
 if first.pid then found=0;
 if reason_to_leave='death' and found then reason_to_leave='DK';
 if reason_to_leave='death' then found=1;
 drop found;
run;
proc sort data=want;
 by pid discharge_year;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 07 Mar 2017 02:52:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-Retain-to-correct-values-based-on-the-previous-record/m-p/338660#M77165</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2017-03-07T02:52:40Z</dc:date>
    </item>
    <item>
      <title>Re: Use Retain to correct values based on the previous record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-Retain-to-correct-values-based-on-the-previous-record/m-p/338663#M77167</link>
      <description>&lt;P&gt;I'd approach the problem as follows:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data have;
  input PID 1 HospID $ 3 admission_year 5-8 discharge_year 10-13 reason_to_leave $ 15-21;
  datalines;
1 A 2012 2013 tempout
1 A 2013 2014 moved
1 B 2015 2016 death
2 A 2012 2013 tempout
2 A 2013 2014 left
2 B 2015 .    .
3 A 2012 2013 death
3 A 2013 2014 moved
3 B 2015 2016 death
4 A 2012 2013 moved
4 B 2013 2014 death
4 B 2014 2015 tempout
4 B 2015 2016 death
5 A 2012 2014 tempout
5 A 2015 2016 death
5 B 2013 2014 moved
5 C 2015 .    .
;
run;

proc sort data=have;
  by pid discharge_year;
run;

data want;
  do until (last.pid);
    set have;
    by pid;
    if reason_to_leave eq 'death' then latest_death=discharge_year;
  end;
  do until (last.pid);
    set have;
    by pid;
    if reason_to_leave eq 'death' and discharge_year ne latest_death then reason_to_leave='DK';
    output;
  end;
run;
&lt;/PRE&gt;
&lt;P&gt;HTH,&lt;/P&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 07 Mar 2017 02:56:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-Retain-to-correct-values-based-on-the-previous-record/m-p/338663#M77167</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-03-07T02:56:46Z</dc:date>
    </item>
    <item>
      <title>Re: Use Retain to correct values based on the previous record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-Retain-to-correct-values-based-on-the-previous-record/m-p/338686#M77182</link>
      <description>&lt;HR /&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/19901"&gt;@Solph&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;I have vertical data recording a person 's in and out of hospital and reason for leaving.&amp;nbsp;&lt;EM&gt;&lt;STRONG&gt;I&lt;EM&gt;'m trying to fix people who have multiple deaths&lt;/EM&gt;.&lt;/STRONG&gt;&lt;/EM&gt;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000" face="Times New Roman" size="3"&gt;and later wrote&lt;/FONT&gt;&lt;/P&gt;
&lt;HR /&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;Naturally, &lt;BR /&gt;1. a person can only die once (so records for ID 3 &amp;amp; 4 are wrong) &lt;BR /&gt;2. if there is subsequent records after death, death should reset as unknown (DK). (so record for #5 is wrong).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;FONT color="#000000" face="Times New Roman" size="3"&gt;Occurance of multiple deaths are just subsets of rule 2 above.&amp;nbsp; It seems to me that &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/17429"&gt;@LaurieF&lt;/a&gt;'s response fits the requirements exactly.&amp;nbsp; I understand the problem to mean that a death record is only acceptable as the final record. All other death records should be flagged.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000" face="Times New Roman" size="3"&gt;c&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000" face="Times New Roman" size="3"&gt;x&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 07 Mar 2017 06:19:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-Retain-to-correct-values-based-on-the-previous-record/m-p/338686#M77182</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-03-07T06:19:47Z</dc:date>
    </item>
    <item>
      <title>Re: Use Retain to correct values based on the previous record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-Retain-to-correct-values-based-on-the-previous-record/m-p/339082#M77334</link>
      <description>&lt;PRE&gt;
Or use DOW + index skill, something like Arthur did.


data have;
  input PID 1 HospID $ 3 admission_year 5-8 discharge_year 10-13 reason_to_leave $ 15-21;
  datalines;
1 A 2012 2013 tempout
1 A 2013 2014 moved
1 B 2015 2016 death
2 A 2012 2013 tempout
2 A 2013 2014 left
2 B 2015 .    .
3 A 2012 2013 death
3 A 2013 2014 moved
3 B 2015 2016 death
4 A 2012 2013 moved
4 B 2013 2014 death
4 B 2014 2015 tempout
4 B 2015 2016 death
5 A 2012 2014 tempout
5 A 2015 2016 death
5 B 2013 2014 moved
5 C 2015 .    .
;
run;


data want;
  do i=1 by 1 until (last.pid);
    set have;
    by pid;
    if reason_to_leave eq 'death' then n=i;
  end;
  do j=1 by 1 until (last.pid);
    set have;
    by pid;
    if reason_to_leave eq 'death' and j lt n then reason_to_leave='DK';
    output;
  end;
  drop i j n;
run;

&lt;/PRE&gt;</description>
      <pubDate>Wed, 08 Mar 2017 02:41:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-Retain-to-correct-values-based-on-the-previous-record/m-p/339082#M77334</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2017-03-08T02:41:47Z</dc:date>
    </item>
    <item>
      <title>Re: Use Retain to correct values based on the previous record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-Retain-to-correct-values-based-on-the-previous-record/m-p/339083#M77335</link>
      <description>&lt;P&gt;But - but - why make it so hard?&lt;/P&gt;</description>
      <pubDate>Wed, 08 Mar 2017 02:43:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-Retain-to-correct-values-based-on-the-previous-record/m-p/339083#M77335</guid>
      <dc:creator>LaurieF</dc:creator>
      <dc:date>2017-03-08T02:43:45Z</dc:date>
    </item>
    <item>
      <title>Re: Use Retain to correct values based on the previous record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-Retain-to-correct-values-based-on-the-previous-record/m-p/339087#M77338</link>
      <description>&lt;PRE&gt;

Maybe OP can clarify more about what he want.

PID=5 is different between these code.

&lt;/PRE&gt;</description>
      <pubDate>Wed, 08 Mar 2017 03:21:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-Retain-to-correct-values-based-on-the-previous-record/m-p/339087#M77338</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2017-03-08T03:21:21Z</dc:date>
    </item>
  </channel>
</rss>

