<?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 function without getting rid of original observations in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Using-retain-function-without-getting-rid-of-original/m-p/424147#M104403</link>
    <description>&lt;P&gt;data have;&lt;BR /&gt;input enrolid year (enroll_start_dt enroll_end_dt) (:date9.);&amp;nbsp;&lt;BR /&gt;format enroll_start_dt enroll_end_dt date9.;&lt;BR /&gt;datalines;&lt;BR /&gt;1234 2010 01-JAN-2010 31-DEC-2010&lt;BR /&gt;1234 2011 01-JAN-2011 7-SEP-2011&lt;BR /&gt;5678 2010 01-JAN-2010 8-AUG-2010&lt;BR /&gt;4829 2010 01-JAN-2010 31-DEC-2010&lt;BR /&gt;4829 2011 01-JAN-2011 31-DEC-2011&lt;BR /&gt;4829 2012 01-JAN-2012 31-DEC-2012&lt;BR /&gt;4829 2013 01-JAN-2013 31-DEC-2013&lt;BR /&gt;;&lt;/P&gt;&lt;P&gt;data want;&lt;BR /&gt;do until(last.enrolid);&lt;BR /&gt;set have;&lt;BR /&gt;by enrolid notsorted;&lt;BR /&gt;if first.enrolid then overall_start =enroll_start_dt;&lt;BR /&gt;if last.enrolid then do;&lt;BR /&gt;overall_end =enroll_end_dt;&lt;BR /&gt;overall_days=intck('days', overall_start,overall_end);&lt;BR /&gt;end;&lt;BR /&gt;end;&lt;BR /&gt;do until(last.enrolid);&lt;BR /&gt;set have;&lt;BR /&gt;by enrolid notsorted;&lt;BR /&gt;output;&lt;BR /&gt;end;&lt;BR /&gt;format overall_start overall_end date9.;&lt;BR /&gt;run;&lt;/P&gt;</description>
    <pubDate>Sun, 31 Dec 2017 02:15:27 GMT</pubDate>
    <dc:creator>novinosrin</dc:creator>
    <dc:date>2017-12-31T02:15:27Z</dc:date>
    <item>
      <title>Using retain function without getting rid of original observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-retain-function-without-getting-rid-of-original/m-p/424144#M104402</link>
      <description>&lt;P&gt;HI all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I currently have a dataset that has&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have; 
input enrolid year enroll_start_dt enroll_end_dt; /* Don't know how to format the dates -- any advice?? */

datalines;
1234 2010 01-JAN-2010 31-DEC-2010 
1234 2011 01-JAN-2011 7-SEP-2011
5678 2010 01-JAN-2010 8-AUG-2010
4829 2010 01-JAN-2010 31-DEC-2010
4829 2011 01-JAN-2011 31-DEC-2011
4829 2012 01-JAN-2012 31-DEC-2012
4829 2013 01-JAN-2013 31-DEC-2013
;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And I want to get this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want; 
input enrolid year enroll_start_dt enroll_end_dt overall_start overall_end overall_days;
datalines;
1234 2010 01-JAN-2010 31-DEC-2010 01-JAN-2010 7-SEP-2011 614
1234 2011 01-JAN-2011 7-SEP-2011 01-JAN-2010 7-SEP-2011 614
5678 2010 01-JAN-2010 8-AUG-2010 01-JAN-2010 8-AUG-2010 219
4829 2010 01-JAN-2010 31-DEC-2010 01-JAN-2010 31-DEC-2013 1095
4829 2011 01-JAN-2011 31-DEC-2011 01-JAN-2010 31-DEC-2013 1095 
4829 2012 01-JAN-2012 31-DEC-2012 01-JAN-2010 31-DEC-2013 1095
4829 2013 01-JAN-2013 31-DEC-2013 01-JAN-2010 31-DEC-2013 1095
;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;But with the current code I'm running I'm getting something funky ... that looks almost like the above but I am not able to retain all of the original observations corresponding to each unique enrolid - enroll_start_dt - enroll_end_dt combination...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My current code is as follows:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
			set have;
			by enrolid enroll_start_dt enroll_end_dt;
			retain enroll_start_dt enroll_end_dt overall_start overall_end overall_days;
			format overall_start overall_end date11.;
			if first.enrolid and first.enroll_start_dt and first.enroll_end_dt then do;
				overall_start=enroll_start_dt;
				overall_end=enroll_end_dt;
				overall_days=overall_end-overall_start;
			end;
			
				else if enroll_start_dt&amp;gt;overall_end+1 then do;
				output;
				overall_start=enroll_start_dt;
				overall_end=enroll_end_dt;
				overall_days=overall_end-overall_start;
			end;&lt;BR /&gt;&lt;BR /&gt;else if enroll_start_dt&amp;lt;=overall_end+1 then do;&lt;BR /&gt; overall_start=min(overall_start,enroll_start_dt);&lt;BR /&gt; overall_end = max(overall_end, enroll_end_dt);&lt;BR /&gt; overall_days=overall_end-overall_start;&lt;BR /&gt; end;
			if last.enrolid then output;
		run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 31 Dec 2017 02:00:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-retain-function-without-getting-rid-of-original/m-p/424144#M104402</guid>
      <dc:creator>cdubs</dc:creator>
      <dc:date>2017-12-31T02:00:25Z</dc:date>
    </item>
    <item>
      <title>Re: Using retain function without getting rid of original observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-retain-function-without-getting-rid-of-original/m-p/424147#M104403</link>
      <description>&lt;P&gt;data have;&lt;BR /&gt;input enrolid year (enroll_start_dt enroll_end_dt) (:date9.);&amp;nbsp;&lt;BR /&gt;format enroll_start_dt enroll_end_dt date9.;&lt;BR /&gt;datalines;&lt;BR /&gt;1234 2010 01-JAN-2010 31-DEC-2010&lt;BR /&gt;1234 2011 01-JAN-2011 7-SEP-2011&lt;BR /&gt;5678 2010 01-JAN-2010 8-AUG-2010&lt;BR /&gt;4829 2010 01-JAN-2010 31-DEC-2010&lt;BR /&gt;4829 2011 01-JAN-2011 31-DEC-2011&lt;BR /&gt;4829 2012 01-JAN-2012 31-DEC-2012&lt;BR /&gt;4829 2013 01-JAN-2013 31-DEC-2013&lt;BR /&gt;;&lt;/P&gt;&lt;P&gt;data want;&lt;BR /&gt;do until(last.enrolid);&lt;BR /&gt;set have;&lt;BR /&gt;by enrolid notsorted;&lt;BR /&gt;if first.enrolid then overall_start =enroll_start_dt;&lt;BR /&gt;if last.enrolid then do;&lt;BR /&gt;overall_end =enroll_end_dt;&lt;BR /&gt;overall_days=intck('days', overall_start,overall_end);&lt;BR /&gt;end;&lt;BR /&gt;end;&lt;BR /&gt;do until(last.enrolid);&lt;BR /&gt;set have;&lt;BR /&gt;by enrolid notsorted;&lt;BR /&gt;output;&lt;BR /&gt;end;&lt;BR /&gt;format overall_start overall_end date9.;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Sun, 31 Dec 2017 02:15:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-retain-function-without-getting-rid-of-original/m-p/424147#M104403</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2017-12-31T02:15:27Z</dc:date>
    </item>
    <item>
      <title>Re: Using retain function without getting rid of original observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-retain-function-without-getting-rid-of-original/m-p/424150#M104405</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/174079"&gt;@cdubs&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;Here a solution approach using SAS SQL flavor:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input enrolid year (enroll_start_dt enroll_end_dt) (:date11.); 
  format enroll_start_dt enroll_end_dt date11.;
  datalines;
1234 2010 01-JAN-2010 31-DEC-2010 
1234 2011 01-JAN-2011 7-SEP-2011
5678 2010 01-JAN-2010 8-AUG-2010
4829 2010 01-JAN-2010 31-DEC-2010
4829 2011 01-JAN-2011 31-DEC-2011
4829 2012 01-JAN-2012 31-DEC-2012
4829 2013 01-JAN-2013 31-DEC-2013
;
run;

proc sql;
  create table want as
    select 
      *
      ,min(enroll_start_dt) as overall_start format=date11.
      ,max(enroll_end_dt)   as overall_end   format=date11.
      ,(calculated overall_end - calculated overall_start) as overall_days
    from 
      have
    group by enrolid
  ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 31 Dec 2017 02:40:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-retain-function-without-getting-rid-of-original/m-p/424150#M104405</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2017-12-31T02:40:21Z</dc:date>
    </item>
  </channel>
</rss>

