<?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: Time to event censoring calculation, please advise in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Time-to-event-censoring-calculation-please-advise/m-p/319426#M70151</link>
    <description>&lt;P&gt;I have this code but- I need to collapse multiple rows for each ID into a single row with needed variables.&lt;/P&gt;&lt;P&gt;Please advise...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data y ;&lt;BR /&gt;set x;&lt;BR /&gt;by id;&lt;BR /&gt;retain phase pos_d neg_d ;&lt;BR /&gt;changed_result =0; flag=0; /* initial value */&lt;BR /&gt;if first.id then phase = 0;&lt;BR /&gt;if phase = 0 and result= 'Positive' then do; pos_d = t0; phase=1; end;&lt;BR /&gt;if phase = 1 and &lt;SPAN&gt;result&lt;/SPAN&gt; in ('','unknown') then do; neg_d = t0; phase=2;&lt;SPAN&gt;diff &lt;/SPAN&gt;= neg_d - pos_d; end;&lt;BR /&gt;if phase = 1 and &lt;SPAN&gt;result&lt;/SPAN&gt; in ('Negative') then do; neg_d = t0; phase =2;&amp;nbsp;&lt;SPAN&gt;diff &lt;/SPAN&gt;= neg_d - pos_d; &lt;SPAN&gt;changed_result&lt;/SPAN&gt; = 1;end;&lt;BR /&gt;else do; diff = .; &lt;SPAN&gt;changed_result&lt;/SPAN&gt; = 0; flag=1; end;&lt;BR /&gt;if last.id and phase=0 then neg=1;&lt;BR /&gt;*output;&lt;BR /&gt;keep id pos_d neg_d diff&amp;nbsp;&lt;SPAN&gt;changed_result&amp;nbsp;flag neg;&lt;/SPAN&gt;&lt;BR /&gt;run;&lt;/P&gt;</description>
    <pubDate>Fri, 16 Dec 2016 02:10:33 GMT</pubDate>
    <dc:creator>ahhh</dc:creator>
    <dc:date>2016-12-16T02:10:33Z</dc:date>
    <item>
      <title>Time to event censoring calculation, please advise</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Time-to-event-censoring-calculation-please-advise/m-p/319423#M70150</link>
      <description>&lt;P&gt;I need help with this algorithm:&lt;/P&gt;&lt;P&gt;CALCULATING THE TIME BETWEEN THE FIRST POSITIVE TO HOW EVER LONG THEY REMAIN POSITIVE (BEFORE HAVING A NEGATIVE OR UNKNOWN/MISSING RESULT)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID VISIT T0 RESULT&lt;BR /&gt;1 V1 07SEP2005 negative&lt;BR /&gt;1 v2 06oct2005 negative&lt;BR /&gt;1 v3 01apr2006 positive&lt;BR /&gt;1 v4 03sep2006 negative&lt;BR /&gt;1 v5 08sep2006 negative&lt;BR /&gt;2 v1 07sep2005 negative&lt;BR /&gt;2 v2 02oct2005 positive&lt;BR /&gt;2 v3 05nov2005 positive&lt;BR /&gt;2 v4 10nov2006 negative&lt;BR /&gt;3 v1 07SEP2005 negative&lt;BR /&gt;3 v2 03OCT2005 positive&lt;BR /&gt;3 v3 09nov2005&lt;BR /&gt;3 v4 10dec2005 negative&lt;BR /&gt;3 v5 03mar2006 positive&lt;BR /&gt;4 v1 07SEP2005 negative&lt;BR /&gt;4 v2 10SEP2005 negative&lt;BR /&gt;4 v3 07OCT2005 negative&lt;BR /&gt;4 v4 17nOV2005 negative&lt;BR /&gt;4 v5 27JAN2006 negative&lt;BR /&gt;5 v1 03mar2007 positive&lt;BR /&gt;5 v2 30mar2007 positive&lt;BR /&gt;5 v3 06jun2007 positive&lt;BR /&gt;5 v4 13mar2008 positive&lt;BR /&gt;6 v1 03feb2007 positive&lt;BR /&gt;6 v2 30jun2007 UNKNOWN&lt;BR /&gt;6 v3 06nov2007&lt;BR /&gt;6 v4 03mar2008 positive&lt;BR /&gt;7 v1 01jan2007 positive&lt;BR /&gt;7 v2 30jun2007 positive&lt;BR /&gt;7 v3 08nov2007&lt;BR /&gt;7 v4 01feb2008 positive&lt;BR /&gt;8 v1 07aug2005 negative&lt;BR /&gt;8 v2 07oct2005 negative&lt;BR /&gt;8 v3 15nov2005 positive&lt;BR /&gt;8 v4 01jan2006&lt;BR /&gt;9 v1 07jan2005 negative&lt;BR /&gt;9 v2 10mar2005 positive&lt;BR /&gt;9 v3 27OCT2005 UNKNOWN&lt;BR /&gt;9 v4 07dec2005 negative&lt;BR /&gt;9 v5 20JAN2006 negative&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;diff=t1-t2&lt;BR /&gt;BASICALLY WE FOLLOW FIRST POSITIVE RESULT UNTIL 1.)THERE IS A NEGATIVE RESULT OR 2.) TILL THE LAST IF THEY NEVER BECOME NEGATIVES OR 3.) THE NEXT RESULT IS MISSING OR UNKNOWN&lt;BR /&gt;&lt;BR /&gt;1.) if an id has all negative results (and no positives) it has to be flagged (flag=1)(example: Id 4),&lt;BR /&gt;2.) if an id has all positive results (and no negatives) it has to be flagged (flag=2)(example: Id 5),&lt;BR /&gt;and diff= date of first positive minus date of last positive and changed_result=0&lt;BR /&gt;3.) if the results are like this-- 1- POSITIVE&lt;BR /&gt;2- POSITIVE&lt;BR /&gt;3- POSITIVE&lt;BR /&gt;4- (MISSING VALUE)&lt;BR /&gt;5- POSITIVE&lt;BR /&gt;then diff= dates of first positive and 3 rd positive need to be subtracted and changed_result=0&lt;BR /&gt;(similar situation when results are&lt;BR /&gt;1- POSITIVE&lt;BR /&gt;2- POSITIVE&lt;BR /&gt;3- (MISSING VALUE)&lt;BR /&gt;4- POSITIVE&lt;BR /&gt;then diff= dates of first positive and 2 rd positive need to be subtracted and changed_result=0&lt;BR /&gt;In other words, whenever resulTS are in (positive, missing) then diff=dates of first positive and the positive result right before the missing value&lt;BR /&gt;need to be subtracted and changed_result=0&lt;BR /&gt;4.) if the result are like this-- 1-negative&lt;BR /&gt;2-negative&lt;BR /&gt;3-positive&lt;BR /&gt;4-missing&lt;BR /&gt;then diff= missing value (cuz we have nothing to compare it to) and changed_result=0&lt;BR /&gt;In other words, if the second element while calculating diff is a missing value, then diff=missing and changed_result=0&lt;BR /&gt;5.)'unknown' values in result var should be treated same as missing values&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Number of visits may vary for each id and sometimes 'results' might be missing/unknown (treat both the same way).&lt;BR /&gt;For each unique ID:&lt;BR /&gt;6.) When there is 'positive' test result, we need to look for next consecutive 'negative' result and- we need to calculate the difference between the dates of the first positive result and the date of the first negative result after the first positive result and a new variable 'changed_result' is set to 1&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;In the output only one row should be there for each id:&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;expected output:&lt;/P&gt;&lt;P&gt;ID diff changed_result t1 t2 FLAG&lt;BR /&gt;1 155 1 01apr2006 03sep2006&lt;BR /&gt;2 404 1 02oct2005 10nov2006&lt;BR /&gt;3 . 0 . . (CANT' BE CALCULATED AS THERE IS A MISSING RESULT AFTER FIRST POSITIVE)&lt;BR /&gt;4 . 0 . . 1 (ALL NEGATIVE RESULTS)&lt;BR /&gt;5 376 0 03mar2007 13mar2008 2 (ALL POSITIVE RESULTS)&lt;BR /&gt;6 . 0 . . (CANT' BE CALCULATED AS THERE IS A unknown RESULT AFTER FIRST POSITIVE)&lt;BR /&gt;7 180 0 01jan2007 30jun2007 (this is how long were definitely positive)&lt;BR /&gt;8 . 0 . . (CANT' BE CALCULATED AS THERE IS A MISSING RESULT AFTER FIRST POSITIVE)&lt;BR /&gt;9 . 0 . . (CANT' BE CALCULATED AS THERE IS A unknown RESULT AFTER FIRST POSITIVE)&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;I hope I can get some answers soon. I use BASE SAS V9&lt;BR /&gt;Thank you so much in advance for your help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 16 Dec 2016 01:13:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Time-to-event-censoring-calculation-please-advise/m-p/319423#M70150</guid>
      <dc:creator>ahhh</dc:creator>
      <dc:date>2016-12-16T01:13:23Z</dc:date>
    </item>
    <item>
      <title>Re: Time to event censoring calculation, please advise</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Time-to-event-censoring-calculation-please-advise/m-p/319426#M70151</link>
      <description>&lt;P&gt;I have this code but- I need to collapse multiple rows for each ID into a single row with needed variables.&lt;/P&gt;&lt;P&gt;Please advise...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data y ;&lt;BR /&gt;set x;&lt;BR /&gt;by id;&lt;BR /&gt;retain phase pos_d neg_d ;&lt;BR /&gt;changed_result =0; flag=0; /* initial value */&lt;BR /&gt;if first.id then phase = 0;&lt;BR /&gt;if phase = 0 and result= 'Positive' then do; pos_d = t0; phase=1; end;&lt;BR /&gt;if phase = 1 and &lt;SPAN&gt;result&lt;/SPAN&gt; in ('','unknown') then do; neg_d = t0; phase=2;&lt;SPAN&gt;diff &lt;/SPAN&gt;= neg_d - pos_d; end;&lt;BR /&gt;if phase = 1 and &lt;SPAN&gt;result&lt;/SPAN&gt; in ('Negative') then do; neg_d = t0; phase =2;&amp;nbsp;&lt;SPAN&gt;diff &lt;/SPAN&gt;= neg_d - pos_d; &lt;SPAN&gt;changed_result&lt;/SPAN&gt; = 1;end;&lt;BR /&gt;else do; diff = .; &lt;SPAN&gt;changed_result&lt;/SPAN&gt; = 0; flag=1; end;&lt;BR /&gt;if last.id and phase=0 then neg=1;&lt;BR /&gt;*output;&lt;BR /&gt;keep id pos_d neg_d diff&amp;nbsp;&lt;SPAN&gt;changed_result&amp;nbsp;flag neg;&lt;/SPAN&gt;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Fri, 16 Dec 2016 02:10:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Time-to-event-censoring-calculation-please-advise/m-p/319426#M70151</guid>
      <dc:creator>ahhh</dc:creator>
      <dc:date>2016-12-16T02:10:33Z</dc:date>
    </item>
    <item>
      <title>Re: Time to event censoring calculation, please advise</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Time-to-event-censoring-calculation-please-advise/m-p/319429#M70154</link>
      <description>&lt;P&gt;Please post OUTPUT to match your sample data.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 16 Dec 2016 02:26:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Time-to-event-censoring-calculation-please-advise/m-p/319429#M70154</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-12-16T02:26:07Z</dc:date>
    </item>
    <item>
      <title>Re: Time to event censoring calculation, please advise</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Time-to-event-censoring-calculation-please-advise/m-p/319438#M70160</link>
      <description>&lt;P&gt;This is the output I am expecting(plz refer the code i posted):&lt;/P&gt;&lt;P&gt;ID diff &amp;nbsp; changed_result &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;pos_d&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; neg_d&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; FLAG &amp;nbsp; &amp;nbsp; &amp;nbsp;neg &amp;nbsp;&lt;BR /&gt;1 &amp;nbsp;155 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 01apr2006 03sep2006&lt;BR /&gt;2 &amp;nbsp;404 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 02oct2005 10nov2006&lt;BR /&gt;3 &amp;nbsp; &amp;nbsp;. &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0 &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; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp;&lt;BR /&gt;4 &amp;nbsp; &amp;nbsp;. &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0 &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; &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; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;BR /&gt;5 &amp;nbsp; 376 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0 &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;03mar2007 13mar2008 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&amp;nbsp;&lt;BR /&gt;6 &amp;nbsp; &amp;nbsp;. &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0 &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; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&amp;nbsp;&lt;BR /&gt;7 &amp;nbsp; 180 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0 &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;01jan2007 &amp;nbsp; &amp;nbsp;30jun2007&amp;nbsp;&lt;BR /&gt;8 &amp;nbsp; &amp;nbsp;. &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0 &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; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&amp;nbsp;&lt;BR /&gt;9 &amp;nbsp; &amp;nbsp;. &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0 &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; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 16 Dec 2016 03:27:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Time-to-event-censoring-calculation-please-advise/m-p/319438#M70160</guid>
      <dc:creator>ahhh</dc:creator>
      <dc:date>2016-12-16T03:27:22Z</dc:date>
    </item>
    <item>
      <title>Re: Time to event censoring calculation, please advise</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Time-to-event-censoring-calculation-please-advise/m-p/319450#M70165</link>
      <description>&lt;PRE&gt;
There are too many scenarios you need consider about.


data have;
infile cards truncover;
input ID VISIT $ T0 :date9. RESULT : $20.;
format t0 date9.;
cards;
1 V1 07SEP2005 negative
1 v2 06oct2005 negative
1 v3 01apr2006 positive
1 v4 03sep2006 negative
1 v5 08sep2006 negative
2 v1 07sep2005 negative
2 v2 02oct2005 positive
2 v3 05nov2005 positive
2 v4 10nov2006 negative
3 v1 07SEP2005 negative
3 v2 03OCT2005 positive
3 v3 09nov2005
3 v4 10dec2005 negative
3 v5 03mar2006 positive
4 v1 07SEP2005 negative
4 v2 10SEP2005 negative
4 v3 07OCT2005 negative
4 v4 17nOV2005 negative
4 v5 27JAN2006 negative
5 v1 03mar2007 positive
5 v2 30mar2007 positive
5 v3 06jun2007 positive
5 v4 13mar2008 positive
6 v1 03feb2007 positive
6 v2 30jun2007 UNKNOWN
6 v3 06nov2007
6 v4 03mar2008 positive
7 v1 01jan2007 positive
7 v2 30jun2007 positive
7 v3 08nov2007
7 v4 01feb2008 positive
8 v1 07aug2005 negative
8 v2 07oct2005 negative
8 v3 15nov2005 positive
8 v4 01jan2006
9 v1 07jan2005 negative
9 v2 10mar2005 positive
9 v3 27OCT2005 UNKNOWN
9 v4 07dec2005 negative
9 v5 20JAN2006 negative
;
run;
data x;
 set have;
 by id;
 retain t0_p found;
 flag=0;
 if first.id then call missing(t0_p,found);
 if result='positive' and not found then do;t0_p=t0;found=1;end;
 if id=lag(id) and lag(result)='positive' and result='negative' then do;
  flag=1;diff=t0-t0_p;
 end;
 
 if id ne lag(id) or result ne 'positive' then group+1;
 format t0_p date9.;
 drop found;
run;
data want;
n=0;
 do until(last.group);
  set x;
  by group;
   n+1;
  if first.group then first=t0;
  if result ne 'positive' then have=1;
 end;
 do until(last.group);
  set x;
  by group;
  if not have and n ne 1 and last.group=1 then diff=t0-first;
  output;
 end;
 drop first n;
run;

&lt;/PRE&gt;</description>
      <pubDate>Fri, 16 Dec 2016 05:31:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Time-to-event-censoring-calculation-please-advise/m-p/319450#M70165</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-12-16T05:31:01Z</dc:date>
    </item>
    <item>
      <title>Re: Time to event censoring calculation, please advise</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Time-to-event-censoring-calculation-please-advise/m-p/319591#M70244</link>
      <description>&lt;P&gt;Thanks Ksharp. The output I am getting with your code has multiple rows per id and i need only one row per id.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This following code is working well except for id7 - the t1 and t2 values are off but the diff is calculated correctly. If possible please help fix this issue:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data test;&lt;BR /&gt;infile datalines truncover;&lt;BR /&gt;input id visit $ t0 date9. result $;&lt;BR /&gt;format t0 ddmmyy10.;&lt;BR /&gt;datalines;&lt;BR /&gt;1 V1 07SEP2005 negative&lt;BR /&gt;1 v2 06oct2005 negative&lt;BR /&gt;1 v3 01apr2006 positive&lt;BR /&gt;1 v4 03sep2006 negative&lt;BR /&gt;1 v5 08sep2006 negative&lt;BR /&gt;2 v1 07sep2005 negative&lt;BR /&gt;2 v2 02oct2005 positive&lt;BR /&gt;2 v3 05nov2005 positive&lt;BR /&gt;2 v4 10nov2006 negative&lt;BR /&gt;3 v1 07SEP2005 negative&lt;BR /&gt;3 v2 03OCT2005 positive&lt;BR /&gt;3 v3 09nov2005&lt;BR /&gt;3 v4 10dec2005 negative&lt;BR /&gt;3 v5 03mar2006 positive&lt;BR /&gt;4 v1 07SEP2005 negative&lt;BR /&gt;4 v2 10SEP2005 negative&lt;BR /&gt;4 v3 07OCT2005 negative&lt;BR /&gt;4 v4 17nOV2005 negative&lt;BR /&gt;4 v5 27JAN2006 negative&lt;BR /&gt;5 v1 03mar2007 positive&lt;BR /&gt;5 v2 30mar2007 positive&lt;BR /&gt;5 v3 06jun2007 positive&lt;BR /&gt;5 v4 13mar2008 positive&lt;BR /&gt;6 v1 03feb2007 positive&lt;BR /&gt;6 v2 30jun2007 UNKNOWN&lt;BR /&gt;6 v3 06nov2007&lt;BR /&gt;6 v4 03mar2008 positive&lt;BR /&gt;7 v1 01jan2007 positive&lt;BR /&gt;7 v2 30jun2007 positive&lt;BR /&gt;7 v3 08nov2007&lt;BR /&gt;7 v4 01feb2008 positive&lt;BR /&gt;8 v1 07aug2005 negative&lt;BR /&gt;8 v2 07oct2005 negative&lt;BR /&gt;8 v3 15nov2005 positive&lt;BR /&gt;8 v4 01jan2006&lt;BR /&gt;9 v1 07jan2005 negative&lt;BR /&gt;9 v2 10mar2005 positive&lt;BR /&gt;9 v3 27OCT2005 UNKNOWN&lt;BR /&gt;9 v4 07dec2005 negative&lt;BR /&gt;9 v5 20JAN2006 negative&lt;BR /&gt;; run;&lt;/P&gt;&lt;P&gt;data alg2;&lt;BR /&gt;format id diff changed_result t1 t2 flag;&lt;BR /&gt;keep id diff changed_result t1 t2 flag;&lt;BR /&gt;set test;&lt;BR /&gt;by id;&lt;BR /&gt;retain type ; /* 1=start negative, 2=start positive, 0=changed */&lt;BR /&gt;retain pos1d; /* 1st positive date */&lt;BR /&gt;retain flag; /* 1=all negative, 2=all positive, .=changed */&lt;BR /&gt;retain t2; /* date used to calc diff */&lt;BR /&gt;retain diff changed_result 0;&lt;BR /&gt;result = lowcase(result);&lt;BR /&gt;&lt;BR /&gt;if first.id then do; diff=.;&lt;BR /&gt;if result = 'positive' then do; type=2; pos1d = t0; flag=2; end; else&lt;BR /&gt;if result = 'negative' then do; type=1; flag=1; end;&lt;BR /&gt;else do; type = 0; flag=.; end;&lt;BR /&gt;&lt;BR /&gt;end; else&lt;BR /&gt;&lt;BR /&gt;if result = 'positive' then do;&lt;BR /&gt;if type ne 2 /* 1st positive which is not 1st in ID */&lt;BR /&gt;then do; type=2; pos1d = t0; flag=.; end;&lt;BR /&gt;else if flag=2 then do;&lt;BR /&gt;diff=t0-pos1d; t2=t0;&lt;BR /&gt;end;&lt;BR /&gt;/* if type=2 no change need */&lt;BR /&gt;end; else&lt;BR /&gt;if result = 'negative' then do;&lt;BR /&gt;if type =2 and diff=. /* 1st negative following positive(s) */&lt;BR /&gt;then do; diff = t0 - pos1d; t2=t0; flag=.; changed_result=1; end;&lt;BR /&gt;/* if type is 1 or 0 no change need */&lt;BR /&gt;end;&lt;BR /&gt;else do;&lt;BR /&gt;if type =2 /* missing following positive(s) */&lt;BR /&gt;then do; pos1d=.; *t2=.; changed_result=0; flag=.; type=0; end;&lt;BR /&gt;end;&lt;BR /&gt;&lt;BR /&gt;if last.id then do;&lt;BR /&gt;if flag=1 then do; diff=.; t1=.; t2=.; output; end; else&lt;BR /&gt;if flag=2 then do; diff=t0 - pos1d; t1=pos1d; t2=t0; output; end;&lt;BR /&gt;else do;&lt;BR /&gt;if diff &amp;gt; 0&lt;BR /&gt;then do; t1=pos1d; /* changed_result diff and t2 retained */ output; end;&lt;BR /&gt;else do; diff=.; t1=.; t2=.; output; end;&lt;BR /&gt;end;&lt;BR /&gt;end;&lt;BR /&gt;format t1 t2 date9.;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Fri, 16 Dec 2016 16:48:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Time-to-event-censoring-calculation-please-advise/m-p/319591#M70244</guid>
      <dc:creator>ahhh</dc:creator>
      <dc:date>2016-12-16T16:48:07Z</dc:date>
    </item>
    <item>
      <title>Re: Time to event censoring calculation, please advise</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Time-to-event-censoring-calculation-please-advise/m-p/319659#M70263</link>
      <description>&lt;P&gt;It helps to clarify requirements if show what the desired&amp;nbsp;output for your example data would be.&lt;/P&gt;</description>
      <pubDate>Fri, 16 Dec 2016 21:07:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Time-to-event-censoring-calculation-please-advise/m-p/319659#M70263</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-12-16T21:07:02Z</dc:date>
    </item>
    <item>
      <title>Re: Time to event censoring calculation, please advise</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Time-to-event-censoring-calculation-please-advise/m-p/319712#M70297</link>
      <description>&lt;P&gt;That would be easy , just add one more data step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
infile cards truncover;
input ID VISIT $ T0 :date9. RESULT : $20.;
format t0 date9.;
cards;
1 V1 07SEP2005 negative
1 v2 06oct2005 negative
1 v3 01apr2006 positive
1 v4 03sep2006 negative
1 v5 08sep2006 negative
2 v1 07sep2005 negative
2 v2 02oct2005 positive
2 v3 05nov2005 positive
2 v4 10nov2006 negative
3 v1 07SEP2005 negative
3 v2 03OCT2005 positive
3 v3 09nov2005
3 v4 10dec2005 negative
3 v5 03mar2006 positive
4 v1 07SEP2005 negative
4 v2 10SEP2005 negative
4 v3 07OCT2005 negative
4 v4 17nOV2005 negative
4 v5 27JAN2006 negative
5 v1 03mar2007 positive
5 v2 30mar2007 positive
5 v3 06jun2007 positive
5 v4 13mar2008 positive
6 v1 03feb2007 positive
6 v2 30jun2007 UNKNOWN
6 v3 06nov2007
6 v4 03mar2008 positive
7 v1 01jan2007 positive
7 v2 30jun2007 positive
7 v3 08nov2007
7 v4 01feb2008 positive
8 v1 07aug2005 negative
8 v2 07oct2005 negative
8 v3 15nov2005 positive
8 v4 01jan2006
9 v1 07jan2005 negative
9 v2 10mar2005 positive
9 v3 27OCT2005 UNKNOWN
9 v4 07dec2005 negative
9 v5 20JAN2006 negative
;
run;
data x;
 set have;
 by id;
 retain t0_p found;
 flag=0;
 if first.id then call missing(t0_p,found);
 if result='positive' and not found then do;t0_p=t0;found=1;end;
 if id=lag(id) and lag(result)='positive' and result='negative' then do;
  flag=1;diff=t0-t0_p;
 end;
 
 if id ne lag(id) or result ne 'positive' then group+1;
 format t0_p date9.;
 drop found;
run;
data want;
n=0;
 do until(last.group);
  set x;
  by group;
   n+1;
  if first.group then first=t0;
  if result ne 'positive' then have=1;
 end;
 do until(last.group);
  set x;
  by group;
  if not have and n ne 1 and last.group=1 then diff=t0-first;
  output;
 end;
 drop first n group have;
run;

data final_want;
 do until(last.id);
  set want;
  by id;
  if diff then found=1;
 end;
 do until(last.id);
  set want;
  by id;
  if diff then output;
 end;
 if not found then do;call missing(t0,t0_p);output;end;
 drop flag found result;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 17 Dec 2016 07:52:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Time-to-event-censoring-calculation-please-advise/m-p/319712#M70297</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-12-17T07:52:55Z</dc:date>
    </item>
  </channel>
</rss>

