<?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: Finding first and last within subgroups in SAS Data Management</title>
    <link>https://communities.sas.com/t5/SAS-Data-Management/Finding-first-and-last-within-subgroups/m-p/281209#M8016</link>
    <description>&lt;P&gt;The underlying issue here is that your BASELINE_DATE variable is character.&amp;nbsp; That's not how SAS is built to handle dates.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try converting it to numeric:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;baseline_sasdate = input(baseline_date, worddate18.);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then use the numeric version in your programming.&lt;/P&gt;</description>
    <pubDate>Wed, 29 Jun 2016 18:30:06 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2016-06-29T18:30:06Z</dc:date>
    <item>
      <title>Finding first and last within subgroups</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Finding-first-and-last-within-subgroups/m-p/281191#M8013</link>
      <description>&lt;P&gt;Hi Everyone,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am trying to flag first and last observations based on baseline date (baseline_doc) for all same groupings of client_id.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example, I want my final dataset to look like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;Client_id       Baseline_Date          First.baseline_date       Last.baseline_date
     1               April 1 2016             0                           0
     1              April 20 2016             0                           1
     1              January 1 2016            1                           0
     2              January 1 2016            1                           0
     2              January 20 2016           0                           1
     3              February 20 2016          1                           1
   &lt;/PRE&gt;&lt;P&gt;This is the code I am using:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data FA_CA_DADOS_CLEAN_DELETED;
	set fa_ca_dados_clean2;
	by client_id baseline_doc;
	firstclient_id = first.client_id;
	lastclient_id = last.client_id;
	firstBaseline_doc = first.baseline_doc;
	lastBaseline_doc = last.baseline_doc;
run;&lt;/PRE&gt;&lt;P&gt;However, my code doesn't give me the output like I want. What is happening is that every client_id is getting tagged with a 1 for last.baseline_doc but there definitely are some that should be 0 b/c I have duplicate client_ids with different dates.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What am I doing wrong?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I copied this code directly from this resource:&lt;/P&gt;&lt;P&gt;&lt;A href="https://onlinecourses.science.psu.edu/stat481/node/9" target="_blank"&gt;https://onlinecourses.science.psu.edu/stat481/node/9&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;and it has the output I want so I'm not sure what is going on.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I proc sorted by client_id and baseline_date before I ran the code above!&lt;/P&gt;</description>
      <pubDate>Wed, 29 Jun 2016 18:14:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Finding-first-and-last-within-subgroups/m-p/281191#M8013</guid>
      <dc:creator>christinagting0</dc:creator>
      <dc:date>2016-06-29T18:14:20Z</dc:date>
    </item>
    <item>
      <title>Re: Finding first and last within subgroups</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Finding-first-and-last-within-subgroups/m-p/281204#M8014</link>
      <description>&lt;P&gt;First,&amp;nbsp;confusing when you are mixing variable names (doc vs date).&lt;/P&gt;
&lt;P&gt;Second, nice to show the&amp;nbsp;actual output.&lt;/P&gt;
&lt;P&gt;Third, I think you are confusing the levels of BY variable where first. and last. operate.&lt;/P&gt;
&lt;P&gt;First. will tag any observation that is first within it's value of the specified&amp;nbsp; BY-group.&lt;/P&gt;
&lt;P&gt;Since you have unique (my guess, I don't see the actual source data) values of baseline_doc/date, every row will tagged.&lt;/P&gt;
&lt;P&gt;So my&amp;nbsp;guess is that you only want tag set on first./last.client_id...? Perhaps that wasn't a coincidence that you&amp;nbsp;omitted those variables in your desired output.&lt;/P&gt;</description>
      <pubDate>Wed, 29 Jun 2016 18:25:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Finding-first-and-last-within-subgroups/m-p/281204#M8014</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2016-06-29T18:25:26Z</dc:date>
    </item>
    <item>
      <title>Re: Finding first and last within subgroups</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Finding-first-and-last-within-subgroups/m-p/281208#M8015</link>
      <description>&lt;P&gt;Yes that is right I want to tag the first and last client_ids based on baseline date&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;please see my example desired output above.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Anyone else have any ideas?&lt;/P&gt;</description>
      <pubDate>Wed, 29 Jun 2016 18:29:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Finding-first-and-last-within-subgroups/m-p/281208#M8015</guid>
      <dc:creator>christinagting0</dc:creator>
      <dc:date>2016-06-29T18:29:42Z</dc:date>
    </item>
    <item>
      <title>Re: Finding first and last within subgroups</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Finding-first-and-last-within-subgroups/m-p/281209#M8016</link>
      <description>&lt;P&gt;The underlying issue here is that your BASELINE_DATE variable is character.&amp;nbsp; That's not how SAS is built to handle dates.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try converting it to numeric:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;baseline_sasdate = input(baseline_date, worddate18.);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then use the numeric version in your programming.&lt;/P&gt;</description>
      <pubDate>Wed, 29 Jun 2016 18:30:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Finding-first-and-last-within-subgroups/m-p/281209#M8016</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-06-29T18:30:06Z</dc:date>
    </item>
    <item>
      <title>Re: Finding first and last within subgroups</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Finding-first-and-last-within-subgroups/m-p/281223#M8017</link>
      <description>&lt;P&gt;Hi Astounding..&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I double checked hoping you were right, but my baseline_doc variable is definitely number data type&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any other ideas?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is my full code so far:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;proc sort data=FA_CA_DADOS_CLEAN out=fa_ca_dados_clean2;
by mrn baseline_doc;
run;


data FA_CA_DADOS_CLEAN_DELETED;
	set fa_ca_dados_clean2;
	by mrn baseline_doc;
	firstMRN = first.mrn;
	lastMRN = last.mrn;
	firstBaseline_doc = first.baseline_doc;
	lastBaseline_doc = last.baseline_doc;
run;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;MRN = client ID&lt;/P&gt;&lt;P&gt;Basedline_doc = baseline date&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 29 Jun 2016 18:53:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Finding-first-and-last-within-subgroups/m-p/281223#M8017</guid>
      <dc:creator>christinagting0</dc:creator>
      <dc:date>2016-06-29T18:53:40Z</dc:date>
    </item>
    <item>
      <title>Re: Finding first and last within subgroups</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Finding-first-and-last-within-subgroups/m-p/281227#M8018</link>
      <description>&lt;P&gt;Well, if your baseline date is already numeric, then the output you are showing doesn't match the program you are running.&amp;nbsp; There is no way for April 1 2016 to sort earlier than January 1 2016, when the variable is on SAS's date scale.&amp;nbsp; That's the order you would get for a character variable, not numeric.&amp;nbsp; I can't see what's in your data, but I know that there's a mismatch somewhere.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 29 Jun 2016 19:00:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Finding-first-and-last-within-subgroups/m-p/281227#M8018</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-06-29T19:00:09Z</dc:date>
    </item>
    <item>
      <title>Re: Finding first and last within subgroups</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Finding-first-and-last-within-subgroups/m-p/281254#M8019</link>
      <description>&lt;P&gt;Thanks everyone!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I couldn't figure it out so I just ended up doing it quickly in excel...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;if anyone else knows the reason please post a solution. this will forever bug me:(&lt;/P&gt;</description>
      <pubDate>Wed, 29 Jun 2016 20:10:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Finding-first-and-last-within-subgroups/m-p/281254#M8019</guid>
      <dc:creator>christinagting0</dc:creator>
      <dc:date>2016-06-29T20:10:33Z</dc:date>
    </item>
    <item>
      <title>Re: Finding first and last within subgroups</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Finding-first-and-last-within-subgroups/m-p/281329#M8020</link>
      <description>&lt;P&gt;Did I miss something ?&lt;/P&gt;
&lt;P&gt;Just SORT and use FIRST LAST keyword ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sort data=have;&lt;/P&gt;
&lt;P&gt;by Client_id &amp;nbsp; &amp;nbsp; &amp;nbsp; Baseline_Date ;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;set have;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;by&amp;nbsp;&lt;SPAN&gt;Client_id ;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;first=first.Client_id &amp;nbsp; &amp;nbsp; &amp;nbsp;;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;last=last.Client_id &amp;nbsp; &amp;nbsp; &amp;nbsp;;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;run;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 30 Jun 2016 03:40:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Finding-first-and-last-within-subgroups/m-p/281329#M8020</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-06-30T03:40:15Z</dc:date>
    </item>
  </channel>
</rss>

