Identify first and last value by date collected per record - RunSubmit.com most recent 30 from http://www.runsubmit.com 2010-07-29T20:05:49Z http://www.runsubmit.com/feeds/question/160 http://www.creativecommons.org/licenses/by-nc/2.5/rdf http://www.runsubmit.com/questions/160/identify-first-and-last-value-by-date-collected-per-record Identify first and last value by date collected per record KwonC 2009-12-04T02:53:20Z 2010-01-10T22:04:40Z <p>Here is how my data are set up:</p> <p>where date# is the date the lab result# was collected. I have tried to check if the dates are in asending order but cannot be certain (the real data can have up to 10 dates with 10 corresponding lab results). Where . are missing data. Where DATE# are dates mm/dd/yyyy, RESULT# are numeric, and ID is a string.</p> <pre><code>ID DATE1 RESULT1 DATE2 RESULT2 DATE3 RESULT3 DATE4 RESULT4 1 1/1/2000 200 3/1/2000 250 8/1/2000 100 9/1/2000 400 2 . . . . . . . . 3 2/1/2000 225 9/1/2000 100 . . . . 4 12/1/2000 300 . . . . . . </code></pre> <p>QUESTION 1: How do I identify/tag the earliest and oldest lab result per ID? </p> <p>These here is the output I need:</p> <pre><code>ID FIRST LAST 1 200 400 2 . . 3 225 100 4 300 300 </code></pre> <p>QUESTION2: any way to make the LAST output for ID 4 set to missing since there is only one result for this record?</p> http://www.runsubmit.com/questions/160/identify-first-and-last-value-by-date-collected-per-record/161#161 Answer by Toloc for Identify first and last value by date collected per record Toloc 2009-12-04T08:31:27Z 2009-12-16T16:44:26Z <p>Hello,</p> <p>Here is a solution for both questions with the use of arrays.</p> <p>Regards</p> <pre><code>DATA result(KEEP=id min max); SET dataset; ARRAY dates date1-date10; ARRAY results result1-result10; id_min=1; id_max=1; DO i=2 TO 10; IF dates{i}&gt;dates{id_max} THEN id_max=i; IF dates{i} &lt; dates{id_min} THEN id_min=i; END; first=results{id_min}; last=results{id_max}; /* Check if at least two valid values */ nb_Not_Missing=N(OF date1-date10); IF nb_Not_Missing&lt;=1 THEN last=.; RUN; </code></pre> http://www.runsubmit.com/questions/160/identify-first-and-last-value-by-date-collected-per-record/197#197 Answer by KwonC for Identify first and last value by date collected per record KwonC 2010-01-10T19:26:59Z 2010-01-10T22:04:40Z <p>Wanted to post the code I used after a few adjustments:</p> <pre><code>DATA CD4; *(KEEP=recnum cd4_first cd4_last); SET temp3; ARRAY dates date_cd1-date_cd10; ARRAY results cdct1-cdct10; id_min=1; id_max=1; DO i=2 TO 10; IF dates(i) ^=. THEN IF dates{i} &gt;= dates{id_max} THEN id_max=i; ELSE IF dates{i} &lt;= dates{id_min} THEN id_min=i; END; cd4_first=results{id_min}; cd4_last=results{id_max}; * Check if at least two valid values; nb_Not_Missing=N(OF date_cd1-date_cd10); IF nb_Not_Missing&lt;=1 THEN cd4_last=.; cd4_diff=(cd4_last-cd4_first); RUN; </code></pre>