<?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: selecting last group of observation from a data set in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/selecting-last-group-of-observation-from-a-data-set/m-p/341597#M78211</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input n year :yymmdd8.;
format year yymmddn8.;
cards;
1 20170314
2 20170314
3 20170315
4 20170315
5 20170315
6 20170316
7 20170316
8 20170316
;
run;

proc sql;
create table want as
select * from have
having year = max(year);
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note how I presented example data in a data step. This enables everyone to recreate the dataset with a simple copy/paste and run, and is the way how example data should be posted for convenience. A macro to convert any dataset to a data step can be found at &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;</description>
    <pubDate>Thu, 16 Mar 2017 14:29:52 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2017-03-16T14:29:52Z</dc:date>
    <item>
      <title>selecting last group of observation from a data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/selecting-last-group-of-observation-from-a-data-set/m-p/341586#M78207</link>
      <description>&lt;P&gt;hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;suppose i have a data set&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&l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t;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;7&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;20170316&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;8&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;20170316&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;and I need to select only the last group of 20170316. I cannot specify "where 20170316" because it's dynamic and it can change. I need an universal code that will select always last group of observations in a dataset.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;thank you in advance&lt;/P&gt;</description>
      <pubDate>Thu, 16 Mar 2017 14:13:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/selecting-last-group-of-observation-from-a-data-set/m-p/341586#M78207</guid>
      <dc:creator>John1231</dc:creator>
      <dc:date>2017-03-16T14:13:54Z</dc:date>
    </item>
    <item>
      <title>Re: selecting last group of observation from a data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/selecting-last-group-of-observation-from-a-data-set/m-p/341596#M78210</link>
      <description>&lt;P&gt;One way&lt;/P&gt;
&lt;PRE&gt;proc sql;
   create table want as
   select * 
   from dataset
   having variable = max(variable);
quit;&lt;/PRE&gt;
&lt;P&gt;Where dataset is your data set name and variable is the name of the variable of interest.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There will be a note like this in the log:&lt;/P&gt;
&lt;P&gt;NOTE: The query requires remerging summary statistics back with the original data.&lt;/P&gt;
&lt;P&gt;because of using the max&amp;nbsp;in this way.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 16 Mar 2017 14:27:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/selecting-last-group-of-observation-from-a-data-set/m-p/341596#M78210</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-03-16T14:27:08Z</dc:date>
    </item>
    <item>
      <title>Re: selecting last group of observation from a data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/selecting-last-group-of-observation-from-a-data-set/m-p/341597#M78211</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input n year :yymmdd8.;
format year yymmddn8.;
cards;
1 20170314
2 20170314
3 20170315
4 20170315
5 20170315
6 20170316
7 20170316
8 20170316
;
run;

proc sql;
create table want as
select * from have
having year = max(year);
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note how I presented example data in a data step. This enables everyone to recreate the dataset with a simple copy/paste and run, and is the way how example data should be posted for convenience. A macro to convert any dataset to a data step can be found at &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;</description>
      <pubDate>Thu, 16 Mar 2017 14:29:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/selecting-last-group-of-observation-from-a-data-set/m-p/341597#M78211</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-03-16T14:29:52Z</dc:date>
    </item>
    <item>
      <title>Re: selecting last group of observation from a data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/selecting-last-group-of-observation-from-a-data-set/m-p/341599#M78212</link>
      <description>&lt;P&gt;SAS can do this automatically with by groups:&lt;/P&gt;
&lt;PRE&gt;data want;
  set have;
  by year;
  if last.year then output;
run;&lt;/PRE&gt;
&lt;P&gt;When you do by &amp;lt;variables&amp;gt; in a datastep like this, first. and last. flags are put in for each by group, note that it is a heirachy, so:&lt;/P&gt;
&lt;P&gt;by vara varb;&lt;/P&gt;
&lt;P&gt;Will put a first and last for varb within vara which will also get a first and last for each group.&lt;/P&gt;</description>
      <pubDate>Thu, 16 Mar 2017 14:33:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/selecting-last-group-of-observation-from-a-data-set/m-p/341599#M78212</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-03-16T14:33:17Z</dc:date>
    </item>
    <item>
      <title>Re: selecting last group of observation from a data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/selecting-last-group-of-observation-from-a-data-set/m-p/341605#M78213</link>
      <description>&lt;P&gt;If the last group has not the maximum value :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _NULL_;
	if 0 then set have nobs=numobs;
	set have point=numobs;
	call symput("lastgroupyear",year);
	stop;
run;

data want;
	set have;
	where year=&amp;amp;lastgroupyear;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 16 Mar 2017 14:47:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/selecting-last-group-of-observation-from-a-data-set/m-p/341605#M78213</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2017-03-16T14:47:43Z</dc:date>
    </item>
    <item>
      <title>Re: selecting last group of observation from a data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/selecting-last-group-of-observation-from-a-data-set/m-p/341607#M78215</link>
      <description>&lt;PRE&gt;data want ;
  do until (eof1);
    set have end=eof1;
    _n_=max(_n_,year);
  end;
  do until (eof2);
    set have end=eof2;
    if year eq _n_ then output;
  end;
run;
&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 16 Mar 2017 14:49:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/selecting-last-group-of-observation-from-a-data-set/m-p/341607#M78215</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-03-16T14:49:41Z</dc:date>
    </item>
    <item>
      <title>Re: selecting last group of observation from a data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/selecting-last-group-of-observation-from-a-data-set/m-p/341852#M78309</link>
      <description>&lt;PRE&gt;

data have;
input n year :yymmdd8.;
format year yymmddn8.;
cards;
1 20170314
2 20170314
3 20170315
4 20170315
5 20170315
6 20170316
7 20170316
8 20170316
;
run;


data want;
 do until(last.year);
  set have nobs=nobs;
  by year notsorted;
  count+1;
 end;
 
 do until(last.year);
  set have;
  by year notsorted;
  if count=nobs then output;
 end;
 drop count;
run;


&lt;/PRE&gt;</description>
      <pubDate>Fri, 17 Mar 2017 04:07:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/selecting-last-group-of-observation-from-a-data-set/m-p/341852#M78309</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2017-03-17T04:07:20Z</dc:date>
    </item>
  </channel>
</rss>

