<?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: Flag Last Assay Date Prior to Exposure Date in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Flag-Last-Assay-Date-Prior-to-Exposure-Date/m-p/530367#M145017</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/155121"&gt;@mglogan&lt;/a&gt;&amp;nbsp; Here is a test with taking sample as numeric sas dates as yymmdd10.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input SUBJECT   ASSAY_NAME   &amp;amp; $40.                   ASSAY_DATE  : yymmdd10.        EXPOSURE_DATE : yymmdd10.;  
format 	ASSAY_DATE EXPOSURE_DATE yymmdd10.;
cards;
001             Alanine Aminotransferase     2017-07-05T12:01    2017-07-19T09:55
001             Alanine Aminotransferase     2017-07-19T09:22    2017-07-19T09:55     Y
001             Albumin                                 2017-08-15T09:38    2017-07-19T09:55
001             Albumin                                 2017-08-30T09:09    2017-07-19T09:55
001             Albumin                                 2017-07-15T08:10    2017-07-19T09:55 
;

proc sql;
create table want(drop=dif) as
select *, ifn(EXPOSURE_DATE-ASSAY_DATE&amp;lt;0,.,EXPOSURE_DATE-ASSAY_DATE) as dif,ifc(min(calculated dif)=calculated dif,'Y',' ') as want
from have
group by subject,ASSAY_NAME	;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Feel free to to use an &lt;STRONG&gt;order by clause&lt;/STRONG&gt; after&lt;STRONG&gt; group by&lt;/STRONG&gt; to get the order the way want.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sat, 26 Jan 2019 20:19:49 GMT</pubDate>
    <dc:creator>novinosrin</dc:creator>
    <dc:date>2019-01-26T20:19:49Z</dc:date>
    <item>
      <title>Flag Last Assay Date Prior to Exposure Date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Flag-Last-Assay-Date-Prior-to-Exposure-Date/m-p/530358#M145010</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need to create a new variable that flags last assay date prior to exposure date.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;it seems simple, but I am unsure of the best method?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;can you provide some suggestions?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;thank you in advanced!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Sample data:&lt;/P&gt;&lt;P&gt;SUBJECT&amp;nbsp; &amp;nbsp;ASSAY_NAME&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ASSAY_DATE&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; EXPOSURE_DATE&amp;nbsp; &amp;nbsp;WANT: BASELINE&lt;/P&gt;&lt;P&gt;001&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Alanine Aminotransferase&amp;nbsp; &amp;nbsp; &amp;nbsp;2017-07-05T12:01&amp;nbsp; &amp;nbsp; 2017-07-19T09:55&lt;BR /&gt;001&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Alanine Aminotransferase&amp;nbsp; &amp;nbsp; &amp;nbsp;2017-07-19T09:22&amp;nbsp; &amp;nbsp; 2017-07-19T09:55&amp;nbsp; &amp;nbsp; &amp;nbsp;Y&lt;BR /&gt;001&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Albumin&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2017-08-15T09:38&amp;nbsp; &amp;nbsp; 2017-07-19T09:55&lt;BR /&gt;001&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Albumin&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2017-08-30T09:09&amp;nbsp; &amp;nbsp; 2017-07-19T09:55&lt;BR /&gt;001&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Albumin&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2017-07-15T08:10&amp;nbsp; &amp;nbsp; 2017-07-19T09:55&amp;nbsp; &amp;nbsp; &amp;nbsp;Y&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 26 Jan 2019 19:38:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Flag-Last-Assay-Date-Prior-to-Exposure-Date/m-p/530358#M145010</guid>
      <dc:creator>mglogan</dc:creator>
      <dc:date>2019-01-26T19:38:15Z</dc:date>
    </item>
    <item>
      <title>Re: Flag Last Assay Date Prior to Exposure Date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Flag-Last-Assay-Date-Prior-to-Exposure-Date/m-p/530362#M145012</link>
      <description>&lt;P&gt;Are your dates numeric SAS dates?&lt;/P&gt;</description>
      <pubDate>Sat, 26 Jan 2019 19:51:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Flag-Last-Assay-Date-Prior-to-Exposure-Date/m-p/530362#M145012</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-01-26T19:51:14Z</dc:date>
    </item>
    <item>
      <title>Re: Flag Last Assay Date Prior to Exposure Date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Flag-Last-Assay-Date-Prior-to-Exposure-Date/m-p/530364#M145014</link>
      <description>&lt;P&gt;The logic is very simple&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Let me know if you can understand the following&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want(drop=dif) as
select *, ifn(EXPOSURE_DATE-ASSAY_DATE&amp;lt;0,.,EXPOSURE_DATE-ASSAY_DATE) as dif,ifc(min(calculated dif)=calculated dif,'Y',' ') as want
from have
group by subject,ASSAY_NAME	;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 26 Jan 2019 20:16:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Flag-Last-Assay-Date-Prior-to-Exposure-Date/m-p/530364#M145014</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-01-26T20:16:13Z</dc:date>
    </item>
    <item>
      <title>Re: Flag Last Assay Date Prior to Exposure Date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Flag-Last-Assay-Date-Prior-to-Exposure-Date/m-p/530365#M145015</link>
      <description>&lt;P&gt;Hmm so, the dates are character format in this example. I have a previous date vale in sas numeric format but not the time. However, I can create numeric sas date time values.&lt;/P&gt;</description>
      <pubDate>Sat, 26 Jan 2019 20:16:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Flag-Last-Assay-Date-Prior-to-Exposure-Date/m-p/530365#M145015</guid>
      <dc:creator>mglogan</dc:creator>
      <dc:date>2019-01-26T20:16:47Z</dc:date>
    </item>
    <item>
      <title>Re: Flag Last Assay Date Prior to Exposure Date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Flag-Last-Assay-Date-Prior-to-Exposure-Date/m-p/530366#M145016</link>
      <description>&lt;P&gt;Here is an simple way.&amp;nbsp; Just sort the data by descending date and flag the first one in the group.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have ;
  by SUBJECT ASSAY_NAME    EXPOSURE_DATE  descending ASSAY_DATE ;
run;

data want ;
  set have;
  by SUBJECT ASSAY_NAME    EXPOSURE_DATE ;
  if first.expose_date then BASELINE='Y';
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 26 Jan 2019 20:19:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Flag-Last-Assay-Date-Prior-to-Exposure-Date/m-p/530366#M145016</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-01-26T20:19:30Z</dc:date>
    </item>
    <item>
      <title>Re: Flag Last Assay Date Prior to Exposure Date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Flag-Last-Assay-Date-Prior-to-Exposure-Date/m-p/530367#M145017</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/155121"&gt;@mglogan&lt;/a&gt;&amp;nbsp; Here is a test with taking sample as numeric sas dates as yymmdd10.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input SUBJECT   ASSAY_NAME   &amp;amp; $40.                   ASSAY_DATE  : yymmdd10.        EXPOSURE_DATE : yymmdd10.;  
format 	ASSAY_DATE EXPOSURE_DATE yymmdd10.;
cards;
001             Alanine Aminotransferase     2017-07-05T12:01    2017-07-19T09:55
001             Alanine Aminotransferase     2017-07-19T09:22    2017-07-19T09:55     Y
001             Albumin                                 2017-08-15T09:38    2017-07-19T09:55
001             Albumin                                 2017-08-30T09:09    2017-07-19T09:55
001             Albumin                                 2017-07-15T08:10    2017-07-19T09:55 
;

proc sql;
create table want(drop=dif) as
select *, ifn(EXPOSURE_DATE-ASSAY_DATE&amp;lt;0,.,EXPOSURE_DATE-ASSAY_DATE) as dif,ifc(min(calculated dif)=calculated dif,'Y',' ') as want
from have
group by subject,ASSAY_NAME	;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Feel free to to use an &lt;STRONG&gt;order by clause&lt;/STRONG&gt; after&lt;STRONG&gt; group by&lt;/STRONG&gt; to get the order the way want.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 26 Jan 2019 20:19:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Flag-Last-Assay-Date-Prior-to-Exposure-Date/m-p/530367#M145017</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-01-26T20:19:49Z</dc:date>
    </item>
    <item>
      <title>Re: Flag Last Assay Date Prior to Exposure Date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Flag-Last-Assay-Date-Prior-to-Exposure-Date/m-p/530368#M145018</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/155121"&gt;@mglogan&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hmm so, the dates are character format in this example. I have a previous date vale in sas numeric format but not the time. However, I can create numeric sas date time values.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;If the dates are as in your post it doesn't matter if they are valid datetime values with an ISO format attached to make them print like that or just character strings in that ISO format. Either way they will sort the same since the lexical ordering of that string is the same as the numeric ordering of the dates they represent.&lt;/P&gt;</description>
      <pubDate>Sat, 26 Jan 2019 20:21:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Flag-Last-Assay-Date-Prior-to-Exposure-Date/m-p/530368#M145018</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-01-26T20:21:24Z</dc:date>
    </item>
    <item>
      <title>Re: Flag Last Assay Date Prior to Exposure Date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Flag-Last-Assay-Date-Prior-to-Exposure-Date/m-p/530383#M145020</link>
      <description>&lt;P&gt;So I noticed that I will need to include time in the calculation, since there are assays performed on the day of exposure but prior to exposure time. In this case can I use the raw sas datetime value or do I need&amp;nbsp; to apply a datetime format?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;thank you!&lt;/P&gt;</description>
      <pubDate>Sat, 26 Jan 2019 22:02:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Flag-Last-Assay-Date-Prior-to-Exposure-Date/m-p/530383#M145020</guid>
      <dc:creator>mglogan</dc:creator>
      <dc:date>2019-01-26T22:02:24Z</dc:date>
    </item>
    <item>
      <title>Re: Flag Last Assay Date Prior to Exposure Date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Flag-Last-Assay-Date-Prior-to-Exposure-Date/m-p/530384#M145021</link>
      <description>&lt;P&gt;Thank for the quick response! I'll try this method too.&lt;/P&gt;</description>
      <pubDate>Sat, 26 Jan 2019 22:05:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Flag-Last-Assay-Date-Prior-to-Exposure-Date/m-p/530384#M145021</guid>
      <dc:creator>mglogan</dc:creator>
      <dc:date>2019-01-26T22:05:57Z</dc:date>
    </item>
    <item>
      <title>Re: Flag Last Assay Date Prior to Exposure Date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Flag-Last-Assay-Date-Prior-to-Exposure-Date/m-p/530387#M145022</link>
      <description>&lt;P&gt;No need to apply format. As long as your date or datetime value are numbers, the code will comfortably work&lt;/P&gt;</description>
      <pubDate>Sat, 26 Jan 2019 22:46:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Flag-Last-Assay-Date-Prior-to-Exposure-Date/m-p/530387#M145022</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-01-26T22:46:07Z</dc:date>
    </item>
    <item>
      <title>Re: Flag Last Assay Date Prior to Exposure Date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Flag-Last-Assay-Date-Prior-to-Exposure-Date/m-p/530391#M145023</link>
      <description>&lt;P&gt;If your data are sorted by subject/assay_name/assay_date, the a single data step can do what you need:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=nxt_:);
  set have (keep=subject assay_name);
  by subject assay_name ;

  merge have
        have (firstobs=2 keep=assay_date rename=(assay_date=nxt_assay_date));

  if assay_date&amp;lt;exposure_date and
     (last.assay_name=1 or nxt_assay_date&amp;gt;exposure_date)
  then flag='Y';
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;OL style="list-style-position: inside;"&gt;
&lt;LI&gt;&amp;nbsp;The set statement only reads the BY variables&amp;nbsp; subject and assay_name.&amp;nbsp; This provides the automatic dummy variable last.assay_name which is used later to test whether the record-in-hand is the last record for that subject/assay_name combination.&lt;/LI&gt;
&lt;LI&gt;&amp;nbsp;The MERGE statement provide a way to simultaneously read the current record and the next record (the assay_date variable only, renamed to nxt_assay_date).&amp;nbsp; This allows a test to determine whether the current record is the last one with assay_date &amp;lt; exposure_date.&lt;/LI&gt;
&lt;/OL&gt;</description>
      <pubDate>Sat, 26 Jan 2019 23:43:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Flag-Last-Assay-Date-Prior-to-Exposure-Date/m-p/530391#M145023</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2019-01-26T23:43:28Z</dc:date>
    </item>
    <item>
      <title>Re: Flag Last Assay Date Prior to Exposure Date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Flag-Last-Assay-Date-Prior-to-Exposure-Date/m-p/531445#M145449</link>
      <description>&lt;P&gt;&lt;SPAN style="font-size: 12.0pt; font-family: 'Times New Roman',serif; color: #073763;"&gt;Hello everyone, thank you for your suggestions.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 12.0pt; font-family: 'Times New Roman',serif; color: #073763;"&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 12.0pt; font-family: 'Times New Roman',serif; color: #073763;"&gt;After looking at my data, it turns out that I will need to flag baseline records by subject, test (i.e. glucose, albumin etc.) and category (chemistry, hematology, urinalysis, serology etc.), Date values are in character format.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 12.0pt; font-family: 'Times New Roman',serif; color: #073763;"&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 12.0pt; font-family: 'Times New Roman',serif; color: #073763;"&gt;My output sample data is attached. The code I am working on is below, but it doesn't quite do what I want. It is flagging all dates prior to exposure by test category, but I only need the latest (or first.lbcat), and I have not been able to get it to flag correctly.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data lbflag; 
  set _lbflag;
  by usubjid lbtest lbcat descending lbdtc;
  retain flag;
  if first.lbtest then flag=0;

  *** flag test dates prior to exposure ;
  if flag=0 and lbdtc &amp;lt;= rfxstdtc and lbstat ~='NOT DONE' then flag=1 ; 

  *** if test is prior to exposure, flag by lbcat ;
  if flag=1 then do;
  retain _flag ;
  if first.lbcat then _flag=0;
   if lbdtc &amp;lt;= rfxstdtc
	then do ;
	 _flag=1 ;
	 lbblfl = 'Y' ;
        end;
     end;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 30 Jan 2019 17:35:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Flag-Last-Assay-Date-Prior-to-Exposure-Date/m-p/531445#M145449</guid>
      <dc:creator>mglogan</dc:creator>
      <dc:date>2019-01-30T17:35:59Z</dc:date>
    </item>
  </channel>
</rss>

