<?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: Calculating time between dates with conditions of per patient ID and by specific observations in SAS Data Management</title>
    <link>https://communities.sas.com/t5/SAS-Data-Management/Calculating-time-between-dates-with-conditions-of-per-patient-ID/m-p/277482#M7842</link>
    <description>&lt;P&gt;You didn't post output yet.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; 
data test; 
informat ID $3. TESTDATE yymmdd10. AB $1. RNA $1.; 
input ID TESTDATE AB RNA; 
datalines; 
1 2002-07-25 0 0 
1 2003-06-20 1 0 
1 2003-08-17 1 0 
1 2005-01-01 . 1 
2 2003-04-06 0 0 
2 2006-02-14 1 0
2 2007-01-13 1 1 
2 2009-01-04 1 1
3 2008-05-18 0 0
3 2009-02-13 1 0
3 2010-03-10 . 1
;
run;

data want;
do until(last.id);
  set test;
  by id;
  if AB=1 and not found_AB then do;
   TESTDATE_AB=TESTDATE;
   found_AB=1;
  end; 
  if RNA=1 and not found_RNA then do;
   TESTDATE_RNA=TESTDATE;
   found_RNA=1;
  end; 
end;   
  dif=TESTDATE_RNA-TESTDATE_AB;

 drop found_: TESTDATE_:;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 15 Jun 2016 06:13:25 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2016-06-15T06:13:25Z</dc:date>
    <item>
      <title>Calculating time between dates with conditions of per patient ID and by specific observations</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Calculating-time-between-dates-with-conditions-of-per-patient-ID/m-p/277362#M7840</link>
      <description>&lt;P&gt;I am not sure if this is possible but I would like to calculate the time between test dates (TESTDATE) for each unique patient ID (ID) with the first test date being where antibody (AB)=1 (for the first time) and the last test date being where rna &amp;nbsp;(RNA)=1 (for the first time).&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data test;&amp;nbsp;&lt;/P&gt;&lt;P&gt;informat ID $3. TESTDATE yymmdd10. AB $1. RNA $1.;&amp;nbsp;&lt;/P&gt;&lt;P&gt;input ID TESTDATE AB RNA;&amp;nbsp;&lt;/P&gt;&lt;P&gt;datalines;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1 2002-07-25 0 0&amp;nbsp;&lt;/P&gt;&lt;P&gt;1 2003-06-20 1 0&amp;nbsp;&lt;/P&gt;&lt;P&gt;1 2003-08-17 1 0&amp;nbsp;&lt;/P&gt;&lt;P&gt;1 2005-01-01 . 1&amp;nbsp;&lt;/P&gt;&lt;P&gt;2 2003-04-06 0 0&amp;nbsp;&lt;/P&gt;&lt;P&gt;2 2006-02-14 1 0&lt;/P&gt;&lt;P&gt;2 2007-01-13 1 1&amp;nbsp;&lt;/P&gt;&lt;P&gt;2 2009-01-04 1 1&lt;/P&gt;&lt;P&gt;3 2008-05-18 0 0&lt;/P&gt;&lt;P&gt;3 2009-02-13 1 0&lt;/P&gt;&lt;P&gt;3 2010-03-10 . 1&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is just a sample of my data to get an idea of what I mean. I hope someone can help me! Thank you in advance &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 14 Jun 2016 19:52:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Calculating-time-between-dates-with-conditions-of-per-patient-ID/m-p/277362#M7840</guid>
      <dc:creator>mphqueens</dc:creator>
      <dc:date>2016-06-14T19:52:27Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating time between dates with conditions of per patient ID and by specific observations</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Calculating-time-between-dates-with-conditions-of-per-patient-ID/m-p/277367#M7841</link>
      <description>&lt;P&gt;See if this gets you started:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data want;
   set test;
   by id;
   retain abflag rnaflag abdate rnadate;
   if first.id then do;
      abflag=0;
      rnaflag=0;
      abdate =.;
      rnadate=.;
   end;
   if abflag=0 and ab='1' then do;
      abdate=testdate;
      abflag=1;
   end;
   if rnaflag=0 and rna='1' then do;
      rnadate=testdate;
      rnaflag=1;
      betweendates = rnadate-abdate;
   end;
   format testdate abdate rnadate yymmdd10.;
   drop abflag rnaflag;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You could add an output statement after the betweendates assignment to just output one record per id.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 14 Jun 2016 20:11:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Calculating-time-between-dates-with-conditions-of-per-patient-ID/m-p/277367#M7841</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-06-14T20:11:33Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating time between dates with conditions of per patient ID and by specific observations</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Calculating-time-between-dates-with-conditions-of-per-patient-ID/m-p/277482#M7842</link>
      <description>&lt;P&gt;You didn't post output yet.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; 
data test; 
informat ID $3. TESTDATE yymmdd10. AB $1. RNA $1.; 
input ID TESTDATE AB RNA; 
datalines; 
1 2002-07-25 0 0 
1 2003-06-20 1 0 
1 2003-08-17 1 0 
1 2005-01-01 . 1 
2 2003-04-06 0 0 
2 2006-02-14 1 0
2 2007-01-13 1 1 
2 2009-01-04 1 1
3 2008-05-18 0 0
3 2009-02-13 1 0
3 2010-03-10 . 1
;
run;

data want;
do until(last.id);
  set test;
  by id;
  if AB=1 and not found_AB then do;
   TESTDATE_AB=TESTDATE;
   found_AB=1;
  end; 
  if RNA=1 and not found_RNA then do;
   TESTDATE_RNA=TESTDATE;
   found_RNA=1;
  end; 
end;   
  dif=TESTDATE_RNA-TESTDATE_AB;

 drop found_: TESTDATE_:;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 15 Jun 2016 06:13:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Calculating-time-between-dates-with-conditions-of-per-patient-ID/m-p/277482#M7842</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-06-15T06:13:25Z</dc:date>
    </item>
  </channel>
</rss>

