<?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: Flag for different codes existing in a 30 day period in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Flag-for-different-codes-existing-in-a-30-day-period/m-p/826536#M326474</link>
    <description>&lt;P&gt;If the data are sorted by memberid/locationid/date, then this should do:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (keep=memberid flag);
  set have;
  by memberid locationid date ;
  if first.memberid=1 then flag=0;
  retain flag;
  if first.locationid=0 and code^=lag(code) and dif(date)&amp;lt;=30 then flag=1;
  if last.memberid;
run;
  &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Flag is set to zero at first record for a memberid.&amp;nbsp; All subsequent records are checked to see if flag should be 1.&amp;nbsp; It's never reset from 1 to zero for a memberid.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The flag check tests whether (1) the record in hand is NOT the start of a locationid, (2) the current code differs from the prior code, and (3) the current date minus last date &amp;lt;=30.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 01 Aug 2022 22:51:05 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2022-08-01T22:51:05Z</dc:date>
    <item>
      <title>Flag for different codes existing in a 30 day period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Flag-for-different-codes-existing-in-a-30-day-period/m-p/826505#M326468</link>
      <description>&lt;P&gt;Hello,&amp;nbsp; I'm looking to create a flag for members who have 2 or more different codes within the span of 30 days and the same location.&amp;nbsp; There are 4 possible codes a member (24, 25, 26, 27) can have and they can have the same code more than once.&amp;nbsp; Here's and example of the data and the flag I would like to create:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Memberid&amp;nbsp; &amp;nbsp; date&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; locationid&amp;nbsp; &amp;nbsp;code&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 03Feb2022&amp;nbsp; &amp;nbsp; 12&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;24&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 24Feb2022&amp;nbsp; &amp;nbsp; 12&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;24&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 28Mar2022&amp;nbsp; &amp;nbsp; 13&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;24&lt;/P&gt;
&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 03Feb2022&amp;nbsp; &amp;nbsp; 13&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;24&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 24Feb2022&amp;nbsp; &amp;nbsp; 13&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;25&lt;/P&gt;
&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 03Feb2022&amp;nbsp; &amp;nbsp; 12&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;24&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 24Feb2022&amp;nbsp; &amp;nbsp; 13&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;26&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;output data:&lt;/P&gt;
&lt;P&gt;Memberid&amp;nbsp; &amp;nbsp;Flag&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/P&gt;
&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 01 Aug 2022 19:14:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Flag-for-different-codes-existing-in-a-30-day-period/m-p/826505#M326468</guid>
      <dc:creator>jmmedina25</dc:creator>
      <dc:date>2022-08-01T19:14:26Z</dc:date>
    </item>
    <item>
      <title>Re: Flag for different codes existing in a 30 day period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Flag-for-different-codes-existing-in-a-30-day-period/m-p/826508#M326469</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
    create table want as select memberid,count(distinct code)&amp;gt;1 as flag
    from have 
    group by memberid;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This should match your output. However, your output does not match your description of the problem, the 30 day period is ignored. Perhaps you can explain how that is used, and what the output should look like if the 30 day time period is used.&lt;/P&gt;</description>
      <pubDate>Mon, 01 Aug 2022 19:50:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Flag-for-different-codes-existing-in-a-30-day-period/m-p/826508#M326469</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-08-01T19:50:38Z</dc:date>
    </item>
    <item>
      <title>Re: Flag for different codes existing in a 30 day period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Flag-for-different-codes-existing-in-a-30-day-period/m-p/826536#M326474</link>
      <description>&lt;P&gt;If the data are sorted by memberid/locationid/date, then this should do:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (keep=memberid flag);
  set have;
  by memberid locationid date ;
  if first.memberid=1 then flag=0;
  retain flag;
  if first.locationid=0 and code^=lag(code) and dif(date)&amp;lt;=30 then flag=1;
  if last.memberid;
run;
  &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Flag is set to zero at first record for a memberid.&amp;nbsp; All subsequent records are checked to see if flag should be 1.&amp;nbsp; It's never reset from 1 to zero for a memberid.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The flag check tests whether (1) the record in hand is NOT the start of a locationid, (2) the current code differs from the prior code, and (3) the current date minus last date &amp;lt;=30.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 01 Aug 2022 22:51:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Flag-for-different-codes-existing-in-a-30-day-period/m-p/826536#M326474</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-08-01T22:51:05Z</dc:date>
    </item>
    <item>
      <title>Re: Flag for different codes existing in a 30 day period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Flag-for-different-codes-existing-in-a-30-day-period/m-p/826605#M326503</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Memberid    date    : date9.          locationid   code      ;
format date date9.;
cards;
1                  03Feb2022    12               24        
1                  24Feb2022    12               24
1                  28Mar2022    13               24
2                  03Feb2022    13               24        
2                  24Feb2022    13               25
3                  03Feb2022    12               24        
3                  24Feb2022    13               26
;

proc sql;
create table want as
 select memberid,max(flag) as flag
  from (
select memberid,locationid,count(distinct code)&amp;gt;1 and range(date)&amp;lt;=30 as flag
    from have 
    group by memberid,locationid
)
group by memberid
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 02 Aug 2022 12:00:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Flag-for-different-codes-existing-in-a-30-day-period/m-p/826605#M326503</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2022-08-02T12:00:53Z</dc:date>
    </item>
    <item>
      <title>Re: Flag for different codes existing in a 30 day period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Flag-for-different-codes-existing-in-a-30-day-period/m-p/826656#M326516</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Memberid    date    : date9.          locationid   code      ;
format date date9.;
cards;
1                  03Feb2022    12               24        
1                  24Feb2022    12               24
1                  28Mar2022    13               24
2                  03Feb2022    13               24        
2                  24Feb2022    13               25
3                  03Feb2022    12               24        
3                  24Feb2022    13               26
;

proc sql;
create table want as
 select memberid,max(flag) as flag
  from (
select memberid,locationid,count(distinct code)&amp;gt;1 and range(date)&amp;lt;=30 as flag
    from have 
    group by memberid,locationid
)
group by memberid
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;This is different than how I read the OP's request&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; "&lt;SPAN&gt;2 or more different codes within the span of 30 days and the same location"&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;I took it to mean that a given location may have a total date range &amp;gt;30 but would still get FLAG=1 if any two distinct codes were within 30 days of each other.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/261072"&gt;@jmmedina25&lt;/a&gt;:&amp;nbsp; &amp;nbsp;If instead you mean that total range of all dates is &amp;lt;=30 then use&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;'s suggestion and discard my reply.&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, 02 Aug 2022 15:04:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Flag-for-different-codes-existing-in-a-30-day-period/m-p/826656#M326516</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-08-02T15:04:09Z</dc:date>
    </item>
    <item>
      <title>Re: Flag for different codes existing in a 30 day period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Flag-for-different-codes-existing-in-a-30-day-period/m-p/826845#M326599</link>
      <description>Yeah. Maybe I understand wrong . It is all depend what OP want. OP can test it and pick up the correct one .</description>
      <pubDate>Wed, 03 Aug 2022 12:52:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Flag-for-different-codes-existing-in-a-30-day-period/m-p/826845#M326599</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2022-08-03T12:52:01Z</dc:date>
    </item>
  </channel>
</rss>

