<?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 To keep only observations within 4 months of less of each other in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-do-To-keep-only-observations-within-4-months-of-less-of-each/m-p/715906#M221166</link>
    <description>&lt;P&gt;This problem is more tractable if your claims have some sort of ID (i.e. there are no duplicates) Then you can use simple SQL, like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID  Diag   Date:anydtdte.;
claimId + 1; /* Make the claims distinct */
format date yymmdd10.;
datalines; 
1   1   2012/3
1   2   2012/5
1   2   2012/5
1   2   2012/5
1   1   2013/4
1   1   2014/5
1   1   2014/6
1   2   2014/10
2   1   2012/6
2   1   2013/6
2   1   2014/6
2   1   2014/8
2   1   2015/6
3   1   2013/5
3   1   2013/6
3   2   2013/8
3   1   2015/2
3   2   2015/8
;

proc sql;
create table want as
(select * from have as a where diag=1 and 
    exists (select * from have where diag=2 and id=a.id and intck("month", a.date, date) between 0 and 4))
union
(select * from have as b where diag=2 and 
    exists (select * from have where diag=1 and id=b.id and intck("month", date, b.date) between 0 and 4))
order by id, date, diag;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 01 Feb 2021 22:50:41 GMT</pubDate>
    <dc:creator>PGStats</dc:creator>
    <dc:date>2021-02-01T22:50:41Z</dc:date>
    <item>
      <title>How do To keep only observations within 4 months of less of each other</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-To-keep-only-observations-within-4-months-of-less-of-each/m-p/715883#M221159</link>
      <description>&lt;P&gt;Hello everyone!&lt;BR /&gt;&lt;SPAN style="font-family: inherit; -webkit-tap-highlight-color: transparent; -webkit-text-size-adjust: 100%;"&gt;I am working with a large dataset of medical claims trying to see how many of my patients are getting skin cancer treated within 4 months of diagnosis. So for each patient, I only want to keep observations that contain biopsied lesion code (1) and skin cancer code (2) if they are within 4 months of each other. Here’s an example of the data set:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: inherit; -webkit-tap-highlight-color: transparent; -webkit-text-size-adjust: 100%;"&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Patient ID&lt;/TD&gt;&lt;TD&gt;Diagnosis&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/TD&gt;&lt;TD&gt;Date&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;2012/3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;2012/5&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;2012/5&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;2012/5&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;2013/4&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;2014/5&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt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015/8&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: inherit; -webkit-tap-highlight-color: transparent; -webkit-text-size-adjust: 100%;"&gt;The final data set would look something like this&lt;/SPAN&gt;&lt;SPAN style="font-family: inherit; -webkit-tap-highlight-color: transparent; -webkit-text-size-adjust: 100%;"&gt;: &lt;/SPAN&gt;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Patient ID&lt;/TD&gt;&lt;TD&gt;Diagnosis&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/TD&gt;&lt;TD&gt;Date&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;2012/3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;2012/5&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;2012/5&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;2012/5&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;2014/6&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;2014/10&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;2013/5&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;2013/6&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;2013/8&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: inherit; -webkit-tap-highlight-color: transparent; -webkit-text-size-adjust: 100%;"&gt;Not all biopsied lesions are connected to a skin cancer, which is why I am having difficulty dropping unwanted observations.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: inherit; -webkit-tap-highlight-color: transparent; -webkit-text-size-adjust: 100%;"&gt;If anyone could help write this code, I would really appreciate it! Thank you.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 01 Feb 2021 20:20:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-To-keep-only-observations-within-4-months-of-less-of-each/m-p/715883#M221159</guid>
      <dc:creator>kbanders</dc:creator>
      <dc:date>2021-02-01T20:20:53Z</dc:date>
    </item>
    <item>
      <title>Re: How do To keep only observations within 4 months of less of each other</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-To-keep-only-observations-within-4-months-of-less-of-each/m-p/715903#M221164</link>
      <description>&lt;P&gt;This is a good example for:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Reading each patient_id's data twice.&amp;nbsp; The first time to record use the diagnosis/year/month in hand to record a list of acceptable combinations.&amp;nbsp; If the current diagnosis is 1, then step forward to record acceptable combinations for diagnosis 2.&amp;nbsp; If it is a 2, the step backward to record acceptable combinations of diagnosis 1.&lt;BR /&gt;&lt;BR /&gt;The second pass through the patient_id compares the diagnosis/year/month in-hand to the recorded list of acceptable combinations.&lt;BR /&gt;&lt;BR /&gt;&lt;/LI&gt;
&lt;LI&gt;Using a 3-dimensional array (called KEEP below) to record the keep dummies from #1 above.&amp;nbsp; The dimensions would be diagnosis {1 through 2}, year {2012 through 2019} and month {1 through 12}.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=_:);
  array keep {1:2,2012:2019,1:12} _temporary_;
  call missing(of keep{*});

  do until (last.patient_id);
    set have;
    by patient_id;

    /*if diagnosis=1 then the month increment is +1, otherwise it is -1 */
    _increment=ifn(diagnosis=1,1,-1); 
    /*if diagnosis=1 then set keep dummies for plane=2 (diagnosis 2), otherwise set for plane=1 */
    _plane=3-diagnosis;

    /* For each incoming record, set dummies for the complementary diagnosis */
    do _i=0 to 4;
      keep{_plane,year,month}=1;
      month=month+_increment;&lt;BR /&gt;      /* If month is out of range, the re-set it, and also increment the year */
      if not (1 &amp;lt;= month &amp;lt;= 12)  then do;
        year=year + _increment;
        if month=13 then month=1;
        else month=12;
      end;
    end;
  end;

  /* Now re-read the patient_id and output records with corresponding keep dummies */
  do until (last.patient_id);
    set have;
    by patient_id;
    if keep{diagnosis,year,month}=1 then output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You didn't provide the starting data in the format of a working data step, so I don't offer validation of this program.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Notice I set the lower and upper bounds of the 2nd dimension of the array to accommodate a year range from 2012 to 2019.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Edited addition:&lt;/P&gt;
&lt;P&gt;I assume the data are sorted by ID&amp;nbsp; (though not necessarily date year/month within id).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Although the data step reads each patient_id twice, it is NOT twice the amount of input disk activity, because the recently read observations will have been cached in memory when retrieved for the second pass.&lt;/P&gt;</description>
      <pubDate>Mon, 01 Feb 2021 22:34:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-To-keep-only-observations-within-4-months-of-less-of-each/m-p/715903#M221164</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2021-02-01T22:34:56Z</dc:date>
    </item>
    <item>
      <title>Re: How do To keep only observations within 4 months of less of each other</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-To-keep-only-observations-within-4-months-of-less-of-each/m-p/715906#M221166</link>
      <description>&lt;P&gt;This problem is more tractable if your claims have some sort of ID (i.e. there are no duplicates) Then you can use simple SQL, like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID  Diag   Date:anydtdte.;
claimId + 1; /* Make the claims distinct */
format date yymmdd10.;
datalines; 
1   1   2012/3
1   2   2012/5
1   2   2012/5
1   2   2012/5
1   1   2013/4
1   1   2014/5
1   1   2014/6
1   2   2014/10
2   1   2012/6
2   1   2013/6
2   1   2014/6
2   1   2014/8
2   1   2015/6
3   1   2013/5
3   1   2013/6
3   2   2013/8
3   1   2015/2
3   2   2015/8
;

proc sql;
create table want as
(select * from have as a where diag=1 and 
    exists (select * from have where diag=2 and id=a.id and intck("month", a.date, date) between 0 and 4))
union
(select * from have as b where diag=2 and 
    exists (select * from have where diag=1 and id=b.id and intck("month", date, b.date) between 0 and 4))
order by id, date, diag;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 01 Feb 2021 22:50:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-To-keep-only-observations-within-4-months-of-less-of-each/m-p/715906#M221166</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2021-02-01T22:50:41Z</dc:date>
    </item>
    <item>
      <title>Re: How do To keep only observations within 4 months of less of each other</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-To-keep-only-observations-within-4-months-of-less-of-each/m-p/718723#M222460</link>
      <description>&lt;P&gt;Thank you both for your help!!&lt;/P&gt;</description>
      <pubDate>Thu, 11 Feb 2021 19:33:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-To-keep-only-observations-within-4-months-of-less-of-each/m-p/718723#M222460</guid>
      <dc:creator>kbanders</dc:creator>
      <dc:date>2021-02-11T19:33:26Z</dc:date>
    </item>
  </channel>
</rss>

