<?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 Subsequent Records based on initial 'Anchor' record in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Flag-Subsequent-Records-based-on-initial-Anchor-record/m-p/413849#M101330</link>
    <description>&lt;P&gt;I think I have the right intent here, but it may depend on what should happen if the same KEY has multiple ACUTE observations.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;data all_claims4;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;set all_claims3;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;by key;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;if first.key&amp;nbsp;&lt;FONT color="#000000"&gt;then flag=0;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;FONT color="#000000"&gt;&lt;SPAN&gt;if upcase(prov_type) = 'ACUTE' and flag=0 then flag = 1;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;if upcase(prov_type) ne 'ACUTE' and&lt;/FONT&gt;&lt;SPAN&gt;&lt;FONT color="#000000"&gt;&amp;nbsp;rea&lt;/FONT&gt;dm_days le 5 and flag=1 then do;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp;first_pac='Y';&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp;output;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp;first_pac=' ';&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp;flag=5;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;end;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;else output;&lt;/P&gt;
&lt;P&gt;retain flag;&lt;/P&gt;
&lt;P&gt;drop flag;&lt;BR /&gt;&lt;SPAN&gt;run;&lt;/SPAN&gt;&lt;/P&gt;</description>
    <pubDate>Thu, 16 Nov 2017 00:00:29 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2017-11-16T00:00:29Z</dc:date>
    <item>
      <title>Flag Subsequent Records based on initial 'Anchor' record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Flag-Subsequent-Records-based-on-initial-Anchor-record/m-p/413823#M101317</link>
      <description>&lt;P&gt;Will probably be easier to see that to explain in the subject line, so thank you for looking.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Attempting to flag a PAC (post acute care) record based on whether or not there was an acute stay prior to the PAC visit.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data have;&lt;/P&gt;&lt;P&gt;input key $ prov_type $ key_lag $ readm_days;&lt;/P&gt;&lt;P&gt;datalines;&lt;/P&gt;&lt;P&gt;971 acute 0 0&lt;/P&gt;&lt;P&gt;971 ltac 971 0&lt;/P&gt;&lt;P&gt;971 irf 971 0&lt;/P&gt;&lt;P&gt;510 psych 971 0&lt;/P&gt;&lt;P&gt;510 acute 0 0&lt;/P&gt;&lt;P&gt;510 HH 510 1&lt;/P&gt;&lt;P&gt;510 HH 510 1&lt;/P&gt;&lt;P&gt;510 HH 510 1&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I would like to see is a new flag on row 2 and 6.&amp;nbsp; I get repeating flags on 6,7,8. Row 4 should not have a flag because it occurs before (chronologically) the next acute record.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is the code I've been banging up against the wall for awhile.&amp;nbsp; It also might make more sense than my 'english' from above.&lt;/P&gt;&lt;P&gt;data all_claims4;&lt;BR /&gt;set all_claims3;&lt;BR /&gt;by key;&lt;BR /&gt;&lt;BR /&gt;if key=key_lg then do;&lt;BR /&gt;if first.key&lt;BR /&gt;and prov_type ne 'ACUTE'&lt;BR /&gt;and readm_days le 5 then first_pac='Y';&lt;BR /&gt;end;&lt;BR /&gt;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would be glad to provide more detail if necessary.&amp;nbsp; There are other variables in the set like admission date that I am not using for this run, but if it makes sense to include that variable for the solution, I can.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you in advance for sharing your wisdom.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;-Brian&lt;/P&gt;</description>
      <pubDate>Wed, 15 Nov 2017 22:10:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Flag-Subsequent-Records-based-on-initial-Anchor-record/m-p/413823#M101317</guid>
      <dc:creator>shounster</dc:creator>
      <dc:date>2017-11-15T22:10:52Z</dc:date>
    </item>
    <item>
      <title>Re: Flag Subsequent Records based on initial 'Anchor' record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Flag-Subsequent-Records-based-on-initial-Anchor-record/m-p/413840#M101325</link>
      <description>&lt;P&gt;See if this is doing what you want without the days considered.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data have;
   input key $ prov_type $ ;
datalines;
971 acute 
971 ltac 
971 irf 
510 psych 
510 acute 
510 HH 
510 HH 
510 HH 
;
run;
/* assume the data in Have is in the order you want to process*/
data want;
   set have;
   by notsorted key;
   Retain Flag 0;
   lp=lag(prov_type);
   if first.key then flag=0;
   else flag= (lp = 'acute');
   drop lp;
run;

&lt;/PRE&gt;
&lt;P&gt;Since you didn't show any values for the readm_days larger than 1 this should work for your example data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To add day limit&lt;/P&gt;
&lt;PRE&gt;&lt;FONT face="SAS Monospace" size="2"&gt;else flag= (lp = 'acute' and readm_days &amp;lt;5);&lt;/FONT&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;BTW there are many good reasons to code yes/no as 1/0 instead of character. For instance the sum within a group of records is number of yes values, the mean is the percentage. Also SAS will treat 1 as 'true' (actually that's how the value is assigned in this code) so you can use statements like:&lt;/P&gt;
&lt;P&gt;If flag then ...&lt;/P&gt;
&lt;P&gt;instead of having to&amp;nbsp;&amp;nbsp;explicitly us&lt;/P&gt;
&lt;P&gt;If flag='Y' then ...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And if a person wants to read Yes or No use a custom format to display desired text for reports.&lt;/P&gt;</description>
      <pubDate>Wed, 15 Nov 2017 23:23:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Flag-Subsequent-Records-based-on-initial-Anchor-record/m-p/413840#M101325</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-11-15T23:23:25Z</dc:date>
    </item>
    <item>
      <title>Re: Flag Subsequent Records based on initial 'Anchor' record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Flag-Subsequent-Records-based-on-initial-Anchor-record/m-p/413849#M101330</link>
      <description>&lt;P&gt;I think I have the right intent here, but it may depend on what should happen if the same KEY has multiple ACUTE observations.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;data all_claims4;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;set all_claims3;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;by key;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;if first.key&amp;nbsp;&lt;FONT color="#000000"&gt;then flag=0;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;FONT color="#000000"&gt;&lt;SPAN&gt;if upcase(prov_type) = 'ACUTE' and flag=0 then flag = 1;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;if upcase(prov_type) ne 'ACUTE' and&lt;/FONT&gt;&lt;SPAN&gt;&lt;FONT color="#000000"&gt;&amp;nbsp;rea&lt;/FONT&gt;dm_days le 5 and flag=1 then do;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp;first_pac='Y';&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp;output;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp;first_pac=' ';&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp;flag=5;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;end;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;else output;&lt;/P&gt;
&lt;P&gt;retain flag;&lt;/P&gt;
&lt;P&gt;drop flag;&lt;BR /&gt;&lt;SPAN&gt;run;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 16 Nov 2017 00:00:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Flag-Subsequent-Records-based-on-initial-Anchor-record/m-p/413849#M101330</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-11-16T00:00:29Z</dc:date>
    </item>
    <item>
      <title>Re: Flag Subsequent Records based on initial 'Anchor' record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Flag-Subsequent-Records-based-on-initial-Anchor-record/m-p/413986#M101392</link>
      <description>Thank you! I just kept getting stuck on the flagging of the variables I needed. Also, thank you for the tip on the Y/N v 1/0!</description>
      <pubDate>Thu, 16 Nov 2017 13:24:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Flag-Subsequent-Records-based-on-initial-Anchor-record/m-p/413986#M101392</guid>
      <dc:creator>shounster</dc:creator>
      <dc:date>2017-11-16T13:24:53Z</dc:date>
    </item>
    <item>
      <title>Re: Flag Subsequent Records based on initial 'Anchor' record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Flag-Subsequent-Records-based-on-initial-Anchor-record/m-p/413987#M101393</link>
      <description>Thank you so very much. This solution worked as well. I appreciate you taking the time.</description>
      <pubDate>Thu, 16 Nov 2017 13:25:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Flag-Subsequent-Records-based-on-initial-Anchor-record/m-p/413987#M101393</guid>
      <dc:creator>shounster</dc:creator>
      <dc:date>2017-11-16T13:25:25Z</dc:date>
    </item>
  </channel>
</rss>

