<?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: Separating last visit number  from 1624 total visits among 60 patients in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Separating-last-visit-number-from-1624-total-visits-among-60/m-p/484332#M125748</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/156253"&gt;@mgrzyb&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think your code requires only a few modifications. The PROC SORT step was good. The sorted HAVE dataset will have the largest VISITNO per ID in the last observation of the respective ID. So, in a subsequent data step we can go through the dataset using the SET statement (as you've suggested) and select&amp;nbsp;&lt;SPAN&gt;the last observation&amp;nbsp;from each block of consecutive observations with the same ID ("BY group" in SAS jargon).&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt; The subsetting IF condition you used ("&lt;FONT face="courier new,courier"&gt;if last.visitno;&lt;/FONT&gt;") might sound right&amp;nbsp;in a sense (after all, we do want to select the last VISITNO), but it's inappropriate. The "&lt;FONT face="courier new,courier"&gt;last.&lt;/FONT&gt;" in this syntax refers to the &lt;EM&gt;BY group&lt;/EM&gt; and hence must be followed by the name of the BY variable defining that group, in our case: ID. Moreover it requires a BY statement to specify the BY variable(s).&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Finally, the KEEP statement is the right tool to keep selected variables in an output dataset, not the RETAIN statement.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Now, try this modified data step (after your PROC SORT step):&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
by id;
if last.id;
keep id q_visitdate age_q_visit;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;&lt;/SPAN&gt;&lt;SPAN&gt;As a kind of safety measure you could also replicate the BY statement from your PROC SORT step in the above data step, i.e. include the second BY variable, VISITNO. This wouldn't change the result, but if you forgot to run the PROC SORT step, it would cause an error message in the log even if HAVE was sorted by ID, but not by ID VISITNO. I've included &lt;FONT face="courier new,courier"&gt;age_q_visit&lt;/FONT&gt; in the KEEP statement because you mentioned "age" in your initial post.&lt;/SPAN&gt;&lt;/P&gt;</description>
    <pubDate>Mon, 06 Aug 2018 12:24:42 GMT</pubDate>
    <dc:creator>FreelanceReinh</dc:creator>
    <dc:date>2018-08-06T12:24:42Z</dc:date>
    <item>
      <title>Separating last visit number  from 1624 total visits among 60 patients</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Separating-last-visit-number-from-1624-total-visits-among-60/m-p/484296#M125733</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hi. I need help. I read everything I could, even prior SAS community sites and Cody's books. I'm stuck. please help me.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have 60 patients.&amp;nbsp; Together they have 1624 visits.&amp;nbsp; Each has a&amp;nbsp; different number of visits.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here are my variables:&lt;/P&gt;&lt;P&gt;　&lt;/P&gt;&lt;P&gt;Alphabetic List of Variables and Attributes&lt;/P&gt;&lt;P&gt;# Variable Type Len Format&lt;/P&gt;&lt;P&gt;&amp;nbsp;age_q_visit&amp;nbsp;&amp;nbsp; (N-Number)&lt;/P&gt;&lt;P&gt;&amp;nbsp;id&amp;nbsp;&amp;nbsp; (N)&lt;/P&gt;&lt;P&gt;&amp;nbsp;q_oxyconcent&amp;nbsp;&amp;nbsp; (N)&lt;/P&gt;&lt;P&gt;q_oxytime&amp;nbsp;&amp;nbsp; (N)&lt;/P&gt;&lt;P&gt;q_post_gluc (N)&lt;/P&gt;&lt;P&gt;q_pre_gluc&amp;nbsp;&amp;nbsp; (N)&lt;/P&gt;&lt;P&gt;q_visitdate&amp;nbsp;&amp;nbsp; Date MMDDYY8.&lt;/P&gt;&lt;P&gt;visitno&amp;nbsp; (N) they are in order by date&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is what I want, but won't work:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;MY GOAL - TO ISOLATE THE LAST VISIT NUMBER AND AGE WHILE&amp;nbsp; KEEPING q_visitdate and ID&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sort data=have; by id visitno; run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data hope;&lt;/P&gt;&lt;P&gt;set have;&lt;/P&gt;&lt;P&gt;if last.visitno;&lt;/P&gt;&lt;P&gt;retain id q_visitnum;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ANY IDEAS?&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks.&lt;/P&gt;&lt;P&gt;set hope;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 06 Aug 2018 09:35:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Separating-last-visit-number-from-1624-total-visits-among-60/m-p/484296#M125733</guid>
      <dc:creator>mgrzyb</dc:creator>
      <dc:date>2018-08-06T09:35:38Z</dc:date>
    </item>
    <item>
      <title>Re: Separating last visit number  from 1624 total visits among 60 patients</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Separating-last-visit-number-from-1624-total-visits-among-60/m-p/484299#M125735</link>
      <description>&lt;P&gt;Would need some test data in the form of a datastep and what you want to see at the end.&amp;nbsp; For instance, what does "last age" mean?&amp;nbsp; Do you just want maximum age from the data?&amp;nbsp; What does last visit mean?&amp;nbsp; Its really hard to guess, but if last visit just means sorting it and taking the last record:&lt;/P&gt;
&lt;PRE&gt;proc sort data=have out=want1 (obs=1);
  by descending visitno;
run;
proc sort data=have out=want2 (obs=1);
  by descending age;
run;&lt;/PRE&gt;
&lt;P&gt;That will give the last visit and last age.&lt;/P&gt;</description>
      <pubDate>Mon, 06 Aug 2018 09:58:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Separating-last-visit-number-from-1624-total-visits-among-60/m-p/484299#M125735</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-08-06T09:58:35Z</dc:date>
    </item>
    <item>
      <title>Re: Separating last visit number  from 1624 total visits among 60 patients</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Separating-last-visit-number-from-1624-total-visits-among-60/m-p/484305#M125740</link>
      <description>&lt;P&gt;Dear Super User,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you! However, &amp;nbsp; it did not work.&amp;nbsp; I want to save id and last clinicvisitdate (a date var).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So I need both the ID and the last clinicvisitdate for 60 patients.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;SO I want an output like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;id lastclinicvisitdate&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp; 12/14/15&lt;/P&gt;&lt;P&gt;2 &amp;nbsp; &amp;nbsp; 2/9/18&lt;/P&gt;&lt;P&gt;etc.&lt;/P&gt;</description>
      <pubDate>Mon, 06 Aug 2018 10:30:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Separating-last-visit-number-from-1624-total-visits-among-60/m-p/484305#M125740</guid>
      <dc:creator>mgrzyb</dc:creator>
      <dc:date>2018-08-06T10:30:33Z</dc:date>
    </item>
    <item>
      <title>Re: Separating last visit number  from 1624 total visits among 60 patients</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Separating-last-visit-number-from-1624-total-visits-among-60/m-p/484306#M125741</link>
      <description>Each patient has about 20 to 30 clinic visit dates. I need the id and last clinic visit date.&lt;BR /&gt;Thank you, though.</description>
      <pubDate>Mon, 06 Aug 2018 10:31:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Separating-last-visit-number-from-1624-total-visits-among-60/m-p/484306#M125741</guid>
      <dc:creator>mgrzyb</dc:creator>
      <dc:date>2018-08-06T10:31:41Z</dc:date>
    </item>
    <item>
      <title>Re: Separating last visit number  from 1624 total visits among 60 patients</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Separating-last-visit-number-from-1624-total-visits-among-60/m-p/484307#M125742</link>
      <description>&lt;P&gt;Please refer to post guidance, provide test data in the form of datastep and what you want out:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 06 Aug 2018 10:46:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Separating-last-visit-number-from-1624-total-visits-among-60/m-p/484307#M125742</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-08-06T10:46:29Z</dc:date>
    </item>
    <item>
      <title>Re: Separating last visit number  from 1624 total visits among 60 patients</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Separating-last-visit-number-from-1624-total-visits-among-60/m-p/484308#M125743</link>
      <description>&lt;P&gt;I don't understand that reading at all!&lt;/P&gt;</description>
      <pubDate>Mon, 06 Aug 2018 10:49:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Separating-last-visit-number-from-1624-total-visits-among-60/m-p/484308#M125743</guid>
      <dc:creator>mgrzyb</dc:creator>
      <dc:date>2018-08-06T10:49:40Z</dc:date>
    </item>
    <item>
      <title>Re: Separating last visit number  from 1624 total visits among 60 patients</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Separating-last-visit-number-from-1624-total-visits-among-60/m-p/484331#M125747</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/156253"&gt;@mgrzyb&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I don't understand that reading at all!&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Then what are you doing here?&lt;/P&gt;
&lt;P&gt;Frankly, that 6-step instruction is so easy to follow that not being able to do that bodes very badly for your future in SAS programming.&lt;/P&gt;</description>
      <pubDate>Mon, 06 Aug 2018 12:19:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Separating-last-visit-number-from-1624-total-visits-among-60/m-p/484331#M125747</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-08-06T12:19:03Z</dc:date>
    </item>
    <item>
      <title>Re: Separating last visit number  from 1624 total visits among 60 patients</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Separating-last-visit-number-from-1624-total-visits-among-60/m-p/484332#M125748</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/156253"&gt;@mgrzyb&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think your code requires only a few modifications. The PROC SORT step was good. The sorted HAVE dataset will have the largest VISITNO per ID in the last observation of the respective ID. So, in a subsequent data step we can go through the dataset using the SET statement (as you've suggested) and select&amp;nbsp;&lt;SPAN&gt;the last observation&amp;nbsp;from each block of consecutive observations with the same ID ("BY group" in SAS jargon).&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt; The subsetting IF condition you used ("&lt;FONT face="courier new,courier"&gt;if last.visitno;&lt;/FONT&gt;") might sound right&amp;nbsp;in a sense (after all, we do want to select the last VISITNO), but it's inappropriate. The "&lt;FONT face="courier new,courier"&gt;last.&lt;/FONT&gt;" in this syntax refers to the &lt;EM&gt;BY group&lt;/EM&gt; and hence must be followed by the name of the BY variable defining that group, in our case: ID. Moreover it requires a BY statement to specify the BY variable(s).&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Finally, the KEEP statement is the right tool to keep selected variables in an output dataset, not the RETAIN statement.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Now, try this modified data step (after your PROC SORT step):&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
by id;
if last.id;
keep id q_visitdate age_q_visit;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;&lt;/SPAN&gt;&lt;SPAN&gt;As a kind of safety measure you could also replicate the BY statement from your PROC SORT step in the above data step, i.e. include the second BY variable, VISITNO. This wouldn't change the result, but if you forgot to run the PROC SORT step, it would cause an error message in the log even if HAVE was sorted by ID, but not by ID VISITNO. I've included &lt;FONT face="courier new,courier"&gt;age_q_visit&lt;/FONT&gt; in the KEEP statement because you mentioned "age" in your initial post.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 06 Aug 2018 12:24:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Separating-last-visit-number-from-1624-total-visits-among-60/m-p/484332#M125748</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2018-08-06T12:24:42Z</dc:date>
    </item>
    <item>
      <title>Re: Separating last visit number  from 1624 total visits among 60 patients</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Separating-last-visit-number-from-1624-total-visits-among-60/m-p/484341#M125753</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Frankly, that 6-step instruction is so easy to follow that not being able to do that bodes very badly for your future in SAS programming.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Maybe.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But I think the process could still be easier. I will write a post about this on the "Community Matters" board, probably later today.&lt;/P&gt;</description>
      <pubDate>Mon, 06 Aug 2018 12:59:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Separating-last-visit-number-from-1624-total-visits-among-60/m-p/484341#M125753</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2018-08-06T12:59:42Z</dc:date>
    </item>
    <item>
      <title>Re: Separating last visit number  from 1624 total visits among 60 patients</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Separating-last-visit-number-from-1624-total-visits-among-60/m-p/484365#M125760</link>
      <description>&lt;P&gt;Hi Freelance Rienhard,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;That code did it! Thank you!&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For the others, please explain what 6 lines of code!!!! Please. I want to understand!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;M.&lt;/P&gt;</description>
      <pubDate>Mon, 06 Aug 2018 14:02:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Separating-last-visit-number-from-1624-total-visits-among-60/m-p/484365#M125760</guid>
      <dc:creator>mgrzyb</dc:creator>
      <dc:date>2018-08-06T14:02:34Z</dc:date>
    </item>
    <item>
      <title>Re: Separating last visit number  from 1624 total visits among 60 patients</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Separating-last-visit-number-from-1624-total-visits-among-60/m-p/485898#M126300</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/156253"&gt;@mgrzyb&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi Freelance Rienhard,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That code did it! Thank you!&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For the others, please explain what 6 lines of code!!!! Please. I want to understand!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;M.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/156253"&gt;@mgrzyb&lt;/a&gt;&amp;nbsp;The 6 step instructions on how to post your data as a data step that was previously&amp;nbsp;linked to. Could you please elaborate on what you cannot follow on this post and what steps gave you issues? Your statement was:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;I don't understand that reading at all!&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You seem to be interpreting things incorrectly (language barrier?), regarding the post (writing vs reading) and "6 lines of code" versus "6 steps" which are very different things.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The link is here:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 10 Aug 2018 17:57:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Separating-last-visit-number-from-1624-total-visits-among-60/m-p/485898#M126300</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-08-10T17:57:57Z</dc:date>
    </item>
  </channel>
</rss>

