<?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: Create a new variable based on the combination of dates and two other variables in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Create-a-new-variable-based-on-the-combination-of-dates-and-two/m-p/943559#M42393</link>
    <description>&lt;P&gt;Just remember if you have seen any risk or index.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data DB;
  input ID :$20. Admission :date. Discharge :date. Index Risk Expect;
  format admission discharge date9.;
cards;
0001  13JAN2017 25JAN2017   0     1    1
0001  13JAN2017 25JAN2017   0     1    1
0001  22FEB2017 03MAR2017   1     0    1
0001  30JAN2019 04MAR2019   0     0    0
0002  01DEC2018 14DEC2018   0     1    1
0002  25DEC2018 02JAN2019   1     0    1
0002  25NOV2020 03DEC2020   0     1    0
0003  09JAN2016 25JAN2016   1     0    0
0003  29JAN2018 12FEB2018   0     0    0
0004  12APR2015 15APR2015   1     1    1
0004  02JUN2018 04JUN2018   0     0    0
0004  29JAN2021 12FEB2021   0     0    0
;

data db1;
  set db ;
  by id ;
  retain any_index any_risk;
  if first.id then call missing(of any_:);
  new_risk = (risk and not any_index)
          or (index and any_risk)
  ;
  output;
  any_index=index or any_index;
  any_risk=risk or any_risk;
run;

proc print;
run; 

proc compare data=db1 ;
  var new_risk;
  with expect;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1726082492655.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/100243i9606B6F7288B7604/image-size/large?v=v2&amp;amp;px=999" role="button" title="Tom_0-1726082492655.png" alt="Tom_0-1726082492655.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 11 Sep 2024 19:21:45 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2024-09-11T19:21:45Z</dc:date>
    <item>
      <title>Create a new variable based on the combination of dates and two other variables</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Create-a-new-variable-based-on-the-combination-of-dates-and-two/m-p/943537#M42392</link>
      <description>&lt;P&gt;Hi guys,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;suppose to have the following:&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data DB;
  input ID :$20. Admission :date09. Discharge :date09. Index  Risk;
cards;
0001  13JAN2017 25JAN2017   0     1    
0001  13JAN2017 25JAN2017   0     1    
0001  22FEB2017 03MAR2017   1     0    
0001  30JAN2019 04MAR2019   0     0    
0002  01DEC2018 14DEC2018   0     1    
0002  25DEC2018 02JAN2019   1     0    
0002  25NOV2020 03DEC2020   0     1    
0003  09JAN2016 25JAN2016   1     0    
0003  29JAN2018 12FEB2018   0     1    
0004  12APR2015 15APR2015   1     1
0004  02JUN2018 04JUN2018   0     0
0004  29JAN2021 12FEB2021   0     0
;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;In other words a column indicates an Index date while another column if a patient is at risk because of a comorbidity. Is there a way to get the following?&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data DB1;
  input ID :$20. Admission :date09. Discharge :date09. Index  Risk New_Risk;
cards;
0001  13JAN2017 25JAN2017   0     1    1
0001  13JAN2017 25JAN2017   0     1    1
0001  22FEB2017 03MAR2017   1     0    1
0001  30JAN2019 04MAR2019   0     0    0
0002  01DEC2018 14DEC2018   0     1    1
0002  25DEC2018 02JAN2019   1     0    1
0002  25NOV2020 03DEC2020   0     1    0
0003  09JAN2016 25JAN2016   1     0    0
0003  29JAN2018 12FEB2018   0     0    0
0004  12APR2015 15APR2015   1     1    1
0004  02JUN2018 04JUN2018   0     0    0
0004  29JAN2021 12FEB2021   0     0    0
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;In other words:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1) if Risk = 1&amp;nbsp; at dates before Index = 1 then New_risk = 1 at dates before and at Index = 1 (although Risk at Index = 1 is 0). This refers to patient 0001 and patient 0002. For this last patient at date&amp;nbsp;&lt;CODE class=" language-sas"&gt;25NOV2020 03DEC2020,  &lt;/CODE&gt;Risk = 1 but since it is = 1 after Index = 1 New_Risk should be = 0;&lt;/P&gt;
&lt;P&gt;2) if Risk = 0 always, then no rule will be applied. All will stay the same. This refers to patient 0003&lt;/P&gt;
&lt;P&gt;3) if Risk = 1 only at Index = 1 New_risk will be =&amp;nbsp; 1 only at Index = 1 and never after. This refers to patient 0004 that has no dates before Index = 1&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can anyone help me please?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you in advance!&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;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 11 Sep 2024 18:56:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Create-a-new-variable-based-on-the-combination-of-dates-and-two/m-p/943537#M42392</guid>
      <dc:creator>NewUsrStat</dc:creator>
      <dc:date>2024-09-11T18:56:42Z</dc:date>
    </item>
    <item>
      <title>Re: Create a new variable based on the combination of dates and two other variables</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Create-a-new-variable-based-on-the-combination-of-dates-and-two/m-p/943559#M42393</link>
      <description>&lt;P&gt;Just remember if you have seen any risk or index.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data DB;
  input ID :$20. Admission :date. Discharge :date. Index Risk Expect;
  format admission discharge date9.;
cards;
0001  13JAN2017 25JAN2017   0     1    1
0001  13JAN2017 25JAN2017   0     1    1
0001  22FEB2017 03MAR2017   1     0    1
0001  30JAN2019 04MAR2019   0     0    0
0002  01DEC2018 14DEC2018   0     1    1
0002  25DEC2018 02JAN2019   1     0    1
0002  25NOV2020 03DEC2020   0     1    0
0003  09JAN2016 25JAN2016   1     0    0
0003  29JAN2018 12FEB2018   0     0    0
0004  12APR2015 15APR2015   1     1    1
0004  02JUN2018 04JUN2018   0     0    0
0004  29JAN2021 12FEB2021   0     0    0
;

data db1;
  set db ;
  by id ;
  retain any_index any_risk;
  if first.id then call missing(of any_:);
  new_risk = (risk and not any_index)
          or (index and any_risk)
  ;
  output;
  any_index=index or any_index;
  any_risk=risk or any_risk;
run;

proc print;
run; 

proc compare data=db1 ;
  var new_risk;
  with expect;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1726082492655.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/100243i9606B6F7288B7604/image-size/large?v=v2&amp;amp;px=999" role="button" title="Tom_0-1726082492655.png" alt="Tom_0-1726082492655.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 11 Sep 2024 19:21:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Create-a-new-variable-based-on-the-combination-of-dates-and-two/m-p/943559#M42393</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-09-11T19:21:45Z</dc:date>
    </item>
    <item>
      <title>Re: Create a new variable based on the combination of dates and two other variables</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Create-a-new-variable-based-on-the-combination-of-dates-and-two/m-p/943585#M42396</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data DB;
  input ID :$20. Admission :date09. Discharge :date09. Index  Risk;
  format Admission Discharge date9.;
cards;
0001  13JAN2017 25JAN2017   0     1    
0001  13JAN2017 25JAN2017   0     1    
0001  22FEB2017 03MAR2017   1     0    
0001  30JAN2019 04MAR2019   0     0    
0002  01DEC2018 14DEC2018   0     1    
0002  25DEC2018 02JAN2019   1     0    
0002  25NOV2020 03DEC2020   0     1    
0003  09JAN2016 25JAN2016   1     0    
0003  29JAN2018 12FEB2018   0     1    
0004  12APR2015 15APR2015   1     1
0004  02JUN2018 04JUN2018   0     0
0004  29JAN2021 12FEB2021   0     0
;
run;

data want;
do i=1 by 1 until(last.id);
  set DB;
  by id;
  if Index then idx_Index=i;
  if Risk and not found then do;idx_Risk=i;found=1;end;
end;
do j=1 by 1 until(last.id);
  set DB;
  by id;
  New_risk =0;
  if not missing(idx_Risk) and idx_Risk&amp;lt;idx_Index and j&amp;lt;=idx_Index then New_risk=1;
  if not missing(idx_Risk) and idx_Risk=idx_Index and j=idx_Index then New_risk=1;
  output;
end;
drop idx_: i j found;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 12 Sep 2024 01:34:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Create-a-new-variable-based-on-the-combination-of-dates-and-two/m-p/943585#M42396</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2024-09-12T01:34:29Z</dc:date>
    </item>
  </channel>
</rss>

