<?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: Creating new variables based on most recent date in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-based-on-most-recent-date/m-p/959744#M374432</link>
    <description>Whichever function you choose (dif or lag), make sure it executes on every observation. If you skip observations where first.med is 0, the next observation will have an incorrect calculation.</description>
    <pubDate>Thu, 20 Feb 2025 04:44:13 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2025-02-20T04:44:13Z</dc:date>
    <item>
      <title>Creating new variables based on most recent date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-based-on-most-recent-date/m-p/959692#M374410</link>
      <description>&lt;P&gt;I'm having trouble figuring out how to create two new variables that depend on the most recent date. Let's say I have ID, dx (diagnosis), a prescribed med, and a start date.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id dx $ med $ startdate :date9.;
format startdate date9.;
datalines;
1 a a 01JAN2020 
1 a a 04APR2020
1 a a 21APR2020
1 a a 01JUL2020
2 a a 01FEB2020
2 a b 01JUL2020
2 a a 07JUL2020
3 b b 01JAN2020
3 b a 04APR2020
3 b a 01MAY2020
3 b a 15JUN2020
3 b a 01SEP2020
3 b b 02SEP2020
;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;For any id, when there is &amp;gt; 60 day gap from the preceding date, given the same dx and med, I want to call this is a "new" prescription. For the same dx and med &amp;lt;60 days from the most recent date I want to call it a "refill".&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
input id dx $ med $ startdate :date9. refill $ new $;
format startdate date9.;
datalines;
1 a a 01JAN2020 0 0
1 a a 04APR2020 0 1
1 a a 21APR2020 1 0
1 a a 01JUL2020 0 1
2 a a 01FEB2020 0 0
2 a a 07JUL2020 0 1
2 a b 01JUL2020 0 0
3 b a 04APR2020 0 0
3 b a 01MAY2020 1 0
3 b a 15JUN2020 1 0
3 b a 01SEP2020 0 1
3 b b 01JAN2020 0 0
3 b b 02SEP2020 0 1
;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The code I've been trying to use is below.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have out=havesort;
by id dx med startdate;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
	set havesort;
	by id dx med;
	IF FIRST.med THEN DO;
	TEMP=startdate;
	refill=0;
	new=0;
	END;
	RETAIN TEMP refill new;
	IF startdate&amp;gt;TEMP + 60 THEN new+1; 
	IF TEMP&amp;lt;startdate&amp;lt;=TEMP + 60 then refill+1;
	TEMP=startdate;
	drop temp;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;However, as you can see, this doesn't reset the 60 day counter to the most recent startdate. How can I do that to get what I want?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Let me know if that does not make sense. Thank you always for the help!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Feb 2025 23:13:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-based-on-most-recent-date/m-p/959692#M374410</guid>
      <dc:creator>sasgorilla</dc:creator>
      <dc:date>2025-02-19T23:13:27Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new variables based on most recent date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-based-on-most-recent-date/m-p/959701#M374412</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have; by id dx; run;

data want;
set have;
by id dx;
length lastdt 4 refill new 3;
format lastdt date9.;
retain lastdt;
if first.dx then lastdt=.;
if startdate-lastdt&amp;gt;60 then new=1;
else if lastdt&amp;gt;. then refill=1;
lastdt=startdate;
run;

proc print data=want; run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 19 Feb 2025 23:36:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-based-on-most-recent-date/m-p/959701#M374412</guid>
      <dc:creator>quickbluefish</dc:creator>
      <dc:date>2025-02-19T23:36:16Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new variables based on most recent date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-based-on-most-recent-date/m-p/959710#M374413</link>
      <description>&lt;P&gt;Thanks&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/223320"&gt;@quickbluefish&lt;/a&gt;&amp;nbsp;! I hadn't even thought to make a separate date column.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So, this worked with a subtle correction. Because the med type can vary and that is relevant to whether it was a new or refill for that particular med, I used the following to get what I needed:&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have out=havesort;
	by id dx med;
run;

data want;
set havesort;
by id dx med;
length lastdt 4 refill new 3;
format lastdt date9.;
retain lastdt;
if first.med then lastdt=.;
if startdate-lastdt&amp;gt;60 then new=1;
else if lastdt&amp;gt;. then refill=1;
lastdt=startdate;
run;

proc print data=want;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 19 Feb 2025 23:47:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-based-on-most-recent-date/m-p/959710#M374413</guid>
      <dc:creator>sasgorilla</dc:creator>
      <dc:date>2025-02-19T23:47:44Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new variables based on most recent date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-based-on-most-recent-date/m-p/959713#M374415</link>
      <description>Oh, missed that part! Glad you got it to work.  Yeah, so many of these kinds of problems that seem tricky are suddenly very simple once the data are sorted appropriately.</description>
      <pubDate>Thu, 20 Feb 2025 01:28:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-based-on-most-recent-date/m-p/959713#M374415</guid>
      <dc:creator>quickbluefish</dc:creator>
      <dc:date>2025-02-20T01:28:59Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new variables based on most recent date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-based-on-most-recent-date/m-p/959715#M374416</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id dx $ med $ startdate :date9.;
format startdate date9.;
datalines;
1 a a 01JAN2020 
1 a a 04APR2020
1 a a 21APR2020
1 a a 01JUL2020
2 a a 01FEB2020
2 a b 01JUL2020
2 a a 07JUL2020
3 b b 01JAN2020
3 b a 04APR2020
3 b a 01MAY2020
3 b a 15JUN2020
3 b a 01SEP2020
3 b b 02SEP2020
;
RUN;
proc sort data=have;by id dx med startdate;run;
data want;
 set have;
 by id dx med;
 refill=0;new=0;
 if not first.med and .&amp;lt;dif(startdate)&amp;lt;60 then refill=1;
 if not first.med and   dif(startdate)&amp;gt;60 then new=1;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 20 Feb 2025 01:45:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-based-on-most-recent-date/m-p/959715#M374416</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2025-02-20T01:45:44Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new variables based on most recent date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-based-on-most-recent-date/m-p/959730#M374423</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have out=need;
  by id dx med startdate;
run;

data want;
  set need;
  by id dx med;
  refill=(first.med=0 and lag(startdate)&amp;gt;=startdate-60);
  new=(first.med=0 and lag(startdate)&amp;lt;startdate-60);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 20 Feb 2025 02:37:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-based-on-most-recent-date/m-p/959730#M374423</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2025-02-20T02:37:36Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new variables based on most recent date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-based-on-most-recent-date/m-p/959736#M374425</link>
      <description>&lt;P&gt;Thank you&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;. I think this is the cleanest solution! I added "&amp;lt;=60" for the refill =1 and it got me what I wanted.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Question, about the dif function, as I'm not familiar with that... In your code I'm assuming the difference is being taken between the preceding startdate and the current startdate because of the "if not first.med" statement. Is that correct?&amp;nbsp;&lt;/P&gt;
&lt;PRE class="language-sas"&gt;&lt;CODE&gt;if not first.med and .&amp;lt;dif(startdate)&amp;lt;60 then refill=1;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 20 Feb 2025 03:20:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-based-on-most-recent-date/m-p/959736#M374425</guid>
      <dc:creator>sasgorilla</dc:creator>
      <dc:date>2025-02-20T03:20:46Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new variables based on most recent date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-based-on-most-recent-date/m-p/959739#M374428</link>
      <description>&lt;P&gt;This is another good option, thank you! This one doesn't feel as intuitive to me due to the "lag" function. I'll have to get more comfortable with it.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;I did find the SAS documentation has a good example &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/n10t6w4op574ttn11yk68dottp7f.htm" target="_self"&gt;here&lt;/A&gt;.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 20 Feb 2025 03:28:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-based-on-most-recent-date/m-p/959739#M374428</guid>
      <dc:creator>sasgorilla</dc:creator>
      <dc:date>2025-02-20T03:28:47Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new variables based on most recent date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-based-on-most-recent-date/m-p/959743#M374431</link>
      <description>dif() &lt;BR /&gt;is the same as &lt;BR /&gt;startdate-lag(startdate)&lt;BR /&gt;For example:&lt;BR /&gt;date                   dif&lt;BR /&gt;01jan2020         .&lt;BR /&gt;04jan2020        3&lt;BR /&gt;06jan2020        2&lt;BR /&gt;05jan2020        -1&lt;BR /&gt;&lt;BR /&gt;Here  I used "if not first.med"  to avoid to assign refill=1 and new=1 when it is the first obs of each group.&lt;BR /&gt;a.k.a&lt;BR /&gt;refill and new will  always get 0 for the first obs of each group  .&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Thu, 20 Feb 2025 03:48:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-based-on-most-recent-date/m-p/959743#M374431</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2025-02-20T03:48:55Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new variables based on most recent date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-based-on-most-recent-date/m-p/959744#M374432</link>
      <description>Whichever function you choose (dif or lag), make sure it executes on every observation. If you skip observations where first.med is 0, the next observation will have an incorrect calculation.</description>
      <pubDate>Thu, 20 Feb 2025 04:44:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-based-on-most-recent-date/m-p/959744#M374432</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2025-02-20T04:44:13Z</dc:date>
    </item>
  </channel>
</rss>

