<?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: Assign the same value to identical IDs with date difference less than 365 days in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Assign-the-same-value-to-identical-IDs-with-date-difference-less/m-p/922426#M41398</link>
    <description>&lt;P&gt;Do you want to only carry forward from previous events, or also backward from future events, if these are nearer?&lt;/P&gt;</description>
    <pubDate>Mon, 01 Apr 2024 06:46:53 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2024-04-01T06:46:53Z</dc:date>
    <item>
      <title>Assign the same value to identical IDs with date difference less than 365 days</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Assign-the-same-value-to-identical-IDs-with-date-difference-less/m-p/922409#M41395</link>
      <description>&lt;P&gt;I would like to replace missing value with closest value from the same DMRN, but the dx_date should be less than 365 days.&lt;/P&gt;
&lt;P&gt;This is how my data look like:&lt;/P&gt;
&lt;P&gt;DMRN&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;dx_date&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;BMI&lt;/P&gt;
&lt;P&gt;30&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 04FEB2013&amp;nbsp; &amp;nbsp; &amp;nbsp; 35.82&lt;/P&gt;
&lt;P&gt;30&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 04JUN2013&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&lt;/P&gt;
&lt;P&gt;30&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 04NOV2014&amp;nbsp; &amp;nbsp; 26.55&lt;/P&gt;
&lt;P&gt;25&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;22JAN2016&amp;nbsp; &amp;nbsp; &amp;nbsp;30.42&lt;/P&gt;
&lt;P&gt;25&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;25MAR2022&amp;nbsp; &amp;nbsp; &amp;nbsp; .&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is how I want it to be;&lt;/P&gt;
&lt;P&gt;DMRN&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; dx_date&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;BMI&lt;/P&gt;
&lt;P&gt;30&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 04FEB2013&amp;nbsp; &amp;nbsp; &amp;nbsp; 35.82&lt;/P&gt;
&lt;P&gt;30&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 04JUN2013&amp;nbsp; &amp;nbsp; &amp;nbsp; 35.82&lt;/P&gt;
&lt;P&gt;30&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 04NOV2013&amp;nbsp; &amp;nbsp; 26.55&lt;/P&gt;
&lt;P&gt;25&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;22JAN2016&amp;nbsp; &amp;nbsp; &amp;nbsp;30.42&lt;/P&gt;
&lt;P&gt;25&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;25MAR2022&amp;nbsp; &amp;nbsp; &amp;nbsp; .&lt;/P&gt;
&lt;P&gt;What codes should I use?&lt;/P&gt;</description>
      <pubDate>Mon, 01 Apr 2024 02:37:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Assign-the-same-value-to-identical-IDs-with-date-difference-less/m-p/922409#M41395</guid>
      <dc:creator>tan-wongv</dc:creator>
      <dc:date>2024-04-01T02:37:43Z</dc:date>
    </item>
    <item>
      <title>Re: Assign the same value to identical IDs with date difference less than 365 days</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Assign-the-same-value-to-identical-IDs-with-date-difference-less/m-p/922422#M41397</link>
      <description>&lt;P&gt;If you don't have a lot of data with a lot of missing values then below should suffice.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
 input DMRN dx_date:date9. BMI;
 format dx_date date9.;
 datalines;
30 04FEB2013 .
30 04MAR2013 99.10
30 03JUN2013 1
30 04JUN2013 .
30 05JUN2013 2
30 06JUN2013 .
30 07JUN2013 .
30 08JUN2013 3
30 04NOV2014 26.55
25 22JAN2016 30.42
25 25MAR2022 .
;

proc sql;
  select 
    l.*,
    case
      when missing(l.bmi) then (select r.bmi
                                from
                                  (
                                    select r.dx_date,r.bmi
                                    from have r 
                                    where r.dmrn=l.dmrn and not missing(r.bmi)
                                    having abs(r.dx_date-l.dx_date)=min(abs(r.dx_date-l.dx_date)) 
                                           and abs(r.dx_date-l.dx_date)&amp;lt;365
                                  ) r
                                  having min(r.dx_date)=r.dx_date
                                )
      else l.bmi
      end as bmi_2
  from have l
  order by l.dmrn, l.dx_date
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you need time series data in the end then consider using Proc Expand instead (requires SAS/ETS licensed).&lt;/P&gt;</description>
      <pubDate>Mon, 01 Apr 2024 05:54:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Assign-the-same-value-to-identical-IDs-with-date-difference-less/m-p/922422#M41397</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2024-04-01T05:54:00Z</dc:date>
    </item>
    <item>
      <title>Re: Assign the same value to identical IDs with date difference less than 365 days</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Assign-the-same-value-to-identical-IDs-with-date-difference-less/m-p/922426#M41398</link>
      <description>&lt;P&gt;Do you want to only carry forward from previous events, or also backward from future events, if these are nearer?&lt;/P&gt;</description>
      <pubDate>Mon, 01 Apr 2024 06:46:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Assign-the-same-value-to-identical-IDs-with-date-difference-less/m-p/922426#M41398</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2024-04-01T06:46:53Z</dc:date>
    </item>
    <item>
      <title>Re: Assign the same value to identical IDs with date difference less than 365 days</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Assign-the-same-value-to-identical-IDs-with-date-difference-less/m-p/922436#M41399</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;pointed out that my original code did not accomodate situations where a DMRN group begins with a missing BMI value.&amp;nbsp; So here is a data step approach that requires presorted the data by DMRN,DX_DATE.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
 input DMRN dx_date:date9. BMI;
 format dx_date date9.;
 datalines;
30 04FEB2013 .
30 04MAR2013 99.10
30 03JUN2013 1
30 04JUN2013 .
30 05JUN2013 2
30 06JUN2013 .
30 07JUN2013 .
30 08JUN2013 3
30 04NOV2014 26.55
25 22JAN2016 30.42
25 25MAR2022 .
;


proc sort data=have;
  by dmrn dx_date;
run;

data non_Missing_windows  /view=non_Missing_windows; 
  set have (rename=(dx_date=nm_dte bmi=nm_bmi) where=(nm_bmi^=.)); /*Read only non-missing BMI's*/
  by dmrn;
  dx_date=nm_dte - ifn(first.dmrn,365,min(365,0.5*dif(nm_dte)));
  format dx_date date9.;
run;

data want (drop=nm_:);
  set have (in=inhave)
      non_missing_windows (in=nmw keep=dmrn dx_date) ;
  by dmrn dx_date;
  if nmw then set non_missing_windows (keep=nm_dte nm_bmi);
  else if first.dmrn then call missing(of nm_:);

  if inhave;
  new_bmi=ifn(bmi=. and dx_date-365&amp;lt;=nm_dte,nm_bmi,bmi);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The code below is the original response, but does not properly process DMRN's that start with a missing BMI:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&lt;STRIKE&gt;data have;
input DMRN         dx_date  :date9.             BMI;
format dx_date date9.;
datalines;
30                04FEB2013      35.82
30                04JUN2013        .
30                04NOV2014      26.55
25                 22JAN2016     30.42
25                 25MAR2022      .
run;

data want (drop=prv_:  nxt_:);
  set have;
  by dmrn notsorted;

  if bmi^=. then do;  /*If non-missing BMI, update PRV_ and NXT_ */
    new_bmi=bmi;
    merge have (firstobs=1 rename=(dmrn=prv_dmrn dx_date=prv_dte bmi=prv_bmi) where=(prv_bmi^=.))
          have (firstobs=2 rename=(dmrn=nxt_dmrn dx_date=nxt_dte bmi=nxt_bmi) where=(nxt_bmi^=.)) ;
  end;
  else do;
    /* Eliminate Non-eligible or ancient history*/
    if prv_dmrn^=dmrn or prv_dte&amp;lt;dx_date-365 then call missing(of prv_:);

    /* Copy PREV, if next is too far or for another DMRN  */
    /* Copy NEXT if it is near enough and PREV is missing */
    if nxt_dmrn ^= dmrn or nxt_dte&amp;gt; dx_date+365 then new_bmi=prv_bmi; 
    else if prv_dte=. and nxt_dmrn=dmrn and nxt_dte &amp;lt;= dx_date+365 then new_bmi=nxt_bmi;

    else do;  /*But if Both prev and next qualify, compare distances */
      prv_nxt_mean_dte=mean(prv_dte,nxt_dte);
      if prv_nxt_mean_dte &amp;gt; dx_date then new_bmi=nxt_bmi;
      else new_bmi=prv_bmi;
    end;
  end;
run;&lt;/STRIKE&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;STRIKE&gt;I've created a variable NEW_BMI so you can see the before and after date step values of BMI.&lt;/STRIKE&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRIKE&gt;The primary tool here is the conditional use of the MERGE statement.&amp;nbsp;&lt;/STRIKE&gt;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;&lt;STRIKE&gt;&amp;nbsp; First, because the MERGE is only run conditionally (i.e. when BMI^=.) the values it reads in are automatically "retained" across subsequent observations that have missing BMI.&amp;nbsp; That is, they are retained until the next MERGE.&lt;/STRIKE&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;STRIKE&gt;Second, one of the merge datasets is the HAVE dataset, but starting at firstobs=2, which allows access to the next non-missing value of BMI.&lt;/STRIKE&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;STRIKE&gt;Note both datasets in the MERGE have qualifying WHERE parameters to ensure that only non-missing BMI values are used for the previous and next values.&lt;/STRIKE&gt;&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRIKE&gt;Then, when the current BMI is missing, one can examine the PRV (previous) and NXT non-missing values to get BMI from the closest date.&amp;nbsp; BTW, if the previous and next dates are equally distant, the code above takes the previous BMI.&lt;/STRIKE&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRIKE&gt;This program assumes that each DMRN is sorted by DX_DATE.&lt;/STRIKE&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 02 Apr 2024 16:09:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Assign-the-same-value-to-identical-IDs-with-date-difference-less/m-p/922436#M41399</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2024-04-02T16:09:43Z</dc:date>
    </item>
    <item>
      <title>Re: Assign the same value to identical IDs with date difference less than 365 days</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Assign-the-same-value-to-identical-IDs-with-date-difference-less/m-p/922482#M41401</link>
      <description>&lt;P&gt;I don't think you have thought through all of the possible combinations of missing BMI values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example if the first observation has a missing value of BMI then the NXT_.... variables are not populated since the MERGE never happens.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Not sure if there are others since I did not test every scenario.&lt;/P&gt;</description>
      <pubDate>Mon, 01 Apr 2024 19:15:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Assign-the-same-value-to-identical-IDs-with-date-difference-less/m-p/922482#M41401</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-04-01T19:15:29Z</dc:date>
    </item>
    <item>
      <title>Re: Assign the same value to identical IDs with date difference less than 365 days</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Assign-the-same-value-to-identical-IDs-with-date-difference-less/m-p/922483#M41402</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I don't think you have thought through all of the possible combinations of missing BMI values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example if the first observation has a missing value of BMI then the NXT_.... variables are not populated since the MERGE never happens.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Not sure if there are others since I did not test every scenario.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You're right.&amp;nbsp; Revision needed.&lt;/P&gt;</description>
      <pubDate>Mon, 01 Apr 2024 19:24:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Assign-the-same-value-to-identical-IDs-with-date-difference-less/m-p/922483#M41402</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2024-04-01T19:24:21Z</dc:date>
    </item>
    <item>
      <title>Re: Assign the same value to identical IDs with date difference less than 365 days</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Assign-the-same-value-to-identical-IDs-with-date-difference-less/m-p/922522#M41403</link>
      <description>Thank you so much for your response!</description>
      <pubDate>Tue, 02 Apr 2024 05:46:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Assign-the-same-value-to-identical-IDs-with-date-difference-less/m-p/922522#M41403</guid>
      <dc:creator>tan-wongv</dc:creator>
      <dc:date>2024-04-02T05:46:16Z</dc:date>
    </item>
    <item>
      <title>Re: Assign the same value to identical IDs with date difference less than 365 days</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Assign-the-same-value-to-identical-IDs-with-date-difference-less/m-p/922523#M41404</link>
      <description>Thank you so much for your help!</description>
      <pubDate>Tue, 02 Apr 2024 05:46:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Assign-the-same-value-to-identical-IDs-with-date-difference-less/m-p/922523#M41404</guid>
      <dc:creator>tan-wongv</dc:creator>
      <dc:date>2024-04-02T05:46:39Z</dc:date>
    </item>
  </channel>
</rss>

