<?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: Identifing date of encounters in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Identifing-date-of-encounters/m-p/867635#M342680</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/19879"&gt;@Quentin&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Yes,&amp;nbsp;&lt;SPAN&gt;&amp;nbsp;I want a&amp;nbsp;rule for how to handle&amp;nbsp;the missingness. I dont want to delete them&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 02 Apr 2023 12:04:22 GMT</pubDate>
    <dc:creator>CathyVI</dc:creator>
    <dc:date>2023-04-02T12:04:22Z</dc:date>
    <item>
      <title>Identifing date of encounters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identifing-date-of-encounters/m-p/867357#M342556</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;I have a large dataset but i have provided a sample of the data, my coding and expected output. My code is not giving me the expected result so I need guidance.&lt;/P&gt;&lt;P&gt;I am working on dates variable, I want to generate the date of mri and date of biopsy.Here is a sample:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data have;&lt;BR /&gt;input pt_id $ date:mmddyy10. HCPCS month_diag year_diag BIOP MRI MRI_BIOP ;&lt;BR /&gt;format date mmddyy10.;&lt;BR /&gt;datalines;&lt;BR /&gt;A 09/01/2016 12345 04 2017 0 1 0&lt;BR /&gt;A 01/01/2017 12345 04 2017 0 1 0&lt;BR /&gt;A 03/01/2017 55700 04 2017 1 0 0&lt;/P&gt;&lt;P&gt;A 03/05/2017 55700 04 2017 1 0 0&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1. When biop=1, I want to generate the date_biops closest to but pre/in diagnosis month/year i.e the date of biosp will be the last date closest to the diagnosis month/year&lt;/P&gt;&lt;P&gt;2. When mri=1, I want to generate&amp;nbsp;the date of mri closest to but pre biopsy date.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is how I want my output to look like.&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;pt_id&lt;/TD&gt;&lt;TD&gt;Month_diag&lt;/TD&gt;&lt;TD&gt;year_diag&lt;/TD&gt;&lt;TD&gt;dt_biop&lt;/TD&gt;&lt;TD&gt;dt_mri&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;TD&gt;April&lt;/TD&gt;&lt;TD&gt;2017&lt;/TD&gt;&lt;TD&gt;3/5/2017&lt;/TD&gt;&lt;TD&gt;1/1/2017&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is my attempted code:&lt;/P&gt;&lt;P&gt;Data want;&lt;BR /&gt;set have;&lt;BR /&gt;Date_code = catx('/',year_diag,Month_diag);&lt;BR /&gt;by pt_id;&lt;BR /&gt;first_biop=first.biop;&lt;BR /&gt;last_biop=last.biop;&lt;/P&gt;&lt;P&gt;retain DT_BIOP;&lt;BR /&gt;if first.biop then call missing (DT_BIOP);&lt;/P&gt;&lt;P&gt;if date &amp;lt;= Date_code and not missing(BIOP) then DT_BIOP=date;&lt;BR /&gt;else if missing(DT_BIOP) then biop_2 =0;&lt;/P&gt;&lt;P&gt;format DT_BIOP yymmdd10.;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;Thank you in advance&lt;/P&gt;</description>
      <pubDate>Thu, 30 Mar 2023 21:20:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identifing-date-of-encounters/m-p/867357#M342556</guid>
      <dc:creator>CathyVI</dc:creator>
      <dc:date>2023-03-30T21:20:54Z</dc:date>
    </item>
    <item>
      <title>Re: Identifing date of encounters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identifing-date-of-encounters/m-p/867358#M342557</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks for taking the time to post a clear question, with a description, sample data, and the code you have tried.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This code doesn't give you exactly the output you want, but I think the logic should work for you.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The logic is:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Data are sorted by patient and encounter date&lt;/LI&gt;
&lt;LI&gt;Create a diagnosis date (last day of the diagnosis month).&lt;/LI&gt;
&lt;LI&gt;For each patient, read through the encounters (in chronological order).&amp;nbsp; If an encounter date is before the diagnosis date, then set biopsy date or mri date to the encounter date.&lt;/LI&gt;
&lt;LI&gt;Output one record per patient.&lt;/LI&gt;
&lt;/UL&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (keep=pt_id DiagDate dt_biop dt_mri);
  set have ;
  by pt_id date ;

  *Make a diagnosis date that is the last day of the month of diagnosis ;
  *Its helpful to have a date variable ;

  DiagDate=intnx('month',mdy(month_diag,1,year_diag),0,'e') ;

  retain dt_biop dt_mri ;

  if first.pt_id then do ;
    call missing(dt_biop,dt_mri) ;
  end ;

  if date &amp;lt;= DiagDate then do ;
    if biop=1 then dt_biop=date ;
    if mri=1  then dt_mri=date ;
  end ;

  if last.pt_id ;

  format DiagDate dt_biop dt_mri date9. ;
run ;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 30 Mar 2023 22:14:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identifing-date-of-encounters/m-p/867358#M342557</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2023-03-30T22:14:21Z</dc:date>
    </item>
    <item>
      <title>Re: Identifing date of encounters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identifing-date-of-encounters/m-p/867365#M342563</link>
      <description>&lt;P&gt;&lt;U&gt;&lt;EM&gt;Below code gives you the output as required -&lt;/EM&gt;&lt;/U&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;*sort data by patient id and date;&lt;BR /&gt;proc sort data= have; by pt_id date; run;&lt;/P&gt;&lt;P&gt;*create temporary table to format date_code &amp;amp; assign first and last biop indicators;&lt;BR /&gt;data _temp_want1_;&lt;BR /&gt;set have;&lt;BR /&gt;by pt_id date;&lt;BR /&gt;format date date9.;&lt;/P&gt;&lt;P&gt;if Month_diag &amp;lt;= 9 then&lt;BR /&gt;do;&lt;BR /&gt;_temp_Date_code = catx('/',('0'||strip(substr((put(Month_diag,$2.)),1,2))),'01',year_diag);&lt;BR /&gt;end;&lt;BR /&gt;else if Month_diag &amp;gt; 9 then&lt;BR /&gt;do;&lt;BR /&gt;_temp_Date_code = catx('/',(strip(substr((put(Month_diag,$2.)),1,2))),'01',year_diag);&lt;BR /&gt;end;&lt;/P&gt;&lt;P&gt;_temp_Date_code2 = input(_temp_Date_code, MMDDYY10.);&lt;BR /&gt;Date_code = _temp_Date_code2;&lt;BR /&gt;format Date_code date9.;&lt;BR /&gt;drop _temp_Date_code _temp_Date_code2;&lt;/P&gt;&lt;P&gt;If first.pt_id then&lt;BR /&gt;do;&lt;BR /&gt;first_biop = biop;&lt;BR /&gt;end;&lt;/P&gt;&lt;P&gt;If last.pt_id then&lt;BR /&gt;do;&lt;BR /&gt;last_biop = biop;&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;*inner table: calculate latest date pre diagnosis date to use in dt_biop assignment;&lt;BR /&gt;*outer table t2: join inner table to first temporary table to assign temporary dt_biop;&lt;BR /&gt;proc sql;&lt;BR /&gt;create table _temp_want_biop_dte_join as&lt;BR /&gt;select t1.*&lt;BR /&gt;, t2. temp_dt_biop&lt;BR /&gt;from _temp_want1_ t1&lt;BR /&gt;left join (select pt_id&lt;BR /&gt;, date&lt;BR /&gt;, case&lt;BR /&gt;when max(date) = date and date &amp;lt; date_code then date&lt;BR /&gt;else . end as temp_dt_biop format=date9.&lt;BR /&gt;from _temp_want1_&lt;BR /&gt;group by pt_id&lt;BR /&gt;having biop = 1 and max(date) = date) t2&lt;BR /&gt;on t1.pt_id = t2.pt_id&lt;BR /&gt;and t1.date = t2.date;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;*assign temporary dt_biopsy across all rows and rename to the final dt_biopsy field;&lt;BR /&gt;proc sql;&lt;BR /&gt;create table _temp_want_biop_dte_join_max as&lt;BR /&gt;select *&lt;BR /&gt;, max(temp_dt_biop) as dt_biop format=date9.&lt;BR /&gt;from _temp_want_biop_dte_join&lt;BR /&gt;group by pt_id;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;*inner table t1: select all data where mri = 1&lt;BR /&gt;*inner table t1: from table t1, group by patient id to select the latest date where mri = 1 and date is less than biopsy date;&lt;BR /&gt;*outer table: select only patient id, date and temporary mri date to join on to full table on the next steps;&lt;BR /&gt;proc sql;&lt;BR /&gt;create table _temp_want_mri_dte as&lt;BR /&gt;select t2.pt_id&lt;BR /&gt;, t2.date&lt;BR /&gt;, t2.temp_dt_mri&lt;BR /&gt;from (select t1.*&lt;BR /&gt;, case&lt;BR /&gt;when max(t1.date) = date and date &amp;lt; dt_biop then date&lt;BR /&gt;else . end as temp_dt_mri format=date9.&lt;BR /&gt;from (select *&lt;BR /&gt;from _temp_want_biop_dte_join_max&lt;BR /&gt;where mri = 1) t1&lt;BR /&gt;group by t1.pt_id) t2&lt;BR /&gt;where t2.temp_dt_mri = t2.date;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;*join temprary mri date onto full data for the same date as where mri = 1 &amp;amp; date is less than biopsy date;&lt;BR /&gt;proc sql;&lt;BR /&gt;create table _temp_want_mri_dte_join as&lt;BR /&gt;select t1.*&lt;BR /&gt;, t2.temp_dt_mri&lt;BR /&gt;from _temp_want_biop_dte_join_max t1&lt;BR /&gt;left join _temp_want_mri_dte t2&lt;BR /&gt;on t1.pt_id = t2.pt_id&lt;BR /&gt;and t1.date = t2.date;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;*fill in temporary mri date across all rows in full data;&lt;BR /&gt;proc sql;&lt;BR /&gt;create table _Temp_want_max_mri_dte as&lt;BR /&gt;select *&lt;BR /&gt;, max(temp_dt_mri) as dt_mri format=date9.&lt;BR /&gt;from _temp_want_mri_dte_join&lt;BR /&gt;group by pt_id;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;*select single row per patient id with required fields &amp;amp; formats;&lt;BR /&gt;proc sql;&lt;BR /&gt;create table want as&lt;BR /&gt;select distinct pt_id&lt;BR /&gt;, put(date_code, monname3.) as month_diag&lt;BR /&gt;, year(date_code) as year_diag&lt;BR /&gt;, dt_biop format=MMDDYY10.&lt;BR /&gt;, dt_mri format=MMDDYY10.&lt;BR /&gt;from _Temp_want_max_mri_dte;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;/*delete temporary tables*/&lt;BR /&gt;proc sql;&lt;BR /&gt;drop table _temp_want1_;&lt;BR /&gt;drop table _temp_want_biop_dte_join;&lt;BR /&gt;drop table _temp_want_biop_dte_join_max;&lt;BR /&gt;drop table _temp_want_mri_dte;&lt;BR /&gt;drop table _temp_want_mri_dte_join;&lt;BR /&gt;drop table _Temp_want_max_mri_dte;&lt;BR /&gt;quit;&lt;/P&gt;</description>
      <pubDate>Fri, 31 Mar 2023 00:54:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identifing-date-of-encounters/m-p/867365#M342563</guid>
      <dc:creator>Modeller</dc:creator>
      <dc:date>2023-03-31T00:54:29Z</dc:date>
    </item>
    <item>
      <title>Re: Identifing date of encounters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identifing-date-of-encounters/m-p/867509#M342633</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/19879"&gt;@Quentin&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/440906"&gt;@Modeller&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;One thing I forgot to mention is that I have missing observations within the date variables. When I ran the code the new variables dt_biop and dt_mri was missing. How do I resolved the issue?&lt;/P&gt;</description>
      <pubDate>Fri, 31 Mar 2023 17:08:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identifing-date-of-encounters/m-p/867509#M342633</guid>
      <dc:creator>CathyVI</dc:creator>
      <dc:date>2023-03-31T17:08:54Z</dc:date>
    </item>
    <item>
      <title>Re: Identifing date of encounters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identifing-date-of-encounters/m-p/867527#M342637</link>
      <description>&lt;P&gt;Do you mean you have cases where the variable DATE is missing?&amp;nbsp; For my code, you could change:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  if date &amp;lt;= DiagDate then do ;
    if biop=1 then dt_biop=date ;
    if mri=1  then dt_mri=date ;
  end ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;to:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  if .Z &amp;lt; date &amp;lt;= DiagDate then do ;
    if biop=1 then dt_biop=date ;
    if mri=1  then dt_mri=date ;
  end ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;That will ignore events where the DATE is missing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Unrelated, I just noticed that your specification is:&lt;/P&gt;
&lt;P&gt;1. When biop=1, I want to generate the date_biops closest to but pre/in diagnosis month/year&lt;/P&gt;
&lt;P&gt;2. When mri=1, I want to generate&amp;nbsp;the date of mri closest to but pre/in&lt;STRONG&gt; biopsy date.&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I didn't do #2 like you want.&amp;nbsp; I generated date_mri as the date closest to but pre/in the diagnosis month.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 31 Mar 2023 17:48:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identifing-date-of-encounters/m-p/867527#M342637</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2023-03-31T17:48:28Z</dc:date>
    </item>
    <item>
      <title>Re: Identifing date of encounters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identifing-date-of-encounters/m-p/867530#M342638</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/19879"&gt;@Quentin&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/440906"&gt;@Modeller&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I recreated the file with missing data but when I ran your new code the out was still missing.&lt;/P&gt;&lt;P&gt;data have;&lt;BR /&gt;input pt_id $ date:mmddyy10. HCPCS month_diag year_diag BIOP MRI MRI_BIOP ;&lt;BR /&gt;format date mmddyy10.;&lt;BR /&gt;datalines;&lt;BR /&gt;A 09/01/2016 12345 04 2017 0 1 0&lt;BR /&gt;A 01/01/2017 12345 04 2017 0 1 0&lt;BR /&gt;A . 55700 04 2017 1 0 0&lt;BR /&gt;A . 55700 04 2017 1 0 0&lt;BR /&gt;B 09/02/2007 99999 08 2007 0 1 0&lt;BR /&gt;B 06/13/2007 99999 08 2007 0 1 0&lt;BR /&gt;B 07/28/2007 23456 08 2007 0 1 0&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sort data = have; by pt_ID date ;proc print; run;&lt;BR /&gt;data want (keep=pt_id DiagDate dt_biop dt_mri);&lt;BR /&gt;set have ;&lt;BR /&gt;by pt_id date ;&lt;/P&gt;&lt;P&gt;*Make a diagnosis date that is the last day of the month of diagnosis ;&lt;BR /&gt;*Its helpful to have a date variable ;&lt;/P&gt;&lt;P&gt;DiagDate= mdy(month_diag,15,year_diag) ;&lt;/P&gt;&lt;P&gt;retain dt_biop dt_mri ;&lt;/P&gt;&lt;P&gt;if first.pt_id then do ;&lt;BR /&gt;call missing(dt_biop,dt_mri) ;&lt;BR /&gt;end ;&lt;/P&gt;&lt;P&gt;if .Z &amp;lt; date &amp;lt;= DiagDate then do ;&lt;BR /&gt;if biop=1 then dt_biop=date ;&lt;BR /&gt;if mri=1 then dt_mri=date ;&lt;BR /&gt;end ;&lt;/P&gt;&lt;P&gt;if last.pt_id ;&lt;/P&gt;&lt;P&gt;format DiagDate dt_biop dt_mri date9. ;&lt;BR /&gt;run ;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 31 Mar 2023 18:00:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identifing-date-of-encounters/m-p/867530#M342638</guid>
      <dc:creator>CathyVI</dc:creator>
      <dc:date>2023-03-31T18:00:01Z</dc:date>
    </item>
    <item>
      <title>Re: Identifing date of encounters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identifing-date-of-encounters/m-p/867573#M342657</link>
      <description>&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;dt_biop is missing for patient A because even though the patient had two biopsies, you don't know the date of the biopsy.&lt;/P&gt;
&lt;P&gt;dt_biop is missing for patient B because they didn't have a biopsy.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;(If I'm understanding your data correctly).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Did you want to add a new rule for how to handle these possibilities?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;-Q.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 31 Mar 2023 21:11:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identifing-date-of-encounters/m-p/867573#M342657</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2023-03-31T21:11:02Z</dc:date>
    </item>
    <item>
      <title>Re: Identifing date of encounters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identifing-date-of-encounters/m-p/867635#M342680</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/19879"&gt;@Quentin&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Yes,&amp;nbsp;&lt;SPAN&gt;&amp;nbsp;I want a&amp;nbsp;rule for how to handle&amp;nbsp;the missingness. I dont want to delete them&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 02 Apr 2023 12:04:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identifing-date-of-encounters/m-p/867635#M342680</guid>
      <dc:creator>CathyVI</dc:creator>
      <dc:date>2023-04-02T12:04:22Z</dc:date>
    </item>
    <item>
      <title>Re: Identifing date of encounters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identifing-date-of-encounters/m-p/867646#M342687</link>
      <description>&lt;P&gt;What rule would you want when the event date is missing, or there are no events? &amp;nbsp;I don't think I'm understanding your goal.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What output would you want for this sample data you shared?:&lt;/P&gt;
&lt;PRE&gt;data have;
input pt_id $ date:mmddyy10. HCPCS month_diag year_diag BIOP MRI MRI_BIOP ;
format date mmddyy10.;
datalines;
A 09/01/2016 12345 04 2017 0 1 0
A 01/01/2017 12345 04 2017 0 1 0
A . 55700 04 2017 1 0 0
A . 55700 04 2017 1 0 0
B 09/02/2007 99999 08 2007 0 1 0
B 06/13/2007 99999 08 2007 0 1 0
B 07/28/2007 23456 08 2007 0 1 0
;
run;&lt;/PRE&gt;</description>
      <pubDate>Sun, 02 Apr 2023 15:53:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identifing-date-of-encounters/m-p/867646#M342687</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2023-04-02T15:53:22Z</dc:date>
    </item>
    <item>
      <title>Re: Identifing date of encounters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identifing-date-of-encounters/m-p/867653#M342692</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/19879"&gt;@Quentin&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1. When the event date is missing and I ran your code, the date of biopsy were all missing(.) No date was generated so I want to fix that by addressing the missing dates.&lt;/P&gt;&lt;P&gt;2. From the task the&amp;nbsp;&lt;SPAN&gt;DT_MRI = date of MRI closest to but &lt;STRONG&gt;pre biopsy date NOT &lt;/STRONG&gt;diagnosis date&lt;STRONG&gt;. &lt;/STRONG&gt;How can I address this issue? Thanks&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 02 Apr 2023 19:16:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identifing-date-of-encounters/m-p/867653#M342692</guid>
      <dc:creator>CathyVI</dc:creator>
      <dc:date>2023-04-02T19:16:15Z</dc:date>
    </item>
    <item>
      <title>Re: Identifing date of encounters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identifing-date-of-encounters/m-p/867661#M342693</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Let's focus on DT_BIOP first.&amp;nbsp; We agree that the sample code I shared will result in DT_BIOP being missing when the biopsy dates are all missing, or if there is no biopsy.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input pt_id $ date:mmddyy10. HCPCS month_diag year_diag BIOP MRI MRI_BIOP ;
format date mmddyy10.;
datalines;
A 09/01/2016 12345 04 2017 0 1 0
A 01/01/2017 12345 04 2017 0 1 0
A . 55700 04 2017 1 0 0
A . 55700 04 2017 1 0 0
B 09/02/2007 99999 08 2007 0 1 0
B 06/13/2007 99999 08 2007 0 1 0
B 07/28/2007 23456 08 2007 0 1 0
;
run;

proc sort data=have ;
  by pt_id date ;
run ;

data want (keep=pt_id DiagDate dt_biop dt_mri);
  set have ;
  by pt_id date ;

  *Make a diagnosis date that is the last day of the month of diagnosis ;
  *Its helpful to have a date variable ;

  DiagDate=intnx('month',mdy(month_diag,1,year_diag),0,'e') ;

  retain dt_biop dt_mri ;

  if first.pt_id then do ;
    call missing(dt_biop,dt_mri) ;
  end ;

  if .Z &amp;lt; date &amp;lt;= DiagDate then do ;
    if biop=1 then dt_biop=date ;
    if mri=1  then dt_mri=date ;
  end ;

  if last.pt_id ;

  format DiagDate dt_biop dt_mri date9. ;
run ;

proc print data=want ;
  var pt_id DiagDate dt_biop ;
run ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results in:&lt;/P&gt;
&lt;PRE&gt;Obs    pt_id     DiagDate      dt_biop

 1       A      30APR2017            .
 2       B      31AUG2007            .&lt;/PRE&gt;
&lt;P&gt;It sounds like you don't want dt_biop to be missing for these two patients.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What value would you want dt_biop to have for pt_Id A?&lt;/P&gt;
&lt;P&gt;What value would you want dt_biop to have for pt_Id B?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 02 Apr 2023 20:01:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identifing-date-of-encounters/m-p/867661#M342693</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2023-04-02T20:01:37Z</dc:date>
    </item>
    <item>
      <title>Re: Identifing date of encounters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identifing-date-of-encounters/m-p/867664#M342694</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/19879"&gt;@Quentin&lt;/a&gt;&amp;nbsp;Yes, you are right.&lt;/P&gt;&lt;P&gt;What value would you want dt_biop to have for pt_Id A?&lt;/P&gt;&lt;P&gt;What value would you want dt_biop to have for pt_Id B?&lt;/P&gt;&lt;P&gt;For pt_Id A &amp;amp; B, I want the dt_biop to be&amp;nbsp;&amp;nbsp;&lt;SPAN&gt;DT_BIOP = date of biopsy closest to but &amp;lt;=&amp;nbsp;&lt;/SPAN&gt;DiagDate.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 02 Apr 2023 20:36:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identifing-date-of-encounters/m-p/867664#M342694</guid>
      <dc:creator>CathyVI</dc:creator>
      <dc:date>2023-04-02T20:36:25Z</dc:date>
    </item>
    <item>
      <title>Re: Identifing date of encounters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identifing-date-of-encounters/m-p/867665#M342695</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/253321"&gt;@CathyVI&lt;/a&gt;&amp;nbsp;sorry, I don' think we're communicating well.&amp;nbsp; You said:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/19879"&gt;@Quentin&lt;/a&gt;&lt;SPAN&gt;&amp;nbsp;Yes, you are right.&lt;/SPAN&gt;
&lt;P&gt;What value would you want dt_biop to have for pt_Id A?&lt;/P&gt;
&lt;P&gt;What value would you want dt_biop to have for pt_Id B?&lt;/P&gt;
&lt;P&gt;For pt_Id A &amp;amp; B, I want the dt_biop to be&amp;nbsp;&amp;nbsp;&lt;SPAN&gt;DT_BIOP = date of biopsy closest to but &amp;lt;=&amp;nbsp;&lt;/SPAN&gt;DiagDate.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You created the example data:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input pt_id $ date:mmddyy10. HCPCS month_diag year_diag BIOP MRI MRI_BIOP ;
format date mmddyy10.;
datalines;
A 09/01/2016 12345 04 2017 0 1 0
A 01/01/2017 12345 04 2017 0 1 0
A . 55700 04 2017 1 0 0
A . 55700 04 2017 1 0 0
B 09/02/2007 99999 08 2007 0 1 0
B 06/13/2007 99999 08 2007 0 1 0
B 07/28/2007 23456 08 2007 0 1 0
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;For that specific example data, what value would you want dt_biop to have for pt_Id A?&amp;nbsp;&amp;nbsp;what value would you want dt_biop to have for pt_Id B?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think missing is the correct answer.&amp;nbsp; But you seem to want a different answer.&amp;nbsp; What specific value (date) would you want for pt_ID A and pt_ID B?&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 02 Apr 2023 20:48:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identifing-date-of-encounters/m-p/867665#M342695</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2023-04-02T20:48:37Z</dc:date>
    </item>
    <item>
      <title>Re: Identifing date of encounters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identifing-date-of-encounters/m-p/867669#M342696</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/19879"&gt;@Quentin&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;From the data it is true that missing date is there but for&amp;nbsp;&lt;SPAN&gt;pt_Id A&amp;nbsp; I want 01/01/2017 because this is the nearest date to the DiagDate.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;For&amp;nbsp;pt_Id B, I want 07/28/2007 because this is the closest/nearest date to or on the DiagDate.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 02 Apr 2023 20:59:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identifing-date-of-encounters/m-p/867669#M342696</guid>
      <dc:creator>CathyVI</dc:creator>
      <dc:date>2023-04-02T20:59:39Z</dc:date>
    </item>
    <item>
      <title>Re: Identifing date of encounters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identifing-date-of-encounters/m-p/867672#M342697</link>
      <description>&lt;P&gt;Pt_ID A did not have a biopsy on 01/01/2017, they had an MRI on that date.&amp;nbsp; You want to set Dt_Biop to the date of the MRI?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That logic needs to be clarified.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Are you saying that if a patient did not have any biopsies prior to the diagnosis date, then Dt_Biop should be set to the date of latest MRI prior to the diagnosis date?&amp;nbsp; That seems confusing.&lt;/P&gt;</description>
      <pubDate>Sun, 02 Apr 2023 21:07:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identifing-date-of-encounters/m-p/867672#M342697</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2023-04-02T21:07:33Z</dc:date>
    </item>
    <item>
      <title>Re: Identifing date of encounters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identifing-date-of-encounters/m-p/867675#M342698</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/19879"&gt;@Quentin&lt;/a&gt;&amp;nbsp;Am sorry to confuse you. You are right initially.&lt;/P&gt;&lt;P&gt;Please this is the right decision.&lt;/P&gt;&lt;P&gt;When biops =1, that is&amp;nbsp;&lt;SPAN&gt;if a patient have any biopsies prior to the diagnosis date, then Dt_Biop should be set to the latest visit date prior to the diagnosis date.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 02 Apr 2023 21:43:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identifing-date-of-encounters/m-p/867675#M342698</guid>
      <dc:creator>CathyVI</dc:creator>
      <dc:date>2023-04-02T21:43:32Z</dc:date>
    </item>
    <item>
      <title>Re: Identifing date of encounters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identifing-date-of-encounters/m-p/867676#M342699</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/253321"&gt;@CathyVI&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/19879"&gt;@Quentin&lt;/a&gt;&amp;nbsp;Am sorry to confuse you. You are right initially.&lt;/P&gt;
&lt;P&gt;Please this is the right decision.&lt;/P&gt;
&lt;P&gt;When biops =1, that is&amp;nbsp;&lt;SPAN&gt;if a patient have any biopsies prior to the diagnosis date, then Dt_Biop should be set to the latest visit date prior to the diagnosis date.&lt;/SPAN&gt;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Okay, so PT_ID B has no biopsies.&amp;nbsp; The patient has no records with Biops=1. What value would you want for Dt_Biop?&lt;/P&gt;
&lt;P&gt;PT_ID A has two biopsies, but you don't know the dates of the biopsies.&amp;nbsp;&amp;nbsp;What value would you want for Dt_Biop?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 02 Apr 2023 21:48:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identifing-date-of-encounters/m-p/867676#M342699</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2023-04-02T21:48:49Z</dc:date>
    </item>
    <item>
      <title>Re: Identifing date of encounters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identifing-date-of-encounters/m-p/867677#M342700</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/19879"&gt;@Quentin&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In this scenario,&amp;nbsp;&lt;SPAN&gt;PT_ID A&amp;nbsp;will be missing because no records with Biops=1. However, when I ran it in the larger data, I would want value for Dt_Biop when Biops =1.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 02 Apr 2023 21:57:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identifing-date-of-encounters/m-p/867677#M342700</guid>
      <dc:creator>CathyVI</dc:creator>
      <dc:date>2023-04-02T21:57:31Z</dc:date>
    </item>
    <item>
      <title>Re: Identifing date of encounters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identifing-date-of-encounters/m-p/867678#M342701</link>
      <description>&lt;P&gt;Ok, then is the code I posted giving you the correct result you want for Dt_Biop?&lt;/P&gt;</description>
      <pubDate>Sun, 02 Apr 2023 21:59:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identifing-date-of-encounters/m-p/867678#M342701</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2023-04-02T21:59:29Z</dc:date>
    </item>
    <item>
      <title>Re: Identifing date of encounters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identifing-date-of-encounters/m-p/867679#M342702</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/19879"&gt;@Quentin&lt;/a&gt;&amp;nbsp;No its not. The entire dt_Biop is missing even for&amp;nbsp;&lt;SPAN&gt;Dt_Biop B&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 02 Apr 2023 22:05:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identifing-date-of-encounters/m-p/867679#M342702</guid>
      <dc:creator>CathyVI</dc:creator>
      <dc:date>2023-04-02T22:05:39Z</dc:date>
    </item>
  </channel>
</rss>

