<?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: Subtracting Observations within a Group in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Subtracting-Observations-within-a-Group/m-p/196996#M49216</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thank you for your help! It worked out perfectly!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sun, 08 Mar 2015 21:09:01 GMT</pubDate>
    <dc:creator>dereck255</dc:creator>
    <dc:date>2015-03-08T21:09:01Z</dc:date>
    <item>
      <title>Subtracting Observations within a Group</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Subtracting-Observations-within-a-Group/m-p/196992#M49212</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In my data set, which is an Excel input, I have a list of dates from patient visits. See example below:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;TABLE border="1" class="jiveBorder" height="199" style="width: 253px; height: 161px; border: #000000 1px solid;" width="251"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TH style="text-align: center; background-color: #6690bc; color: #ffffff; padding: 2px;"&gt;&lt;STRONG&gt;SubjectNum&lt;/STRONG&gt;&lt;/TH&gt;&lt;TH style="text-align: center; background-color: #6690bc; color: #ffffff; padding: 2px;"&gt;&lt;STRONG&gt;DateVisit&lt;/STRONG&gt;&lt;/TH&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD style="padding: 2px;"&gt;&lt;P&gt;1&lt;/P&gt;&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;01Jan2015&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD style="padding: 2px;"&gt;1&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;01Feb2015&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD style="padding: 2px;"&gt;1&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;11Feb2015&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD style="padding: 2px;"&gt;2&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;01Dec2014&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD style="padding: 2px;"&gt;2&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;12Dec2014&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD style="padding: 2px;"&gt;2&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;01Jan2015&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD style="padding: 2px;"&gt;2&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;07Jan2015&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&lt;BR /&gt;I'm trying to find the number of days between each visit for each subject. When I use the following code, SAS substracts the DateVisit observations regardless of Subject Number:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data xyz;&lt;/P&gt;&lt;P&gt;set abc;&lt;/P&gt;&lt;P&gt;DaysBtwn=dif(DateVisit);&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I tried adding a by (by=SubjectNum) statement, and that didn't work. Not sure if a by statement would do anything here anyway. What's a good (and easy) way to get this done? Thank you!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 08 Mar 2015 14:11:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Subtracting-Observations-within-a-Group/m-p/196992#M49212</guid>
      <dc:creator>dereck255</dc:creator>
      <dc:date>2015-03-08T14:11:54Z</dc:date>
    </item>
    <item>
      <title>Re: Subtracting Observations within a Group</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Subtracting-Observations-within-a-Group/m-p/196993#M49213</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;data xyz;&lt;/P&gt;&lt;P&gt;set abc;&lt;/P&gt;&lt;P&gt;by SubjectNum&lt;/P&gt;&lt;P&gt;retain LastVisit;&lt;/P&gt;&lt;P&gt;LastVisit=lag(DateVisit)&lt;/P&gt;&lt;P&gt;if first.SubjectNum then Diff=.;&lt;/P&gt;&lt;P&gt;else DaysBtwn=DateVisit-LastVisit;&lt;/P&gt;&lt;P&gt;drop LastVisit;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Or:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data xyz;&lt;/P&gt;&lt;P&gt;&amp;nbsp; set abc;&lt;/P&gt;&lt;P&gt;&amp;nbsp; if _n_^=1 then set abc (keep=SubjectNum DateVisit rename=(SubjectNum=_SubjectNum DateVisit=_DateVisit));&lt;/P&gt;&lt;P&gt;&amp;nbsp; Diff=.;&lt;/P&gt;&lt;P&gt;&amp;nbsp; if SubjectNum=_SubjectNum and not first.SubjectNum then Diff=_DateVisit-DateVisit;&lt;/P&gt;&lt;P&gt;&amp;nbsp; drop _SubjectNum _DateVisit;&lt;/P&gt;&lt;P&gt;&amp;nbsp; run;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 08 Mar 2015 15:32:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Subtracting-Observations-within-a-Group/m-p/196993#M49213</guid>
      <dc:creator>slchen</dc:creator>
      <dc:date>2015-03-08T15:32:01Z</dc:date>
    </item>
    <item>
      <title>Re: Subtracting Observations within a Group</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Subtracting-Observations-within-a-Group/m-p/196994#M49214</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;While earlier suggestions might work, this would be a little more similar to your original attempts:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data xyz;&lt;/P&gt;&lt;P&gt;set abc;&lt;/P&gt;&lt;P&gt;by SubjectNum;&lt;/P&gt;&lt;P&gt;DaysBtwn = diff(DateVisit);&lt;/P&gt;&lt;P&gt;if first.SubjectNum then DaysBtwn=.;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Note that DIFF (as well as LAG) must execute on every observation to get the right answer.&amp;nbsp; Well, at least 99% of the time anyway.&amp;nbsp; So never do this:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data xyz;&lt;/P&gt;&lt;P&gt;set abc;&lt;/P&gt;&lt;P&gt;by SubjectNum;&lt;/P&gt;&lt;P&gt;if first.SubjectNum=0 then DaysBtwn = diff(DateVisit);&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 08 Mar 2015 15:45:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Subtracting-Observations-within-a-Group/m-p/196994#M49214</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2015-03-08T15:45:14Z</dc:date>
    </item>
    <item>
      <title>Re: Subtracting Observations within a Group</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Subtracting-Observations-within-a-Group/m-p/196995#M49215</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I suggest Use @Astounding expert help as I copy many of his codes too, but if you are novice like me, just play:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data have;&lt;/P&gt;&lt;P&gt;input subjectnum $ datevisit :date9.;&lt;/P&gt;&lt;P&gt;format datevisit date9.;&lt;/P&gt;&lt;P&gt;datalines;&lt;/P&gt;&lt;P&gt;1 01Jan2015&lt;/P&gt;&lt;P&gt;1 01Feb2015&lt;/P&gt;&lt;P&gt;1 11Feb2015&lt;/P&gt;&lt;P&gt;2 01Dec2014&lt;/P&gt;&lt;P&gt;2 12Dec2014&lt;/P&gt;&lt;P&gt;2 01Jan2015&lt;/P&gt;&lt;P&gt;2 07Jan2015&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data want;&lt;/P&gt;&lt;P&gt;set have;&lt;/P&gt;&lt;P&gt;by subjectnum notsorted;&lt;/P&gt;&lt;P&gt;k=lag(datevisit);&lt;/P&gt;&lt;P&gt;if not first.subjectnum then DaysBtwn=intck('day',k,datevisit);&lt;/P&gt;&lt;P&gt;drop k;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 08 Mar 2015 18:38:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Subtracting-Observations-within-a-Group/m-p/196995#M49215</guid>
      <dc:creator>naveen_srini</dc:creator>
      <dc:date>2015-03-08T18:38:42Z</dc:date>
    </item>
    <item>
      <title>Re: Subtracting Observations within a Group</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Subtracting-Observations-within-a-Group/m-p/196996#M49216</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thank you for your help! It worked out perfectly!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 08 Mar 2015 21:09:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Subtracting-Observations-within-a-Group/m-p/196996#M49216</guid>
      <dc:creator>dereck255</dc:creator>
      <dc:date>2015-03-08T21:09:01Z</dc:date>
    </item>
  </channel>
</rss>

