<?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 Comparing between dates across rows in clustered data structure in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Comparing-between-dates-across-rows-in-clustered-data-structure/m-p/564132#M158207</link>
    <description>&lt;P&gt;Here is the data I have&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id $ treatment $ linenumber date yymmdd10. ;
format date yymmdd10.;
cards;
001 A 1 2014-08-15
001 A 1 2015-06-20
001 A 2 2015-08-29
001 A 2 2018-02-28
001 A 2 2018-05-07
001 B 3 2018-08-01
001 B 4 2019-02-01 
001 C 5 2019-03-03
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I want to calculate the gap between the dates, and then create two flags:&lt;/P&gt;
&lt;P&gt;within the same ID/Treatment/Linenumber, if gap &amp;gt;90 days then GT90_within_line =1;&lt;/P&gt;
&lt;P&gt;within the same ID/Treatment, when linenumber changes, if the gap &amp;lt; 90 days, then LT90_btw_line_within_trt = 1&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The expected output data will be like this:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.PNG" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/30070i1AAFBAA76436CBF6/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture.PNG" alt="Capture.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can anyone help? Many thanks in advance!&lt;/P&gt;</description>
    <pubDate>Thu, 06 Jun 2019 14:41:46 GMT</pubDate>
    <dc:creator>fengyuwuzu</dc:creator>
    <dc:date>2019-06-06T14:41:46Z</dc:date>
    <item>
      <title>Comparing between dates across rows in clustered data structure</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Comparing-between-dates-across-rows-in-clustered-data-structure/m-p/564132#M158207</link>
      <description>&lt;P&gt;Here is the data I have&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id $ treatment $ linenumber date yymmdd10. ;
format date yymmdd10.;
cards;
001 A 1 2014-08-15
001 A 1 2015-06-20
001 A 2 2015-08-29
001 A 2 2018-02-28
001 A 2 2018-05-07
001 B 3 2018-08-01
001 B 4 2019-02-01 
001 C 5 2019-03-03
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I want to calculate the gap between the dates, and then create two flags:&lt;/P&gt;
&lt;P&gt;within the same ID/Treatment/Linenumber, if gap &amp;gt;90 days then GT90_within_line =1;&lt;/P&gt;
&lt;P&gt;within the same ID/Treatment, when linenumber changes, if the gap &amp;lt; 90 days, then LT90_btw_line_within_trt = 1&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The expected output data will be like this:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.PNG" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/30070i1AAFBAA76436CBF6/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture.PNG" alt="Capture.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can anyone help? Many thanks in advance!&lt;/P&gt;</description>
      <pubDate>Thu, 06 Jun 2019 14:41:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Comparing-between-dates-across-rows-in-clustered-data-structure/m-p/564132#M158207</guid>
      <dc:creator>fengyuwuzu</dc:creator>
      <dc:date>2019-06-06T14:41:46Z</dc:date>
    </item>
    <item>
      <title>Re: Comparing between dates across rows in clustered data structure</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Comparing-between-dates-across-rows-in-clustered-data-structure/m-p/564166#M158220</link>
      <description>&lt;P&gt;I have a solution but wonder if there is simpler methods&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id $ treatment $ linenumber date yymmdd10. ;
format date yymmdd10.;
cards;
001 A 1 2014-08-15
001 A 1 2015-06-20
001 A 2 2015-08-29
001 A 2 2018-02-28
001 A 2 2018-05-07
001 B 3 2018-08-01
001 B 4 2019-02-01 
001 C 5 2019-03-03
002 A 1 2015-06-27
002 A 1 2015-09-02
002 A 2 2016-01-01
;
run;
proc sort data=have;
	by id treatment linenumber date;
run;

data want;
	set have;
	by id treatment linenumber date;
	gap=dif(date);

	if first.id then
		gap=.;
run;

proc sql;
	create table step1 as 
		select *, case when count(date)&amp;gt;=2 and gap &amp;gt; 90 then 1 else . end as GT90_within_line
			from want
				group by id, treatment, linenumber
					order by id, treatment, linenumber, date;
quit;

data step2;
	set step1;
	by id treatment linenumber date;

	if not first.treatment and first.linenumber and . &amp;lt; gap &amp;lt;90 then
		LT90_btw_line_within_trt =1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 06 Jun 2019 15:31:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Comparing-between-dates-across-rows-in-clustered-data-structure/m-p/564166#M158220</guid>
      <dc:creator>fengyuwuzu</dc:creator>
      <dc:date>2019-06-06T15:31:39Z</dc:date>
    </item>
    <item>
      <title>Re: Comparing between dates across rows in clustered data structure</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Comparing-between-dates-across-rows-in-clustered-data-structure/m-p/564186#M158225</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/56807"&gt;@fengyuwuzu&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It can be done in one data step using lag function, if the input data set is sorted on&amp;nbsp;ID Treatment Linenumber Date.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=oID oTreatment oDate); 
	set have; by ID Treatment Linenumber;
	oID = lag(ID);
	oTreatment = lag(Treatment);
	oLinenumber = lag(Linenumber);
	oDate = lag(Date);
	if not first.ID then Gap = Date-oDate;
	if not first.Linenumber then do;
		if Gap &amp;gt; 90 then GT90_within_line = 1;
	end;
	else if not first.Treatment then if Gap &amp;lt; 90 then LT90_btw_line_within_trt = 1;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 06 Jun 2019 16:20:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Comparing-between-dates-across-rows-in-clustered-data-structure/m-p/564186#M158225</guid>
      <dc:creator>ErikLund_Jensen</dc:creator>
      <dc:date>2019-06-06T16:20:20Z</dc:date>
    </item>
    <item>
      <title>Re: Comparing between dates across rows in clustered data structure</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Comparing-between-dates-across-rows-in-clustered-data-structure/m-p/564250#M158248</link>
      <description>&lt;P&gt;Thank you!&lt;/P&gt;</description>
      <pubDate>Thu, 06 Jun 2019 18:27:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Comparing-between-dates-across-rows-in-clustered-data-structure/m-p/564250#M158248</guid>
      <dc:creator>fengyuwuzu</dc:creator>
      <dc:date>2019-06-06T18:27:38Z</dc:date>
    </item>
  </channel>
</rss>

