<?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: given 2 dates in consecutive rows, I need to expand dataset so that each row is a daily incremen in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/given-2-dates-in-consecutive-rows-I-need-to-expand-dataset-so/m-p/344422#M79150</link>
    <description>This helps a lot! Thank you!</description>
    <pubDate>Sun, 26 Mar 2017 16:28:04 GMT</pubDate>
    <dc:creator>Fettah</dc:creator>
    <dc:date>2017-03-26T16:28:04Z</dc:date>
    <item>
      <title>given 2 dates in consecutive rows, I need to expand dataset so that each row is a daily increment</title>
      <link>https://communities.sas.com/t5/SAS-Programming/given-2-dates-in-consecutive-rows-I-need-to-expand-dataset-so/m-p/344347#M79123</link>
      <description>&lt;P&gt;I have a dataset that looks like this:&lt;BR /&gt; &lt;/P&gt;
&lt;PRE&gt;ID Date       Status
1  1/1/2010   A
1  2/14/2010  B
1  3/1/2010   .
2  1/11/2010  A
2  1/20/2010  .&lt;/PRE&gt;
&lt;P&gt;I need to a dataset where observations are daily increments of the 2 dates, and I want to keep the same 'status' until the next date.&lt;/P&gt;
&lt;P&gt;Essentially, I would like my final dataset to somehow look like this:&lt;/P&gt;
&lt;PRE&gt;ID Date      Status
1  1/1/2010  A
1  1/2/2010  A
1  1/3/2010  A
1  ...       A
1  2/13/2010 A
1  2/14/2010 B
1  2/15/2010 B
1  ...       B
1  3/1/2010  B
2  1/11/2010 A
2  1/12/2010 A
2  ...       A
3  1/20/2010 A&lt;/PRE&gt;
&lt;P&gt;This seems to be&amp;nbsp;a simple enough problem, but I can't seem to find the most efficient way to do this.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks for your help!&lt;/P&gt;</description>
      <pubDate>Sat, 25 Mar 2017 22:03:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/given-2-dates-in-consecutive-rows-I-need-to-expand-dataset-so/m-p/344347#M79123</guid>
      <dc:creator>Fettah</dc:creator>
      <dc:date>2017-03-25T22:03:27Z</dc:date>
    </item>
    <item>
      <title>Re: given 2 dates in consecutive rows, I need to expand dataset so that each row is a daily incremen</title>
      <link>https://communities.sas.com/t5/SAS-Programming/given-2-dates-in-consecutive-rows-I-need-to-expand-dataset-so/m-p/344355#M79127</link>
      <description>&lt;P&gt;I think that the following does what you want to accomplish:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data have;
  informat date mmddyy10.;
  format date mmddyy10.;
  input ID Date       Status $;
  cards;
1  1/1/2010   A
1  2/14/2010  B
1  3/1/2010   .
2  1/11/2010  A
2  1/20/2010  .
;

data want (drop=n_:);
  merge have have (firstobs=2 rename=(ID=n_ID date=n_date status=n_status));
  if not missing(status) then output;
  if ID eq n_ID then do;
    if missing(n_status) then n_end=n_date;
    else n_end=n_date-1;
    do date=date+1 to n_end;
      output;
    end;
  end;
run;
&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 26 Mar 2017 00:07:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/given-2-dates-in-consecutive-rows-I-need-to-expand-dataset-so/m-p/344355#M79127</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-03-26T00:07:06Z</dc:date>
    </item>
    <item>
      <title>Re: given 2 dates in consecutive rows, I need to expand dataset so that each row is a daily incremen</title>
      <link>https://communities.sas.com/t5/SAS-Programming/given-2-dates-in-consecutive-rows-I-need-to-expand-dataset-so/m-p/344357#M79129</link>
      <description>&lt;P&gt;Some thoughts on a solution.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Putting the dates in macro variables then subtracting them for each interval may work.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your dataset is call 'have'&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Data _null_;&lt;/P&gt;
&lt;P&gt;set have end=no_more;&lt;/P&gt;
&lt;P&gt;call symputx('Date'||left(_n_),date);&lt;/P&gt;
&lt;P&gt;If no_more then call symputx('num_records',(_n_));&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;now you dates in macro variables date1 date2 etc..&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Run a PROC FREQ on the variable ID to get the size of a do loop inside a datastep to make new records. Loop over all unique IDs&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc freq data=have;&lt;/P&gt;
&lt;P&gt;var ID / out=distinctID;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;now you you have a dataset distinctID&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data _null_;&lt;/P&gt;
&lt;P&gt;set distinctID end=no_more;&lt;/P&gt;
&lt;P&gt;call symputx ('ID'||left(_n_),ID);&lt;/P&gt;
&lt;P&gt;call symputx('CounID'||left(_n_),count);&lt;/P&gt;
&lt;P&gt;if no_more then call symputx('num_distinctIDs',(_n_));&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 26 Mar 2017 00:33:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/given-2-dates-in-consecutive-rows-I-need-to-expand-dataset-so/m-p/344357#M79129</guid>
      <dc:creator>ptimusk</dc:creator>
      <dc:date>2017-03-26T00:33:56Z</dc:date>
    </item>
    <item>
      <title>Re: given 2 dates in consecutive rows, I need to expand dataset so that each row is a daily incremen</title>
      <link>https://communities.sas.com/t5/SAS-Programming/given-2-dates-in-consecutive-rows-I-need-to-expand-dataset-so/m-p/344379#M79134</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  informat date mmddyy10.;
  format date mmddyy10.;
  input ID Date       Status $;
  cards;
1  1/1/2010   A
1  2/14/2010  B
1  3/1/2010   .
2  1/11/2010  A
2  1/20/2010  .
;
run;

data temp;
 set have;
 by id;
 length _status $ 40;
 retain _status;
 if first.id then call missing(_status);
 if not missing(status) then _status=status;
 drop status;
run;

data want;
 merge temp temp(keep=id date rename=(id=_id date=_date) firstobs=2);
 output;
 if id=_id then do;
   do i=date+1 to _date-1;
    date=i;output;
   end;
 end;
drop _id _date i;
run;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 26 Mar 2017 05:01:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/given-2-dates-in-consecutive-rows-I-need-to-expand-dataset-so/m-p/344379#M79134</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2017-03-26T05:01:36Z</dc:date>
    </item>
    <item>
      <title>Re: given 2 dates in consecutive rows, I need to expand dataset so that each row is a daily incremen</title>
      <link>https://communities.sas.com/t5/SAS-Programming/given-2-dates-in-consecutive-rows-I-need-to-expand-dataset-so/m-p/344397#M79142</link>
      <description>&lt;P&gt;Hi Fettah,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;An alternative solution to this might be one that does not use the DATA step MERGE.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The code below might be useful.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data Have;
Input ID
      Date  MMDDYY10.
  @15 Status $1.
      ;
Format  Date Date9.;
DATALINES;
1  1/1/2010   A
1  2/14/2010  B
1  3/1/2010   .
2  1/11/2010  A
2  1/20/2010  .
;
Run;

**************************************************************;
*Make sure your rows are in the correct order.                ;
**************************************************************;
Proc Sort Data=Have;
 By ID
    Date
    ;
Run;

**************************************************************;
*Now get the dataset that we want.......                      ;
**************************************************************;
Data Want;
 ********************************************************************************************;
 *Use a NULL Informat statement to force the order of our columns just to make reading easier;
 ********************************************************************************************;
 Informat ID
          Date
          Status
          ;
 ******************************************************************************;
 *Set these two variables to hold their values between passes of the DATA step.;
 ******************************************************************************;
 Retain prevDate   .
        prevStatus ""
        ;
 *******************************************************************************;
 *Go and get our dataset and RENAME our two variables that we want to manipulate;
 *******************************************************************************;
 Set Have(Rename=(Date=xDate Status=xStatus));
  By ID
     xDate
     ;

********************************************************************************;
*If this is anything BUT the first row for a group of rows sharing the same ID: ;
*1) Trap the Date value held over from the previous row as our StartDate.       ;
*2.1) If this is the final row in our group of rows with same ID, trap its Date ;
*     value as our StopDate.                                                    ;
*2.2) If this is NOT the final row in our group of rows with the same ID,       ;
*     subtract 1 day from its Date value and use this as our StopDate.          ;
********************************************************************************;
If NOT First.ID Then
    Do;
        StartDate=prevDate;
        If Last.ID Then
            Do;
                StopDate=xDate;
            End;
            Else
            Do;
                StopDate=xDate-1;
            End;
        ********************************************************************************;
        *3) Loop over StartDate to StopDate and Output one row per pass of the loop.    ;
        *   We will use the value of Status held over from the previous row.            ;
        ********************************************************************************;
        Status=prevStatus;
        Do Date=StartDate To StopDate;
            Output;
        End;

    End;

********************************************************************************************;
*Set aside the Date and Status values from this row ready for the next pass of the DATA step;
********************************************************************************************;
prevDate=xDate;
prevStatus=xStatus;


**************************************************************;
*Tidy up the dataset.......                                   ;
**************************************************************;
Format  prevDate Date xDate Date9.;

Drop prevDate
     prevStatus
     StartDate
     StopDate
     xDate
     xStatus
     ;

Run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I hope that this helps more than it hinders.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Cheers,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Downunder Dave.&lt;/P&gt;</description>
      <pubDate>Sun, 26 Mar 2017 09:00:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/given-2-dates-in-consecutive-rows-I-need-to-expand-dataset-so/m-p/344397#M79142</guid>
      <dc:creator>DaveShea</dc:creator>
      <dc:date>2017-03-26T09:00:39Z</dc:date>
    </item>
    <item>
      <title>Re: given 2 dates in consecutive rows, I need to expand dataset so that each row is a daily incremen</title>
      <link>https://communities.sas.com/t5/SAS-Programming/given-2-dates-in-consecutive-rows-I-need-to-expand-dataset-so/m-p/344414#M79148</link>
      <description>&lt;P&gt;Assuming your date is sorted by ID and date:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have end=done;
  by ID;
  if not done then
    set have(keep=date rename=(date=next_date) firstobs=2);
  if not missing(status);
  output;
  if not last.ID then do date=date+1 to next_date-1;
    output;
    end;
  drop next_date;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 26 Mar 2017 14:43:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/given-2-dates-in-consecutive-rows-I-need-to-expand-dataset-so/m-p/344414#M79148</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2017-03-26T14:43:13Z</dc:date>
    </item>
    <item>
      <title>Re: given 2 dates in consecutive rows, I need to expand dataset so that each row is a daily incremen</title>
      <link>https://communities.sas.com/t5/SAS-Programming/given-2-dates-in-consecutive-rows-I-need-to-expand-dataset-so/m-p/344422#M79150</link>
      <description>This helps a lot! Thank you!</description>
      <pubDate>Sun, 26 Mar 2017 16:28:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/given-2-dates-in-consecutive-rows-I-need-to-expand-dataset-so/m-p/344422#M79150</guid>
      <dc:creator>Fettah</dc:creator>
      <dc:date>2017-03-26T16:28:04Z</dc:date>
    </item>
    <item>
      <title>Re: given 2 dates in consecutive rows, I need to expand dataset so that each row is a daily incremen</title>
      <link>https://communities.sas.com/t5/SAS-Programming/given-2-dates-in-consecutive-rows-I-need-to-expand-dataset-so/m-p/344423#M79151</link>
      <description>Thank you, Art. I wish I could choose multiple replies as the "solution" because this one also works like a charm.</description>
      <pubDate>Sun, 26 Mar 2017 16:29:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/given-2-dates-in-consecutive-rows-I-need-to-expand-dataset-so/m-p/344423#M79151</guid>
      <dc:creator>Fettah</dc:creator>
      <dc:date>2017-03-26T16:29:08Z</dc:date>
    </item>
    <item>
      <title>Re: given 2 dates in consecutive rows, I need to expand dataset so that each row is a daily incremen</title>
      <link>https://communities.sas.com/t5/SAS-Programming/given-2-dates-in-consecutive-rows-I-need-to-expand-dataset-so/m-p/344424#M79152</link>
      <description>&lt;P&gt;I suggest you compare&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/76464"&gt;@s_lassen&lt;/a&gt;'s results with those from the code I suggested. His leaves off the last record, while mine includes it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 26 Mar 2017 16:32:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/given-2-dates-in-consecutive-rows-I-need-to-expand-dataset-so/m-p/344424#M79152</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-03-26T16:32:49Z</dc:date>
    </item>
    <item>
      <title>Re: given 2 dates in consecutive rows, I need to expand dataset so that each row is a daily incremen</title>
      <link>https://communities.sas.com/t5/SAS-Programming/given-2-dates-in-consecutive-rows-I-need-to-expand-dataset-so/m-p/344463#M79158</link>
      <description>You're right Art. After applying the codes yours is the more accurate solution. Thanks for pointing it out.</description>
      <pubDate>Sun, 26 Mar 2017 22:51:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/given-2-dates-in-consecutive-rows-I-need-to-expand-dataset-so/m-p/344463#M79158</guid>
      <dc:creator>Fettah</dc:creator>
      <dc:date>2017-03-26T22:51:26Z</dc:date>
    </item>
  </channel>
</rss>

