<?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: How to separate out and combine dates within a week of eachother for those with the same ID? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-separate-out-and-combine-dates-within-a-week-of-eachother/m-p/808475#M318796</link>
    <description>&lt;P&gt;LAG is not the way to go for this problem, because - among other reasons - it won't tell you whether the upcoming observation is more than seven days in the future.&amp;nbsp; And you need to know that in order to determine whether you have exhausted your seven-day period.&amp;nbsp; In particular, you need&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;a way to retain the first date of the 1-week period and the maximum test score over that same week&lt;BR /&gt;and&lt;/LI&gt;
&lt;LI&gt;a way to know when to output those retained values, i.e. when the next obs is over a week after the beginning of the week ... or the beginning of a new id.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;You'll have to test this below, in the absence of sample data in the form of a working data step:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=_:  nxt_:);
  set have (keep=id);
  by id;
  merge have have (firstobs=2 keep=date test rename=(date=nxt_date test=nxt_test));

  retain _temp_test _temp_date ;
  if missing(_temp_test) then _temp_test=test;
  if missing(_temp_date) then _temp_date=date;

  if last.id=0 and nxt_date&amp;lt;=_temp_date+7 then _temp_test=max(_temp_test,nxt_test);
  if last.id=1 or nxt_date&amp;gt;_temp_date+7;
  date=_temp_date;
  test=_temp_test;
  output;
  call missing(of _temp:);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This assumes the data are sorted by ID/DATE.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The _temp... variables are the prospective final values as you scan for data within a week.&amp;nbsp; Notice this program looks for dates within a week of the FIRST date of the period.&amp;nbsp; So if you have observations on the 1st, 4th, and 8th, only the 1st and 4th will be considered.&amp;nbsp; The 8th would start a new "week".&amp;nbsp; If, on the other hand you want to consolidate all observations until there is a gap of over a week, then change&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  if last.id=0 and nxt_date&amp;lt;=_temp_date+7 then _temp_test=max(_temp_test,nxt_test);
  if last.id=1 or nxt_date&amp;gt;_temp_date+7;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;to&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  if last.id=0 and nxt_date&amp;lt;=date+7 then _temp_test=max(_temp_test,nxt_test);
  if last.id=1 or nxt_date&amp;gt;date+7;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The SET statement reads only the ID variable.&amp;nbsp; In tandem with the BY statement, it provides accurate indication of the beginning of an ID (first.id=1) and the end of an id (last.id=1).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The MERGE statement merges each obs with the next obs, providing a way to look ahead at the upcoming date, and if necessary, the upcoming test.&lt;/P&gt;</description>
    <pubDate>Tue, 19 Apr 2022 03:44:54 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2022-04-19T03:44:54Z</dc:date>
    <item>
      <title>How to separate out and combine dates within a week of eachother for those with the same ID?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-separate-out-and-combine-dates-within-a-week-of-eachother/m-p/808100#M318647</link>
      <description>&lt;P&gt;Dear all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Below is an example of the dataset I am working with. I am trying to combine dates within a week of each other per ID. I have tried using lag and have been able to create a flag for records that have dates within a week of each other but am encountering an error when one person can have multiple records with multiple separate dates within a week (If that makes sense). I think I need to somehow create a unique ID again and cluster the data but I am a bit lost.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;ID&lt;/TD&gt;&lt;TD&gt;Date&lt;/TD&gt;&lt;TD&gt;Test&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;Jan 1, 2005&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;Jan 2, 2005&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;Jan 1, 2005&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;Jan 2, 2005&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;Feb 1, 2005&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;Feb 3, 2005&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;March 2, 2005&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;March 3, 2005&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;April 10, 2006&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My goal would be to have the end data (using max of tested and min of date), look like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;ID&lt;/TD&gt;&lt;TD&gt;Date&lt;/TD&gt;&lt;TD&gt;Tested&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;Jan 1, 2005&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;Jan 1, 2005&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;Feb 1, 2005&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;March 2, 2005&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;April 10, 2006&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for any help or consideration,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 15 Apr 2022 22:27:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-separate-out-and-combine-dates-within-a-week-of-eachother/m-p/808100#M318647</guid>
      <dc:creator>andym90</dc:creator>
      <dc:date>2022-04-15T22:27:50Z</dc:date>
    </item>
    <item>
      <title>Re: How to separate out and combine dates within a week of eachother for those with the same ID?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-separate-out-and-combine-dates-within-a-week-of-eachother/m-p/808450#M318786</link>
      <description>&lt;P&gt;Are we assuming a week is 7 consecutive days and not one of 52 periods or some number of Sun-Sat periods in a given year or leap year?&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What do you want done with cases where an individual has for example a Jan 1, a Jan 5, and a Jan 10 test date? Jan 5 is within one "week" of Jan 1, Jan 10 is within one "week" of Jan 5, but Jan 10 is not within one week of Jan 1.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 18 Apr 2022 22:33:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-separate-out-and-combine-dates-within-a-week-of-eachother/m-p/808450#M318786</guid>
      <dc:creator>HB</dc:creator>
      <dc:date>2022-04-18T22:33:39Z</dc:date>
    </item>
    <item>
      <title>Re: How to separate out and combine dates within a week of eachother for those with the same ID?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-separate-out-and-combine-dates-within-a-week-of-eachother/m-p/808475#M318796</link>
      <description>&lt;P&gt;LAG is not the way to go for this problem, because - among other reasons - it won't tell you whether the upcoming observation is more than seven days in the future.&amp;nbsp; And you need to know that in order to determine whether you have exhausted your seven-day period.&amp;nbsp; In particular, you need&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;a way to retain the first date of the 1-week period and the maximum test score over that same week&lt;BR /&gt;and&lt;/LI&gt;
&lt;LI&gt;a way to know when to output those retained values, i.e. when the next obs is over a week after the beginning of the week ... or the beginning of a new id.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;You'll have to test this below, in the absence of sample data in the form of a working data step:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=_:  nxt_:);
  set have (keep=id);
  by id;
  merge have have (firstobs=2 keep=date test rename=(date=nxt_date test=nxt_test));

  retain _temp_test _temp_date ;
  if missing(_temp_test) then _temp_test=test;
  if missing(_temp_date) then _temp_date=date;

  if last.id=0 and nxt_date&amp;lt;=_temp_date+7 then _temp_test=max(_temp_test,nxt_test);
  if last.id=1 or nxt_date&amp;gt;_temp_date+7;
  date=_temp_date;
  test=_temp_test;
  output;
  call missing(of _temp:);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This assumes the data are sorted by ID/DATE.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The _temp... variables are the prospective final values as you scan for data within a week.&amp;nbsp; Notice this program looks for dates within a week of the FIRST date of the period.&amp;nbsp; So if you have observations on the 1st, 4th, and 8th, only the 1st and 4th will be considered.&amp;nbsp; The 8th would start a new "week".&amp;nbsp; If, on the other hand you want to consolidate all observations until there is a gap of over a week, then change&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  if last.id=0 and nxt_date&amp;lt;=_temp_date+7 then _temp_test=max(_temp_test,nxt_test);
  if last.id=1 or nxt_date&amp;gt;_temp_date+7;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;to&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  if last.id=0 and nxt_date&amp;lt;=date+7 then _temp_test=max(_temp_test,nxt_test);
  if last.id=1 or nxt_date&amp;gt;date+7;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The SET statement reads only the ID variable.&amp;nbsp; In tandem with the BY statement, it provides accurate indication of the beginning of an ID (first.id=1) and the end of an id (last.id=1).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The MERGE statement merges each obs with the next obs, providing a way to look ahead at the upcoming date, and if necessary, the upcoming test.&lt;/P&gt;</description>
      <pubDate>Tue, 19 Apr 2022 03:44:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-separate-out-and-combine-dates-within-a-week-of-eachother/m-p/808475#M318796</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-04-19T03:44:54Z</dc:date>
    </item>
    <item>
      <title>Re: How to separate out and combine dates within a week of eachother for those with the same ID?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-separate-out-and-combine-dates-within-a-week-of-eachother/m-p/808492#M318812</link>
      <description>&lt;P&gt;Not sure if you want 7-days-intervals or weeks. If you actually want weeks, try:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=have nway;
   class Id Date;
   var Test;
   format Date weekv5.;
   output out= want(drop= _:) max=;
run;

proc datasets library= work nolist;
   modify want;
      format Date date9.;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 19 Apr 2022 05:50:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-separate-out-and-combine-dates-within-a-week-of-eachother/m-p/808492#M318812</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2022-04-19T05:50:00Z</dc:date>
    </item>
  </channel>
</rss>

