<?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: Set a new variable as the first occurrence in a list in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Set-a-new-variable-as-the-first-occurrence-in-a-list/m-p/400026#M96938</link>
    <description>Thank you very much! It's a witty solution! It may work – I do know the maximum visits (It's actually 20).</description>
    <pubDate>Sat, 30 Sep 2017 20:38:35 GMT</pubDate>
    <dc:creator>braverju</dc:creator>
    <dc:date>2017-09-30T20:38:35Z</dc:date>
    <item>
      <title>Set a new variable as the first occurrence in a list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Set-a-new-variable-as-the-first-occurrence-in-a-list/m-p/400016#M96933</link>
      <description>&lt;P&gt;Dear SAS users,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have the following "vertical" dataset that represents doctor visits for 4 patients (every patient may have one or more visits):&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;PatientID &amp;nbsp; visitN&amp;nbsp; visitDate&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; labResult&lt;/P&gt;&lt;P&gt;1 &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;&lt;STRONG&gt; &amp;nbsp;03/15/2016 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/STRONG&gt;10&lt;/P&gt;&lt;P&gt;1 &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; &amp;nbsp;&lt;SPAN&gt;03/15/2017 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;20&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;SPAN&gt;09/20/2017 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;20&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;2 &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;&lt;STRONG&gt;06/15/2016 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/STRONG&gt;30&lt;STRONG&gt;&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;2 &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; &amp;nbsp;10&lt;SPAN&gt;/15/2016 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;30&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;3 &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; &lt;STRONG&gt;&amp;nbsp;03/15/2016 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/STRONG&gt;10&lt;/P&gt;&lt;P&gt;4 &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; &lt;STRONG&gt;&amp;nbsp;05/15/2016 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/STRONG&gt;20&lt;/P&gt;&lt;P&gt;4 &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; &amp;nbsp;&lt;SPAN&gt;08/20/2016 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;20&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;4 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;SPAN&gt;03/15/2017 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;20&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I would like to create several new variables:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;1. T1 = &amp;nbsp;to the FIRST visit (i.e., visitN = 0) . Basically, my T1 for each patient will be equal to bolded dates.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;2.&amp;nbsp;nDaysSince1stvisit - number of days since T1 (i.e., since the first visit)&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;May somebody direct me to how to do it? &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Thank you very much ahead.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;BR /&gt;Julia&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;P.S.&amp;nbsp;Eventually, I would like to get a "horizontal" dataset with 1 line per patient and with the labResults data for T1 (first visit), T2_1month (the visit in 1&amp;nbsp;month since the first one), and T3_1year (the visit in 1 year since the first one). However, to avoid confusion, I thought I would rather ask step by step.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 30 Sep 2017 18:14:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Set-a-new-variable-as-the-first-occurrence-in-a-list/m-p/400016#M96933</guid>
      <dc:creator>braverju</dc:creator>
      <dc:date>2017-09-30T18:14:47Z</dc:date>
    </item>
    <item>
      <title>Re: Set a new variable as the first occurrence in a list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Set-a-new-variable-as-the-first-occurrence-in-a-list/m-p/400018#M96934</link>
      <description>&lt;P&gt;If you know the maximum number of visits to expect (say 5 in the example below), then you can do a merge of five data sets, where each of those datasets is a subset based on a visitn value.&amp;nbsp; You have to rename vars from each of the subsets:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input PatientID visitN visitDate :mmddyy10. labResult;
  format visitdate date9.;
datalines;
1 0 03/15/2016 10
1 1 03/15/2017 20
1 2 09/20/2017 20
2 0 06/15/2016 30 
2 1 10/15/2016 30
3 0 03/15/2016 10
4 0 05/15/2016 20
4 1 08/20/2016 20 
4 2 03/15/2017 20
run;

data want;
  merge
    have (where=(visitn=0) rename=(visitdate=date1 labresult=lab1))
    have (where=(visitn=1) rename=(visitdate=date2 labresult=lab2))
    have (where=(visitn=2) rename=(visitdate=date3 labresult=lab3))
    have (where=(visitn=3) rename=(visitdate=date4 labresult=lab4))
    have (where=(visitn=4) rename=(visitdate=date5 labresult=lab5)) ;
    nvisits=visitn+1;
  drop visitn;
  by patientid;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;DATE1 will be the data of the first visit.&amp;nbsp; NVISITS will be the number of visit records.&amp;nbsp; Note the dataset must be sorted by patientid (though not necessarily by visitn within patientid).&lt;/P&gt;</description>
      <pubDate>Sat, 30 Sep 2017 18:25:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Set-a-new-variable-as-the-first-occurrence-in-a-list/m-p/400018#M96934</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-09-30T18:25:50Z</dc:date>
    </item>
    <item>
      <title>Re: Set a new variable as the first occurrence in a list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Set-a-new-variable-as-the-first-occurrence-in-a-list/m-p/400021#M96936</link>
      <description>&lt;P&gt;I'll go with the step-by-step approach, since you may find there are difficult decisions to make about which visit is the one to use for the 1 month or 1 year date.&amp;nbsp; So to answer your original questions, I have to assume that visitDate is a valid SAS date (not a text string), and the data is in order as indicated.&amp;nbsp; Then:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set have;&lt;/P&gt;
&lt;P&gt;by patientID VisitN;&lt;/P&gt;
&lt;P&gt;days_since + dif(visitDate);&lt;/P&gt;
&lt;P&gt;if first.patientID then days_since = 0;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 30 Sep 2017 19:33:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Set-a-new-variable-as-the-first-occurrence-in-a-list/m-p/400021#M96936</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-09-30T19:33:17Z</dc:date>
    </item>
    <item>
      <title>Re: Set a new variable as the first occurrence in a list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Set-a-new-variable-as-the-first-occurrence-in-a-list/m-p/400025#M96937</link>
      <description>Great! Thank you. It works. I'll try to figure out the next steps.</description>
      <pubDate>Sat, 30 Sep 2017 20:37:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Set-a-new-variable-as-the-first-occurrence-in-a-list/m-p/400025#M96937</guid>
      <dc:creator>braverju</dc:creator>
      <dc:date>2017-09-30T20:37:54Z</dc:date>
    </item>
    <item>
      <title>Re: Set a new variable as the first occurrence in a list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Set-a-new-variable-as-the-first-occurrence-in-a-list/m-p/400026#M96938</link>
      <description>Thank you very much! It's a witty solution! It may work – I do know the maximum visits (It's actually 20).</description>
      <pubDate>Sat, 30 Sep 2017 20:38:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Set-a-new-variable-as-the-first-occurrence-in-a-list/m-p/400026#M96938</guid>
      <dc:creator>braverju</dc:creator>
      <dc:date>2017-09-30T20:38:35Z</dc:date>
    </item>
  </channel>
</rss>

