<?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: Using Lag function and first.? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Using-Lag-function-and-first/m-p/565397#M158730</link>
    <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What does the injury 0/1 values stand for in the data set?&lt;/P&gt;
&lt;P&gt;And those are what type of dates? They look like date time but are all in 1960 so I'm guessing those are off for some reason?&lt;/P&gt;
&lt;P&gt;Not sure what they reflect.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your code isn't far off, so this is my last answer here, which assumes visit is your visit date. This also assumes any previous visit within 90 days is the same from the last visit, not the original visit.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;

by id;

length type $20.;

prev_visit = lag(visit);

if first.id then call missing(prev_visit);

if visit - prev_visit &amp;lt;= 90 then type='Follow Up';
else type = 'New';

run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 11 Jun 2019 20:54:13 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2019-06-11T20:54:13Z</dc:date>
    <item>
      <title>Using Lag function and first.?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-Lag-function-and-first/m-p/565350#M158715</link>
      <description>&lt;P&gt;Hello programmers,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am working on an ER injury data.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I consider an injury as 'new' if there were no previous ER visits with an injury within 90 days. Otherwise, it’s a 'followup' visit. All patients are injury free prior to 1998. The data is also such that a non-injury visit cannot occur between two related injury visits.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have been struggling to Mark each injury as being "new" or "followup" and I've also struggled with using&amp;nbsp;an If statement to subset only those records that reflect a new injury.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help is appreciated. Is it possible to use a lag and first. for this? My dataset is attached.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Tue, 11 Jun 2019 18:38:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-Lag-function-and-first/m-p/565350#M158715</guid>
      <dc:creator>ChuksManuel</dc:creator>
      <dc:date>2019-06-11T18:38:07Z</dc:date>
    </item>
    <item>
      <title>Re: Using Lag function and first.?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-Lag-function-and-first/m-p/565366#M158719</link>
      <description>Can you show what you've tried so far?&lt;BR /&gt;&lt;BR /&gt;First would be used to reset statistics/counters, but RETAIN will be a key here. You will also use LAG to get the previous value to test if it's within 90 days, assuming you sort it correctly. But what happens if you have a visit on day 1, day 89 and day 100? Would Day 100 be a follow up or new visit?&lt;BR /&gt;&lt;BR /&gt;It would help if you could provide a smaller example data set directly here and show your expected output as well.</description>
      <pubDate>Tue, 11 Jun 2019 19:22:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-Lag-function-and-first/m-p/565366#M158719</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-06-11T19:22:17Z</dc:date>
    </item>
    <item>
      <title>Re: Using Lag function and first.?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-Lag-function-and-first/m-p/565385#M158724</link>
      <description>&lt;P&gt;data Analysis;&lt;BR /&gt;set ERTeachinghosp;&lt;BR /&gt;by id;&lt;BR /&gt;laginjury = lag(injury);&lt;BR /&gt;if not first.id then PrevInjury = laginjury;&lt;BR /&gt;drop laginjury;&lt;BR /&gt;run;&lt;BR /&gt;proc print; run;&lt;/P&gt;&lt;P&gt;data Analysis2;&lt;BR /&gt;set Analysis;&lt;BR /&gt;if lag(visitdate)- visitdate = 90 then laginjury = 'new';&lt;BR /&gt;else laginjury = 'Follow-up';&lt;BR /&gt;run;&lt;BR /&gt;proc print; run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to&lt;/P&gt;&lt;P&gt;1. subset my injuries : (an injury is new if there were no previous visits with an injury within 90 days..otherwise it's a follow up)&lt;/P&gt;&lt;P&gt;2. Be able to mark each injury as being "new" or "follow up".&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I ran this code and it showed me that all my cases are follow-ups.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 11 Jun 2019 20:14:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-Lag-function-and-first/m-p/565385#M158724</guid>
      <dc:creator>ChuksManuel</dc:creator>
      <dc:date>2019-06-11T20:14:44Z</dc:date>
    </item>
    <item>
      <title>Re: Using Lag function and first.?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-Lag-function-and-first/m-p/565386#M158725</link>
      <description>And this situation:&lt;BR /&gt;But what happens if you have a visit on day 1, day 89 and day 100? Would Day 100 be a follow up or new visit?</description>
      <pubDate>Tue, 11 Jun 2019 20:16:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-Lag-function-and-first/m-p/565386#M158725</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-06-11T20:16:50Z</dc:date>
    </item>
    <item>
      <title>Re: Using Lag function and first.?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-Lag-function-and-first/m-p/565393#M158727</link>
      <description>&lt;DIV&gt;If the person presents to the ER 90 days after their last visit with an injury, then the injury is new.&lt;/DIV&gt;&lt;DIV&gt;So a visit on day 100 (with an injury) would be "new" assuming the patient visited on day 1.&lt;/DIV&gt;</description>
      <pubDate>Tue, 11 Jun 2019 20:37:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-Lag-function-and-first/m-p/565393#M158727</guid>
      <dc:creator>ChuksManuel</dc:creator>
      <dc:date>2019-06-11T20:37:13Z</dc:date>
    </item>
    <item>
      <title>Re: Using Lag function and first.?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-Lag-function-and-first/m-p/565394#M158728</link>
      <description>Jan 1st = Day 1 = injury&lt;BR /&gt;Jan 31st = Day 31 = follow up&lt;BR /&gt;March 15 = Day 70 (approx) = follow up&lt;BR /&gt;April 8th = Day 100 (approx) = follow up or new??</description>
      <pubDate>Tue, 11 Jun 2019 20:40:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-Lag-function-and-first/m-p/565394#M158728</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-06-11T20:40:15Z</dc:date>
    </item>
    <item>
      <title>Re: Using Lag function and first.?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-Lag-function-and-first/m-p/565397#M158730</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What does the injury 0/1 values stand for in the data set?&lt;/P&gt;
&lt;P&gt;And those are what type of dates? They look like date time but are all in 1960 so I'm guessing those are off for some reason?&lt;/P&gt;
&lt;P&gt;Not sure what they reflect.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your code isn't far off, so this is my last answer here, which assumes visit is your visit date. This also assumes any previous visit within 90 days is the same from the last visit, not the original visit.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;

by id;

length type $20.;

prev_visit = lag(visit);

if first.id then call missing(prev_visit);

if visit - prev_visit &amp;lt;= 90 then type='Follow Up';
else type = 'New';

run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 11 Jun 2019 20:54:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-Lag-function-and-first/m-p/565397#M158730</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-06-11T20:54:13Z</dc:date>
    </item>
    <item>
      <title>Re: Using Lag function and first.?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-Lag-function-and-first/m-p/565405#M158733</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here's what i'm trying to do&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have provided 3 scenarios&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;observation 1:&lt;/P&gt;&lt;P&gt;Visit =1 (day 1) , Injury =Yes, Injury= New&lt;/P&gt;&lt;P&gt;Visit=2, Injury= No; follow up&lt;/P&gt;&lt;P&gt;Visit=3, injury= No; follow up&lt;/P&gt;&lt;P&gt;Visit=4, injury=No; follow up&lt;/P&gt;&lt;P&gt;Visit=5 injury=No; follow up&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Observation 2;&lt;/P&gt;&lt;P&gt;Visit =1(Day1) , Injury =Yes, Injury= New&lt;/P&gt;&lt;P&gt;Visit=2, Injury=No; follow up&lt;/P&gt;&lt;P&gt;Visit=3, injury=No; follow up&lt;/P&gt;&lt;P&gt;Visit=4 (Day 100) , injury= Yes, Injury= New&lt;/P&gt;&lt;P&gt;Visit=5 injury=No; follow up&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;observation 3;&lt;/P&gt;&lt;P&gt;Visit =1 (day 1) , Injury =Yes, Injury= New&lt;/P&gt;&lt;P&gt;Visit=2, Injury=No ; follow up&lt;/P&gt;&lt;P&gt;Visit=3 (day 70), injury=Yes; Injury= 'follow up"&lt;/P&gt;&lt;P&gt;Visit=4, injury=No; follow up&lt;/P&gt;&lt;P&gt;Visit=5 injury=No; follow up&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 11 Jun 2019 21:04:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-Lag-function-and-first/m-p/565405#M158733</guid>
      <dc:creator>ChuksManuel</dc:creator>
      <dc:date>2019-06-11T21:04:44Z</dc:date>
    </item>
    <item>
      <title>Re: Using Lag function and first.?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-Lag-function-and-first/m-p/565408#M158736</link>
      <description>&lt;P&gt;&amp;nbsp;I'm asking about this situation, which will occur:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;observation 3;&lt;/P&gt;
&lt;P&gt;Visit =1 (day 1) , Injury =Yes, Injury= New&lt;/P&gt;
&lt;P&gt;Visit=2, Injury=No ; follow up&lt;/P&gt;
&lt;P&gt;Visit=3 (day 70), injury=Yes; Injury= 'follow up"&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;Visit=4, (day 100) injury=Yes; Injury = "Follow up" or "new"&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;Visit=5 injury=No; follow up&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 11 Jun 2019 21:16:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-Lag-function-and-first/m-p/565408#M158736</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-06-11T21:16:59Z</dc:date>
    </item>
    <item>
      <title>Re: Using Lag function and first.?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-Lag-function-and-first/m-p/565413#M158739</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It would be NEW. Because the patient is presenting after 90 days (day 100) with Injury. Injury is coded as 1= yes, 0=No.&lt;/P&gt;&lt;P&gt;If the patient however presented on day 70 with injury(injury=1), it would not be new but a "follow up".&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 11 Jun 2019 21:23:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-Lag-function-and-first/m-p/565413#M158739</guid>
      <dc:creator>ChuksManuel</dc:creator>
      <dc:date>2019-06-11T21:23:01Z</dc:date>
    </item>
    <item>
      <title>Re: Using Lag function and first.?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-Lag-function-and-first/m-p/565415#M158741</link>
      <description>Ok, then the code I provided will not work, because you need to retain the date of the first injury.</description>
      <pubDate>Tue, 11 Jun 2019 21:28:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-Lag-function-and-first/m-p/565415#M158741</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-06-11T21:28:08Z</dc:date>
    </item>
    <item>
      <title>Re: Using Lag function and first.?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-Lag-function-and-first/m-p/565423#M158747</link>
      <description>&lt;P&gt;Yes. I was thinking about using a retain function but where and how it will go is what i found difficult.&lt;/P&gt;</description>
      <pubDate>Tue, 11 Jun 2019 21:59:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-Lag-function-and-first/m-p/565423#M158747</guid>
      <dc:creator>ChuksManuel</dc:creator>
      <dc:date>2019-06-11T21:59:57Z</dc:date>
    </item>
    <item>
      <title>Re: Using Lag function and first.?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-Lag-function-and-first/m-p/565424#M158748</link>
      <description>&lt;P&gt;You're only setting it on each new injury then.&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;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
by id date;

retain prev_inj_date;&lt;BR /&gt;length type $20.;

if first.id then call missing(prev_inj_date);

if injury = 1 and missing(prev_inj_date) then prev_inj_date = date;

if injury = 0 then type='follow up';

if injury =1 and date - prev_inj_date &amp;lt;90 then type = 'follow up';
else do;
type = 'New';
prev_inj_date = date;
end;

run;&lt;BR /&gt;&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 11 Jun 2019 22:08:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-Lag-function-and-first/m-p/565424#M158748</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-06-11T22:08:07Z</dc:date>
    </item>
    <item>
      <title>Re: Using Lag function and first.?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-Lag-function-and-first/m-p/567802#M159722</link>
      <description>Thank you. It worked!</description>
      <pubDate>Thu, 20 Jun 2019 22:59:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-Lag-function-and-first/m-p/567802#M159722</guid>
      <dc:creator>ChuksManuel</dc:creator>
      <dc:date>2019-06-20T22:59:16Z</dc:date>
    </item>
  </channel>
</rss>

