<?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: Subset a datastep for last 5 observations in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Subset-a-datastep-for-last-5-observations/m-p/295030#M61607</link>
    <description>&lt;PRE&gt;
You can code as simple as :

DATA WORK.IN_OUT_LAST5;
SET WORK.IFF_IN_OUT_CHANGE4  NOBS=NOBS;
IF _N_ &amp;gt;= (NOBS-4) THEN OUTPUT;
RUN;

&lt;/PRE&gt;</description>
    <pubDate>Tue, 30 Aug 2016 02:20:28 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2016-08-30T02:20:28Z</dc:date>
    <item>
      <title>Subset a datastep for last 5 observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Subset-a-datastep-for-last-5-observations/m-p/294452#M61436</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;&lt;P&gt;I have a dataset, the number of observations in it will vary frrom day to day.&amp;nbsp; I would like to be able to create a dataset from it where only the last 5 observations are output to a new dataset.&amp;nbsp; I will not have numerical column I could use.&amp;nbsp; The below is representative of the data set I wish to subset.&amp;nbsp; What would be the most efficient way to create a new data set with just the last 5 observations.&amp;nbsp; Thank you.&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;DATA&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; WORK.SET_1;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;INPUT&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;&lt;FONT face="Courier New" size="3"&gt; YYMM &lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;$8.&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;&lt;FONT face="Courier New" size="3"&gt; VAR2 &lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;$3.&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;&lt;FONT face="Courier New" size="3"&gt; VAR3 &lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;$3.&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;DATALINES&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;2016-01 A1 B1&lt;/P&gt;&lt;P&gt;2016-01 A2 B2&lt;/P&gt;&lt;P&gt;2016-01 A3 B3&lt;/P&gt;&lt;P&gt;2016-01 A4 B4&lt;/P&gt;&lt;P&gt;2016-02 A5 B5&lt;/P&gt;&lt;P&gt;2016-03 A6 B6&lt;/P&gt;&lt;P&gt;2016-04 A7 B7&lt;/P&gt;&lt;P&gt;2016-04 A8 B8&lt;/P&gt;&lt;P&gt;2016-04 A9 B9&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 26 Aug 2016 16:09:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Subset-a-datastep-for-last-5-observations/m-p/294452#M61436</guid>
      <dc:creator>BLarson</dc:creator>
      <dc:date>2016-08-26T16:09:46Z</dc:date>
    </item>
    <item>
      <title>Re: Subset a datastep for last 5 observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Subset-a-datastep-for-last-5-observations/m-p/294458#M61439</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA WORK.SET_1;
INPUT YYMM $8. VAR2 $3. VAR3 $3.;
DATALINES;
2016-01 A1 B1
2016-01 A2 B2
2016-01 A3 B3
2016-01 A4 B4
2016-02 A5 B5
2016-03 A6 B6
2016-04 A7 B7
2016-04 A8 B8
2016-04 A9 B9
;;;;
   run;
proc print;
   run;
data last5;
   do point=nobs-4 to nobs;
      set set_1 point=point nobs=nobs;
      output;
      end;
   stop;
   run;
proc print;
   run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 26 Aug 2016 16:18:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Subset-a-datastep-for-last-5-observations/m-p/294458#M61439</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2016-08-26T16:18:05Z</dc:date>
    </item>
    <item>
      <title>Re: Subset a datastep for last 5 observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Subset-a-datastep-for-last-5-observations/m-p/294461#M61440</link>
      <description>&lt;P&gt;Thank you very much, quick and simple.&amp;nbsp; I greatly appreciate it!!&lt;/P&gt;</description>
      <pubDate>Fri, 26 Aug 2016 16:21:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Subset-a-datastep-for-last-5-observations/m-p/294461#M61440</guid>
      <dc:creator>BLarson</dc:creator>
      <dc:date>2016-08-26T16:21:03Z</dc:date>
    </item>
    <item>
      <title>Re: Subset a datastep for last 5 observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Subset-a-datastep-for-last-5-observations/m-p/294558#M61459</link>
      <description>&lt;PRE&gt;
I think my code is faster than John King .


DATA WORK.SET_1;
INPUT YYMM $8. VAR2 $3. VAR3 $3.;
DATALINES;
2016-01 A1 B1
2016-01 A2 B2
2016-01 A3 B3
2016-01 A4 B4
2016-02 A5 B5
2016-03 A6 B6
2016-04 A7 B7
2016-04 A8 B8
2016-04 A9 B9
;;;;
   run;
data want;
 set set_1 nobs=nobs;
 if _n_=nobs-4 or _n_=nobs-3 or _n_=nobs-2 or _n_=nobs-1 or _n_=nobs then output;
run;
&lt;/PRE&gt;</description>
      <pubDate>Sat, 27 Aug 2016 02:03:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Subset-a-datastep-for-last-5-observations/m-p/294558#M61459</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-08-27T02:03:30Z</dc:date>
    </item>
    <item>
      <title>Re: Subset a datastep for last 5 observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Subset-a-datastep-for-last-5-observations/m-p/294561#M61462</link>
      <description>&lt;PRE&gt;
Never mind.  John's code is faster than me after testing .

&lt;/PRE&gt;</description>
      <pubDate>Sat, 27 Aug 2016 02:08:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Subset-a-datastep-for-last-5-observations/m-p/294561#M61462</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-08-27T02:08:04Z</dc:date>
    </item>
    <item>
      <title>Re: Subset a datastep for last 5 observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Subset-a-datastep-for-last-5-observations/m-p/294785#M61532</link>
      <description>&lt;P&gt;Thank you.&amp;nbsp; While you state his code is faster, you did show me a use of _n_ I was not aware of.&amp;nbsp; This is very valuable!!&amp;nbsp; I actually ended up going wiht a PROC SQL step to get the count, then used the count in a macro, then the macro to limit my outputs.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;PROC&lt;/STRONG&gt;&lt;/FONT&gt; &lt;STRONG&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;SQL&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;CREATE&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;TABLE&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;&lt;FONT face="Courier New" size="3"&gt; WORK.OBS_MACRO &lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;AS&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;SELECT&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;&lt;FONT face="Courier New" size="3"&gt; count(Month_Day) &lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;AS&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;&lt;FONT face="Courier New" size="3"&gt; MAX_CT&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;FROM&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;&lt;FONT face="Courier New" size="3"&gt; WORK.IFF_IN_OUT_CHANGE4;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;QUIT&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;*make macro for last 5 obs;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;DATA&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;_NULL_&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;SET&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;&lt;FONT face="Courier New" size="3"&gt; WORK.OBS_MACRO;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;CALL&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;&lt;FONT face="Courier New" size="3"&gt; SYMPUTX (&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;'MAXCT'&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;&lt;FONT face="Courier New" size="3"&gt;,MAX_CT,&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;&lt;FONT color="#800080" face="Courier New" size="3"&gt;'g'&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;&lt;FONT face="Courier New" size="3"&gt;);&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;RUN&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;%put&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;&lt;FONT face="Courier New" size="3"&gt; &amp;amp;MAXCT;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;DATA&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;&lt;FONT face="Courier New" size="3"&gt; WORK.IN_OUT_LAST5;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;SET&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;&lt;FONT face="Courier New" size="3"&gt; WORK.IFF_IN_OUT_CHANGE4;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;IF&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;&lt;FONT face="Courier New" size="3"&gt; _N_ &amp;gt;= (&amp;amp;MAXCT-&lt;/FONT&gt;&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;4&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;&lt;FONT face="Courier New" size="3"&gt;) &lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;THEN&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;OUTPUT&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;RUN&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 29 Aug 2016 12:47:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Subset-a-datastep-for-last-5-observations/m-p/294785#M61532</guid>
      <dc:creator>BLarson</dc:creator>
      <dc:date>2016-08-29T12:47:24Z</dc:date>
    </item>
    <item>
      <title>Re: Subset a datastep for last 5 observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Subset-a-datastep-for-last-5-observations/m-p/295030#M61607</link>
      <description>&lt;PRE&gt;
You can code as simple as :

DATA WORK.IN_OUT_LAST5;
SET WORK.IFF_IN_OUT_CHANGE4  NOBS=NOBS;
IF _N_ &amp;gt;= (NOBS-4) THEN OUTPUT;
RUN;

&lt;/PRE&gt;</description>
      <pubDate>Tue, 30 Aug 2016 02:20:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Subset-a-datastep-for-last-5-observations/m-p/295030#M61607</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-08-30T02:20:28Z</dc:date>
    </item>
    <item>
      <title>Re: Subset a datastep for last 5 observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Subset-a-datastep-for-last-5-observations/m-p/295135#M61642</link>
      <description>&lt;P&gt;This has truely been a great leanring experience for me.&amp;nbsp; I had not thought of the ability to use the NOBS in that manner.&amp;nbsp; I greatly appreciate your expertice!!&lt;/P&gt;</description>
      <pubDate>Tue, 30 Aug 2016 13:02:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Subset-a-datastep-for-last-5-observations/m-p/295135#M61642</guid>
      <dc:creator>BLarson</dc:creator>
      <dc:date>2016-08-30T13:02:41Z</dc:date>
    </item>
  </channel>
</rss>

