<?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: Add rows with missing dates range and retain data from previous non missing row per customer in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Add-rows-with-missing-dates-range-and-retain-data-from-previous/m-p/909664#M358782</link>
    <description>&lt;P&gt;If licensed then I'd be using Proc Expand as already proposed. Else here another code variant to create the additional rows.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  format date date9.;
  input ID date :date9. Y;
  cards;
1 01DEC2023 10
1 04DEC2023 20
1 05DEC2023 30
2 02DEC2023 40
2 04DEC2023 50
2 05DEC2023 60
;

data want;
  set have;
  by id date;
  output;

  if not last.id then
    do;
      /* read ahead: If next date not date+1 create additional rows */
      i=_n_+1;
      set have(keep=date rename=(date=_date)) point=i;
      do i= 1 to (_date-date-1);
        date+1;
        output;
      end;
    end;
  drop _date;
run;

proc print data=want;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1703550524415.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/92069i640EF1E38EDB711E/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_0-1703550524415.png" alt="Patrick_0-1703550524415.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 26 Dec 2023 00:29:43 GMT</pubDate>
    <dc:creator>Patrick</dc:creator>
    <dc:date>2023-12-26T00:29:43Z</dc:date>
    <item>
      <title>Add rows with missing dates range and retain data from previous non missing row per customer</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-rows-with-missing-dates-range-and-retain-data-from-previous/m-p/909632#M358761</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;I have a data set with multiple rows per customer Id and each row related to specific date.&lt;/P&gt;
&lt;P&gt;I would like to perform 2 tasks:&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;U&gt;Task1:&lt;/U&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;Add rows for dates that are not existing.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;U&gt;Task2:&lt;/U&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;For each customer need to fill data of empty rows (except of date) with data from previous non-missing row.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Ronein_0-1703503604124.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/92063iA38E47731A90417A/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Ronein_0-1703503604124.png" alt="Ronein_0-1703503604124.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data tbl1;
format date ddmmyy10.;
input ID date :date9. Y;
cards;
1 01DEC2023 10
1 04DEC2023 20
1 05DEC2023 30
2 02DEC2023 40
2 04DEC2023 50
2 05DEC2023 60
;
Run;
 
proc sql  noprint;
select  min(date) as FROMDate  into :FROMDate
from tbl1
;
quit;

proc sql  noprint;
select  max(date) as TillDate  into :TillDate
from tbl1
;
quit;


data dates(Keep=date) ;
date=&amp;amp;FROMDate.;
end_date=&amp;amp;TillDate.;
format date  end_date date9.;
do while (date&amp;lt;=end_date);
output;
date=intnx('day', date, 1, 's');
end;
format date ddmmyy10.;
run;
&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;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 25 Dec 2023 11:28:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-rows-with-missing-dates-range-and-retain-data-from-previous/m-p/909632#M358761</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2023-12-25T11:28:10Z</dc:date>
    </item>
    <item>
      <title>Re: Add rows with missing dates range and retain data from previous non missing row per customer</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-rows-with-missing-dates-range-and-retain-data-from-previous/m-p/909633#M358762</link>
      <description>&lt;P&gt;First, I don't see a reason to use macro variables or SQL here. Neither are good solutions for the case where you have to do things row-by-row to get the desired answer. The solution is to write a DATA step, and check for missing dates and react accordingly by looping through the missing dates.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is my solution:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data tbl1;
format date ddmmyy10.;
input ID date :date9. Y;
cards;
1 01DEC2023 10
1 04DEC2023 20
1 05DEC2023 30
2 02DEC2023 40
2 04DEC2023 50
2 05DEC2023 60
;
data want;
    set tbl1;
    previd=lag(id);
    prevdate=lag(date);
    prevy=lag(y);
    /* Check for missing dates */
    if prevdate^=(date-1) and id=previd then do;
        /* Loop over missing dates */
        do iter=1 to (date-prevdate);
        	newdate=prevdate+iter;
        	if newdate&amp;lt;date then newy=prevy;
        	else newy=y;
        	output;
    	end;
	end;
	else do; /* No missing dates found */
	    newdate=date;
	    newy=y;
	    output;
    end;
    format newdate date9.;
    drop prev: iter;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 25 Dec 2023 12:07:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-rows-with-missing-dates-range-and-retain-data-from-previous/m-p/909633#M358762</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-12-25T12:07:06Z</dc:date>
    </item>
    <item>
      <title>Re: Add rows with missing dates range and retain data from previous non missing row per customer</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-rows-with-missing-dates-range-and-retain-data-from-previous/m-p/909644#M358768</link>
      <description>&lt;P&gt;If you have SAS/ETS in your license key.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data tbl1;
format date ddmmyy10.;
input ID date :date9. Y;
cards;
1 01DEC2023 10
1 04DEC2023 20
1 05DEC2023 30
2 02DEC2023 40
2 04DEC2023 50
2 05DEC2023 60
;
Run;

proc expand data=tbl1
            out=tbl2
            to=day method=step
            plots=(input output);
  by ID;
  id date;
run;
/* end of program */&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Koen&lt;/P&gt;</description>
      <pubDate>Mon, 25 Dec 2023 15:09:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-rows-with-missing-dates-range-and-retain-data-from-previous/m-p/909644#M358768</guid>
      <dc:creator>sbxkoenk</dc:creator>
      <dc:date>2023-12-25T15:09:22Z</dc:date>
    </item>
    <item>
      <title>Re: Add rows with missing dates range and retain data from previous non missing row per customer</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-rows-with-missing-dates-range-and-retain-data-from-previous/m-p/909652#M358774</link>
      <description>&lt;P&gt;Just build up a skeleton or empty dataset with just the ID and DATE values for all of the days you want.&amp;nbsp; (No need to get fancy to increment dates by days. Simple arithmetic will work since dates are stored as number of days.)&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data empty;
  set have;
  by id;
  if first.id then start=date;
  retain start;
  if last.id;
  end=date;
  do date=start to end;
    output;
  end;
  keep id date;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then you can combine them and use some simple last observation carried forward logic to replace the missing values.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  merge empty have;
  by id date;
  if not first.id then y=coalesce(y,oldy);
  oldy=y;
  retain oldy;
  drop oldy;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;OBS    ID          date     Y

 1      1    2023-12-01    10
 2      1    2023-12-02    10
 3      1    2023-12-03    10
 4      1    2023-12-04    20
 5      1    2023-12-05    30
 6      2    2023-12-02    40
 7      2    2023-12-03    40
 8      2    2023-12-04    50
 9      2    2023-12-05    60
&lt;/PRE&gt;
&lt;P&gt;PS&amp;nbsp; Don't display dates in either DMY or MDY order.&amp;nbsp; It took me a couple of seconds to figure out why you would want to insert dates in the middle of the months of Feb and March.&lt;/P&gt;</description>
      <pubDate>Mon, 25 Dec 2023 16:18:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-rows-with-missing-dates-range-and-retain-data-from-previous/m-p/909652#M358774</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-12-25T16:18:59Z</dc:date>
    </item>
    <item>
      <title>Re: Add rows with missing dates range and retain data from previous non missing row per customer</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-rows-with-missing-dates-range-and-retain-data-from-previous/m-p/909664#M358782</link>
      <description>&lt;P&gt;If licensed then I'd be using Proc Expand as already proposed. Else here another code variant to create the additional rows.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  format date date9.;
  input ID date :date9. Y;
  cards;
1 01DEC2023 10
1 04DEC2023 20
1 05DEC2023 30
2 02DEC2023 40
2 04DEC2023 50
2 05DEC2023 60
;

data want;
  set have;
  by id date;
  output;

  if not last.id then
    do;
      /* read ahead: If next date not date+1 create additional rows */
      i=_n_+1;
      set have(keep=date rename=(date=_date)) point=i;
      do i= 1 to (_date-date-1);
        date+1;
        output;
      end;
    end;
  drop _date;
run;

proc print data=want;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1703550524415.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/92069i640EF1E38EDB711E/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_0-1703550524415.png" alt="Patrick_0-1703550524415.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 26 Dec 2023 00:29:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-rows-with-missing-dates-range-and-retain-data-from-previous/m-p/909664#M358782</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-12-26T00:29:43Z</dc:date>
    </item>
    <item>
      <title>Re: Add rows with missing dates range and retain data from previous non missing row per customer</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-rows-with-missing-dates-range-and-retain-data-from-previous/m-p/909666#M358784</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data tbl1;
format date ddmmyy10.;
input ID date :date9. Y;
cards;
1 01DEC2023 10
1 04DEC2023 20
1 05DEC2023 30
2 02DEC2023 40
2 04DEC2023 50
2 05DEC2023 60
;
Run;

data want;
 merge tbl1 tbl1(firstobs=2 keep=id date rename=(id=_id date=_date));
 output;
 if id=_id then do;
  do date=date+1 to _date-1;
    output;
  end;
 end;
drop _id _date;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 26 Dec 2023 01:37:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-rows-with-missing-dates-range-and-retain-data-from-previous/m-p/909666#M358784</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2023-12-26T01:37:58Z</dc:date>
    </item>
    <item>
      <title>Re: Add rows with missing dates range and retain data from previous non missing row per customer</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-rows-with-missing-dates-range-and-retain-data-from-previous/m-p/909668#M358786</link>
      <description>&lt;P&gt;It looks to me like your code (unlike your sample desired output) intends not only to carry-forward Y values through every missing date up through global maximum &amp;amp;TILLDATE, but also your generation of global &amp;amp;FROMDATE implies you may want to create observations with missing values of Y from &amp;amp;FROMDATE up to the date before the first observed date.&amp;nbsp; &amp;nbsp; Otherwise there is no need for &amp;amp;FROMDATE.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you do NOT need to create dates back to &amp;amp;FROMDATE, then:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
proc sql noprint;
  select max(date) into :max_date from tbl1;
quit;

data want;
  set tbl1 (keep=id);
  by id;
  merge tbl1
        tbl1 (firstobs=2 keep=date rename=(date=_nxt_date));

  if last.id then _nxt_date=&amp;amp;max_date+1;
  do date=date to _nxt_date-1;
    output;
  end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But if you do, then you need an extra conditional process for first.id,:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
  select min(date), max(date) into :min_date,:max_date
  from tbl1;
quit;

data want  (drop=_:);
  set tbl1 (keep=id date rename=(date=_date));
  by id;
  if first.id=1 and _date&amp;gt;&amp;amp;min_date then do date= &amp;amp;min_date to _date-1;
    output;
  end;

  merge tbl1
        tbl1 (firstobs=2 keep=date rename=(date=_nxt_date));
  if last.id=1 then _nxt_date=&amp;amp;max_date+1;

  do date=date to _nxt_date-1;
    output;
  end;
  call missing(of _all_);
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 26 Dec 2023 02:43:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-rows-with-missing-dates-range-and-retain-data-from-previous/m-p/909668#M358786</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2023-12-26T02:43:54Z</dc:date>
    </item>
  </channel>
</rss>

