<?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: Total length of stay with conditions in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Total-length-of-stay-with-conditions/m-p/754147#M237766</link>
    <description>&lt;P&gt;You may need to modify the final subsetting IF.&lt;/P&gt;
&lt;P&gt;data sample1;&lt;BR /&gt;set sample;&lt;BR /&gt;by id discharge;&lt;/P&gt;
&lt;P&gt;if first.id then&lt;BR /&gt;cum_los = 0;&lt;BR /&gt;cum_los + los;&lt;/P&gt;
&lt;P&gt;if event = "discharge1" or event = "death" or discharge = "20sep2015"d;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 14 Jul 2021 18:19:58 GMT</pubDate>
    <dc:creator>JOL</dc:creator>
    <dc:date>2021-07-14T18:19:58Z</dc:date>
    <item>
      <title>Total length of stay with conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Total-length-of-stay-with-conditions/m-p/754140#M237763</link>
      <description>&lt;P&gt;I have the following data:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data sample;
	input id $ (entry) (:mmddyy10.) (discharge) (:mmddyy10.) event $ 10.;
	format entry discharge date9.;
	If discharge lt "30sep2015"d then los=discharge-entry;
	Else los="30sep2015"d-entry;
	datalines;
1 12/05/2014 12/17/2014 discharge2
1 12/24/2014 01/17/2015 discharge2
1 02/06/2015 03/10/2015 discharge1

2 08/01/2013 08/16/2013 discharge1
2 10/24/2013 11/06/2013 discharge1

3 02/10/2015 05/10/2015 discharge1
3 09/10/2015 10/18/2015 discharge2

4 09/23/2013 10/22/2013 discharge2
4 10/30/2013 07/15/2014 discharge2
4 07/25/2014 08/14/2014 death

5 08/01/2015 08/16/2015 discharge2
5 09/01/2015 11/06/2015 discharge1

6 11/05/2013 09/05/2014 discharge2
6 09/15/2014 07/08/2015 discharge2
6 07/14/2015 07/16/2015 discharge2
;
Run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Doyinsola_0-1627575554787.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/62083iA4756B516B1E4D5C/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Doyinsola_0-1627575554787.png" alt="Doyinsola_0-1627575554787.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Each row represents a period of stay that starts with an entry date and a discharge date. The "los" variable represents the length of stay (i.e. the number of days from entry to discharge). The "event" variable describes the type of discharge (discharge1=return not anticipated, discharge2=return anticipated, death).&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;I would like to find the cumulative length of stay (los) by id, where counting stops if the event is discharge1 or death, whichever comes first, or the last of a series of discharge2 dates. So the dataset starts to look like this:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Doyinsola_1-1627575749395.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/62084i3399B19EA1335A88/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Doyinsola_1-1627575749395.png" alt="Doyinsola_1-1627575749395.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="font-family: inherit;"&gt;And ultimately, this is the dataset I am hoping to end up with:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Doyinsola_2-1626284339630.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/61260i3C262B3E9BB3BE9C/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Doyinsola_2-1626284339630.png" alt="Doyinsola_2-1626284339630.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 29 Jul 2021 16:23:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Total-length-of-stay-with-conditions/m-p/754140#M237763</guid>
      <dc:creator>Doyinsola</dc:creator>
      <dc:date>2021-07-29T16:23:22Z</dc:date>
    </item>
    <item>
      <title>Re: Total length of stay with conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Total-length-of-stay-with-conditions/m-p/754147#M237766</link>
      <description>&lt;P&gt;You may need to modify the final subsetting IF.&lt;/P&gt;
&lt;P&gt;data sample1;&lt;BR /&gt;set sample;&lt;BR /&gt;by id discharge;&lt;/P&gt;
&lt;P&gt;if first.id then&lt;BR /&gt;cum_los = 0;&lt;BR /&gt;cum_los + los;&lt;/P&gt;
&lt;P&gt;if event = "discharge1" or event = "death" or discharge = "20sep2015"d;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 14 Jul 2021 18:19:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Total-length-of-stay-with-conditions/m-p/754147#M237766</guid>
      <dc:creator>JOL</dc:creator>
      <dc:date>2021-07-14T18:19:58Z</dc:date>
    </item>
    <item>
      <title>Re: Total length of stay with conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Total-length-of-stay-with-conditions/m-p/754162#M237770</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/120813"&gt;@Doyinsola&lt;/a&gt;&amp;nbsp;What are you doing with ID 2? Is the desired output supposed to be 1 row per ID? I ran&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/151645"&gt;@JOL&lt;/a&gt;'s&amp;nbsp;code but it gets 2 rows for ID 2 as that ID has 2 rows for discharge1. What is your requirement? Do you want to keep the 15 or the 13 for that ID or combine them to 28?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 14 Jul 2021 19:39:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Total-length-of-stay-with-conditions/m-p/754162#M237770</guid>
      <dc:creator>tarheel13</dc:creator>
      <dc:date>2021-07-14T19:39:49Z</dc:date>
    </item>
    <item>
      <title>Re: Total length of stay with conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Total-length-of-stay-with-conditions/m-p/754169#M237773</link>
      <description>Yes, the desired output should be one row per ID. For ID 2 I would like to keep only the first one (15). I also ran &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/151645"&gt;@JOL&lt;/a&gt;'s code, but it eliminates all rows of ID 6 and does not account for the the fact that ID 5 has some days stayed after 30th September 2015. ID 5 should have a final LOS of 29 instead of 81.&lt;BR /&gt;&lt;BR /&gt;Thank you</description>
      <pubDate>Wed, 14 Jul 2021 20:00:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Total-length-of-stay-with-conditions/m-p/754169#M237773</guid>
      <dc:creator>Doyinsola</dc:creator>
      <dc:date>2021-07-14T20:00:31Z</dc:date>
    </item>
    <item>
      <title>Re: Total length of stay with conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Total-length-of-stay-with-conditions/m-p/759606#M240086</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data sample1;
   Set sample;
   By id;

   If first.id then rn = 1; 
   Else rn + 1;
Run;

Proc SQL;
	Create table sample2 as
	Select ID, Entry, Discharge, Event, los, rn,
		Case 
			When Event = 'discharge1' then 'Stop'
        	When Event = 'death' then 'Stop'
        	When Discharge &amp;gt;=  "30sep2015"d then 'Stop'
        	Else '.' 
    	End as Include         
		From sample1;
Quit;

Proc SQL;
      Create table sample3 as
      Select ID, rn    
      From sample2
      Where Include = 'Stop';
Quit;

Proc SQL;
     Create table sample4 as
     Select sample2.ID, sum(sample2.los) as cum_los
     From sample2
     Left join sample3 on sample2.ID = sample3.ID
     Where sample2.rn &amp;lt;= sample3.rn or sample3.ID is null
     Group by sample2.ID;
Quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 05 Aug 2021 03:55:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Total-length-of-stay-with-conditions/m-p/759606#M240086</guid>
      <dc:creator>Doyinsola</dc:creator>
      <dc:date>2021-08-05T03:55:35Z</dc:date>
    </item>
  </channel>
</rss>

