<?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 do I flag 5 values that occur within 30 day time span for each observation. in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/How-do-I-flag-5-values-that-occur-within-30-day-time-span-for/m-p/809451#M33748</link>
    <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/423742"&gt;@sharise&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The below is far from the most elegant solution possible, but, hey, it's weekend.&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
 LENGTH personID $ 3 date 8 providerID $ 3;
 format date date9.;
 input personID $ date : date9. providerID $;
 datalines;
PE1  12FEB2012  PR7
PE1  28FEB2012  PR7
PE1  07MAR2012  PR8
PE1  12MAR2012  PR2
PE1  22MAR2012  PR7
PE1  24MAR2012  PR7
PE1  01APR2012  PR7
PE9  12FEB2014  PR2
PE9  24FEB2014  PR3
PE9  01APR2014  PR2
PE9  07APR2014  PR3
PE9  17APR2014  PR2
PE9  19APR2014  PR2
PE9  30APR2014  PR3
PE9  08MAY2014  PR3
PE8  14FEB2014  PR2
PE8  15FEB2014  PR3
PE8  16FEB2014  PR2
PE8  17FEB2014  PR3
PE8  18FEB2014  PR2
PE8  19FEB2014  PR2
PE8  20FEB2014  PR3
PE8  21FEB2014  PR3
;
run;

proc sort data=have;
 by personID date providerID;
run;

data have1(drop = date providerID);
 LENGTH personID $ 3 ALLvisits $ 1100;
 set have;
 by personID date providerID;
 retain ALLvisits '';
 if first.personID     then ALLvisits=put(date,date9.);
 if NOT first.personID then ALLvisits = trim(left(ALLvisits)) !! '#' !! put(date,date9.);
 if last.personID then output;
run;

data have2;
 merge have have1;
 by personID;
run;

data want(drop = di);
 set have2(rename=(date=date_from));
 count=0;
 do di = (date_from) to (date_from + 30);
  if index(ALLvisits,put(di,date9.))&amp;gt;0 then count=count+1; 
 end;
 format di date9.;
run;

PROC MEANS data=want MAX NWAY noprint;
 CLASS personID;
 var count;
 output out=work.want_out MAX= / autoname;
run;

PROC DATASETS LIBRARY=WORK NoList;
 modify want_out;
  label count_Max = 'Maximum number of visits in a 30 days timeframe'; run;
QUIT;
/* end of program */&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Good luck with learning SAS,&lt;/P&gt;
&lt;P&gt;Koen&lt;/P&gt;</description>
    <pubDate>Sat, 23 Apr 2022 16:25:18 GMT</pubDate>
    <dc:creator>sbxkoenk</dc:creator>
    <dc:date>2022-04-23T16:25:18Z</dc:date>
    <item>
      <title>How do I flag 5 values that occur within 30 day time span for each observation.</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-do-I-flag-5-values-that-occur-within-30-day-time-span-for/m-p/809446#M33747</link>
      <description>&lt;P&gt;I have dataset attached below. This dataset is simulating hospital/clinic data. Each observation defines a visit. A visit is&amp;nbsp;defined as the unique combination of person, date, provider.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am trying to flag persons with at least 5 visits within a 30 day time span. I know how to use where and if statements and I have read from other forums about counting consecutive variable values but I'm unsure how to program for a range of within 30 days. Please excuse me if this is a very simple question but I am not that advanced of a SAS user and having a hard time visualizing this.&lt;/P&gt;&lt;P&gt;I am trying a proc statement but I don't know what functions can give me the 5 visits within 3 days. I am stuck on code below. I don't know if I should even be trying to do this using a proc freq. Maybe I should be doing a data step?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;proc freq data=p.persons;
where date =
run;&lt;/CODE&gt;&amp;nbsp;&lt;/PRE&gt;&lt;P&gt;&lt;SPAN&gt;My version of SAS is 9.04.01M6P110718&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 23 Apr 2022 14:52:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-do-I-flag-5-values-that-occur-within-30-day-time-span-for/m-p/809446#M33747</guid>
      <dc:creator>sharise</dc:creator>
      <dc:date>2022-04-23T14:52:54Z</dc:date>
    </item>
    <item>
      <title>Re: How do I flag 5 values that occur within 30 day time span for each observation.</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-do-I-flag-5-values-that-occur-within-30-day-time-span-for/m-p/809451#M33748</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/423742"&gt;@sharise&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The below is far from the most elegant solution possible, but, hey, it's weekend.&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
 LENGTH personID $ 3 date 8 providerID $ 3;
 format date date9.;
 input personID $ date : date9. providerID $;
 datalines;
PE1  12FEB2012  PR7
PE1  28FEB2012  PR7
PE1  07MAR2012  PR8
PE1  12MAR2012  PR2
PE1  22MAR2012  PR7
PE1  24MAR2012  PR7
PE1  01APR2012  PR7
PE9  12FEB2014  PR2
PE9  24FEB2014  PR3
PE9  01APR2014  PR2
PE9  07APR2014  PR3
PE9  17APR2014  PR2
PE9  19APR2014  PR2
PE9  30APR2014  PR3
PE9  08MAY2014  PR3
PE8  14FEB2014  PR2
PE8  15FEB2014  PR3
PE8  16FEB2014  PR2
PE8  17FEB2014  PR3
PE8  18FEB2014  PR2
PE8  19FEB2014  PR2
PE8  20FEB2014  PR3
PE8  21FEB2014  PR3
;
run;

proc sort data=have;
 by personID date providerID;
run;

data have1(drop = date providerID);
 LENGTH personID $ 3 ALLvisits $ 1100;
 set have;
 by personID date providerID;
 retain ALLvisits '';
 if first.personID     then ALLvisits=put(date,date9.);
 if NOT first.personID then ALLvisits = trim(left(ALLvisits)) !! '#' !! put(date,date9.);
 if last.personID then output;
run;

data have2;
 merge have have1;
 by personID;
run;

data want(drop = di);
 set have2(rename=(date=date_from));
 count=0;
 do di = (date_from) to (date_from + 30);
  if index(ALLvisits,put(di,date9.))&amp;gt;0 then count=count+1; 
 end;
 format di date9.;
run;

PROC MEANS data=want MAX NWAY noprint;
 CLASS personID;
 var count;
 output out=work.want_out MAX= / autoname;
run;

PROC DATASETS LIBRARY=WORK NoList;
 modify want_out;
  label count_Max = 'Maximum number of visits in a 30 days timeframe'; run;
QUIT;
/* end of program */&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Good luck with learning SAS,&lt;/P&gt;
&lt;P&gt;Koen&lt;/P&gt;</description>
      <pubDate>Sat, 23 Apr 2022 16:25:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-do-I-flag-5-values-that-occur-within-30-day-time-span-for/m-p/809451#M33748</guid>
      <dc:creator>sbxkoenk</dc:creator>
      <dc:date>2022-04-23T16:25:18Z</dc:date>
    </item>
    <item>
      <title>Re: How do I flag 5 values that occur within 30 day time span for each observation.</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-do-I-flag-5-values-that-occur-within-30-day-time-span-for/m-p/809452#M33749</link>
      <description>&lt;P&gt;See also previous reply !!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Other approaches I am thinking of :&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;using a hash table for lookup&lt;/LI&gt;
&lt;LI&gt;overlapping time intervals as in the below blog&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Calculating the overlap of date/time intervals&lt;BR /&gt;By Leonid Batkhan on SAS Users January 13, 2022&lt;BR /&gt;&lt;A href="https://blogs.sas.com/content/sgf/2022/01/13/calculating-the-overlap-of-date-time-intervals/" target="_blank"&gt;https://blogs.sas.com/content/sgf/2022/01/13/calculating-the-overlap-of-date-time-intervals/&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Koen&lt;/P&gt;</description>
      <pubDate>Sat, 23 Apr 2022 16:28:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-do-I-flag-5-values-that-occur-within-30-day-time-span-for/m-p/809452#M33749</guid>
      <dc:creator>sbxkoenk</dc:creator>
      <dc:date>2022-04-23T16:28:59Z</dc:date>
    </item>
    <item>
      <title>Re: How do I flag 5 values that occur within 30 day time span for each observation.</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-do-I-flag-5-values-that-occur-within-30-day-time-span-for/m-p/809492#M33757</link>
      <description>&lt;P&gt;I take the objective&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;I am trying to flag persons with at least 5 visits within a 30 day time span&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;to mean you want a dataset with one record per person, with a flag variable indicating that they had at least one 30 day span with 5 or more visits.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And the input data is a one-record-per-visit file, sorted by person id (variable PERSON) and date (DATE)..&amp;nbsp; So you can't go directly to a PROC with a where filter.&amp;nbsp; You need to create a new one-obs-per-person dataset from the one-obs-per-visit dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is a simple example.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data persons (keep=person flag);
  set visits ; 
  by person date;
  retain flag;

  if first.id then flag=0;
  if lag4(id)=id and lag4(date)&amp;gt;=date-30 then flag=1;
  if last.id;
run;
proc freq data=persons;
  tables flag;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;As I mentioned above this expects the VISITS dataset to be sorted by PERSON/DATE.&amp;nbsp; The BY statement above really only requires BY PERSON to produce the desired results - it doesn't need the DATE variable in the BY statement.&amp;nbsp; But I put it there because SAS will stop the data step and let you know if any DATE is out of order.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now if you need something more than this simplistic PERSONS dataset, let us know.&lt;/P&gt;</description>
      <pubDate>Sun, 24 Apr 2022 14:41:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-do-I-flag-5-values-that-occur-within-30-day-time-span-for/m-p/809492#M33757</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-04-24T14:41:30Z</dc:date>
    </item>
    <item>
      <title>Re: How do I flag 5 values that occur within 30 day time span for each observation.</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-do-I-flag-5-values-that-occur-within-30-day-time-span-for/m-p/809505#M33760</link>
      <description>&lt;P&gt;omg, it can be as simple as that of course.&lt;/P&gt;
&lt;P&gt;Again, it's the effect of the weekend, shall we say. &lt;span class="lia-unicode-emoji" title=":smirking_face:"&gt;😏&lt;/span&gt;&lt;span class="lia-unicode-emoji" title=":sleeping_face:"&gt;😴&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Koen&lt;/P&gt;
&lt;DIV id="ConnectiveDocSignExtentionInstalled" data-extension-version="1.0.4"&gt;&amp;nbsp;&lt;/DIV&gt;</description>
      <pubDate>Sun, 24 Apr 2022 15:07:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-do-I-flag-5-values-that-occur-within-30-day-time-span-for/m-p/809505#M33760</guid>
      <dc:creator>sbxkoenk</dc:creator>
      <dc:date>2022-04-24T15:07:02Z</dc:date>
    </item>
    <item>
      <title>Re: How do I flag 5 values that occur within 30 day time span for each observation.</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-do-I-flag-5-values-that-occur-within-30-day-time-span-for/m-p/809551#M33765</link>
      <description>&lt;P&gt;you could use inexact matching with Proc SQL.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 24 Apr 2022 22:29:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-do-I-flag-5-values-that-occur-within-30-day-time-span-for/m-p/809551#M33765</guid>
      <dc:creator>tarheel13</dc:creator>
      <dc:date>2022-04-24T22:29:13Z</dc:date>
    </item>
  </channel>
</rss>

