<?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 Create a Row for Every Date within a Date Range in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Create-a-Row-for-Every-Date-within-a-Date-Range/m-p/817105#M322525</link>
    <description>&lt;P&gt;I have created a process that makes a row for every date in a date range.&amp;nbsp; It is very tedious and slow when working with large data.&amp;nbsp; Can someone help me simply and speed up this process?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As a very small scale example, suppose I have this dataset:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
	create table have (SUBJID char(7), STDT num format=date9., ENDT num format=date9.);
	insert into have (subjid, stdt, endt)
		values ('101-001','01JAN2022'd,'07JAN2022'd)
		values ('101-002','01FEB2022'd,'28FEB2022'd)
		values ('101-003','01JAN2022'd,'14JAN2022'd)
		values ('101-004','01JAN2022'd,'03JAN2022'd);
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I want to get a record for every date within the start and end dates.&amp;nbsp; So, if the subject spans 3 days, then I want to turn it into 3 records where each record contains a date within that range.&amp;nbsp; This is what I am doing to get what I want:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have1;
	set have;
	REPS=endt-stdt+1;
run;



%macro replicate;
	proc sql noprint;
        select count(*) into :nrec trimmed from have1;
		select subjid, reps into :subj1-:subj&amp;amp;nrec., :rep1-:rep&amp;amp;nrec. from have1;
	quit;

	data want0;
		set %do i=1 %to &amp;amp;nrec.;
				%do j=1 %to &amp;amp;&amp;amp;rep&amp;amp;i.;
					have1 (where=(subjid="&amp;amp;&amp;amp;subj&amp;amp;i."))
				%end;
			%end;;
	run;
%mend replicate;

%replicate;



data want;
	set want0;
	by subjid;
	retain DATE;
	if first.subjid then date=stdt;
		else date=date+1;
	format date date9.;
run;
proc sort data=want;
	by subjid date;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This process works, but it isn't very effieicent for large date ranges.&amp;nbsp; I have some ranges that are up to 7000 days, so I am looping A LOT.&amp;nbsp; Is there a quicker/cleaner way to do this same method that won't take so much time?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 08 Jun 2022 14:45:34 GMT</pubDate>
    <dc:creator>djbateman</dc:creator>
    <dc:date>2022-06-08T14:45:34Z</dc:date>
    <item>
      <title>Create a Row for Every Date within a Date Range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-Row-for-Every-Date-within-a-Date-Range/m-p/817105#M322525</link>
      <description>&lt;P&gt;I have created a process that makes a row for every date in a date range.&amp;nbsp; It is very tedious and slow when working with large data.&amp;nbsp; Can someone help me simply and speed up this process?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As a very small scale example, suppose I have this dataset:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
	create table have (SUBJID char(7), STDT num format=date9., ENDT num format=date9.);
	insert into have (subjid, stdt, endt)
		values ('101-001','01JAN2022'd,'07JAN2022'd)
		values ('101-002','01FEB2022'd,'28FEB2022'd)
		values ('101-003','01JAN2022'd,'14JAN2022'd)
		values ('101-004','01JAN2022'd,'03JAN2022'd);
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I want to get a record for every date within the start and end dates.&amp;nbsp; So, if the subject spans 3 days, then I want to turn it into 3 records where each record contains a date within that range.&amp;nbsp; This is what I am doing to get what I want:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have1;
	set have;
	REPS=endt-stdt+1;
run;



%macro replicate;
	proc sql noprint;
        select count(*) into :nrec trimmed from have1;
		select subjid, reps into :subj1-:subj&amp;amp;nrec., :rep1-:rep&amp;amp;nrec. from have1;
	quit;

	data want0;
		set %do i=1 %to &amp;amp;nrec.;
				%do j=1 %to &amp;amp;&amp;amp;rep&amp;amp;i.;
					have1 (where=(subjid="&amp;amp;&amp;amp;subj&amp;amp;i."))
				%end;
			%end;;
	run;
%mend replicate;

%replicate;



data want;
	set want0;
	by subjid;
	retain DATE;
	if first.subjid then date=stdt;
		else date=date+1;
	format date date9.;
run;
proc sort data=want;
	by subjid date;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This process works, but it isn't very effieicent for large date ranges.&amp;nbsp; I have some ranges that are up to 7000 days, so I am looping A LOT.&amp;nbsp; Is there a quicker/cleaner way to do this same method that won't take so much time?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 08 Jun 2022 14:45:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-Row-for-Every-Date-within-a-Date-Range/m-p/817105#M322525</guid>
      <dc:creator>djbateman</dc:creator>
      <dc:date>2022-06-08T14:45:34Z</dc:date>
    </item>
    <item>
      <title>Re: Create a Row for Every Date within a Date Range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-Row-for-Every-Date-within-a-Date-Range/m-p/817112#M322527</link>
      <description>&lt;P&gt;If you have one observation per SUBJID (not clear from your post), why add all the complications?&amp;nbsp; Simply:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   set have;
   do date = stdt to endt;
      output;
   end;
   drop stdt enddt;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If this doesn't do the trick exactly, it should at least give you a starting point to consider.&lt;/P&gt;</description>
      <pubDate>Wed, 08 Jun 2022 14:54:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-Row-for-Every-Date-within-a-Date-Range/m-p/817112#M322527</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2022-06-08T14:54:12Z</dc:date>
    </item>
    <item>
      <title>Re: Create a Row for Every Date within a Date Range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-Row-for-Every-Date-within-a-Date-Range/m-p/817113#M322528</link>
      <description>&lt;P&gt;I actually have more than just SUBJID as BY variables in my real process because I'm working with ConMed data.&amp;nbsp; There is also a sequence ID, so a subject can have more than one record (and the date ranges could overlap).&amp;nbsp; I just figured I could take whatever solution someone else has and add in the sequence variable as well.&amp;nbsp; I'll play around with your solution (much simpler than what I have) and see if I can use it with my sequence variable added.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 08 Jun 2022 14:58:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-Row-for-Every-Date-within-a-Date-Range/m-p/817113#M322528</guid>
      <dc:creator>djbateman</dc:creator>
      <dc:date>2022-06-08T14:58:28Z</dc:date>
    </item>
    <item>
      <title>Re: Create a Row for Every Date within a Date Range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-Row-for-Every-Date-within-a-Date-Range/m-p/817117#M322530</link>
      <description>&lt;P&gt;It is trivial given your example data.&lt;/P&gt;
&lt;LI-SPOILER&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input subjid :$7. stdt :date. endt :date.;
  format stdt endt date9.;
cards;
101-001 01JAN2022 07JAN2022
101-002 01FEB2022 28FEB2022
101-003 01JAN2022 14JAN2022
101-004 01JAN2022 03JAN2022
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;/LI-SPOILER&gt;
&lt;P&gt;Just a simple DO loop.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  do date=stdt to endt ;
    output;
  end;
  format date date9.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If your real problem is more complex (multiple observations per ID, overlapping date ranges, etc) then perhaps you just want to use an array to mark which dates are covered.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let mindt=%sysevalf('01JAN2000'd);
%let maxdt=%sysevalf('31DEC2025'd);

data want ;
   set have;
   by subjid ;
   array _d [&amp;amp;mindt:&amp;amp;maxdt] _temporary_ ;
   if first.subjid then call missing(of _d(*));
   do date=max(stdt,&amp;amp;mindt) to min(endt,&amp;amp;maxdt);
      _d[date]=1;
   end;
   if last.subjid then do date=&amp;amp;mindt to &amp;amp;maxdt ;
      if _d[date] then output;
   end;
   keep subjid date ;
   format date date9.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 08 Jun 2022 15:12:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-Row-for-Every-Date-within-a-Date-Range/m-p/817117#M322530</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-06-08T15:12:56Z</dc:date>
    </item>
    <item>
      <title>Re: Create a Row for Every Date within a Date Range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-Row-for-Every-Date-within-a-Date-Range/m-p/817119#M322531</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/74"&gt;@djbateman&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I actually have more than just SUBJID as BY variables in my real process because I'm working with ConMed data.&amp;nbsp; There is also a sequence ID, so a subject can have more than one record (and the date ranges could overlap).&amp;nbsp; I just figured I could take whatever solution someone else has and add in the sequence variable as well.&amp;nbsp; I'll play around with your solution (much simpler than what I have) and see if I can use it with my sequence variable added.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Sounds like the goal is NOT to generate one observation per date.&lt;/P&gt;
&lt;P&gt;Instead it looks like you want to collapse potentially overlapping periods into a new set of non-overlapping periods.&lt;/P&gt;
&lt;P&gt;That is a question that has been asked and answered before. Search around.&lt;/P&gt;</description>
      <pubDate>Wed, 08 Jun 2022 15:11:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-Row-for-Every-Date-within-a-Date-Range/m-p/817119#M322531</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-06-08T15:11:41Z</dc:date>
    </item>
  </channel>
</rss>

