<?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: Limit observation count by variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Limit-observation-count-by-variable/m-p/858299#M339119</link>
    <description>&lt;P&gt;I'm not sure I understand this part well enough to program it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;retrieve all the LATEST and MAXIMUM of 2(two) records by state&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can you explain further?&lt;/P&gt;</description>
    <pubDate>Fri, 10 Feb 2023 16:38:52 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2023-02-10T16:38:52Z</dc:date>
    <item>
      <title>Limit observation count by variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Limit-observation-count-by-variable/m-p/858297#M339117</link>
      <description>&lt;P&gt;I want to retrieve all the LATEST and MAXIMUM of 2(two) records by state.&lt;/P&gt;
&lt;P&gt;Example: For PA state, I want to retrieve 1 and 3 LicenceStateCd.. etc..&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE width="197"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="64"&gt;LicenceStateCd&lt;/TD&gt;
&lt;TD width="64"&gt;State&lt;/TD&gt;
&lt;TD width="69"&gt;Date&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;PA&lt;/TD&gt;
&lt;TD&gt;1/10/2022&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;2&lt;/TD&gt;
&lt;TD&gt;PA&lt;/TD&gt;
&lt;TD&gt;1/10/2010&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;3&lt;/TD&gt;
&lt;TD&gt;PA&lt;/TD&gt;
&lt;TD&gt;1/10/2020&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;4&lt;/TD&gt;
&lt;TD&gt;CA&lt;/TD&gt;
&lt;TD&gt;1/10/2000&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;5&lt;/TD&gt;
&lt;TD&gt;CA&lt;/TD&gt;
&lt;TD&gt;1/10/2000&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;6&lt;/TD&gt;
&lt;TD&gt;CA&lt;/TD&gt;
&lt;TD&gt;1/10/2010&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;7&lt;/TD&gt;
&lt;TD&gt;MI&lt;/TD&gt;
&lt;TD&gt;1/10/2022&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;8&lt;/TD&gt;
&lt;TD&gt;VA&lt;/TD&gt;
&lt;TD&gt;1/10/2022&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;9&lt;/TD&gt;
&lt;TD&gt;MD&lt;/TD&gt;
&lt;TD&gt;1/20/2022&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;10&lt;/TD&gt;
&lt;TD&gt;VA&lt;/TD&gt;
&lt;TD&gt;1/10/2022&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;</description>
      <pubDate>Fri, 10 Feb 2023 16:24:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Limit-observation-count-by-variable/m-p/858297#M339117</guid>
      <dc:creator>prad001</dc:creator>
      <dc:date>2023-02-10T16:24:17Z</dc:date>
    </item>
    <item>
      <title>Re: Limit observation count by variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Limit-observation-count-by-variable/m-p/858299#M339119</link>
      <description>&lt;P&gt;I'm not sure I understand this part well enough to program it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;retrieve all the LATEST and MAXIMUM of 2(two) records by state&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can you explain further?&lt;/P&gt;</description>
      <pubDate>Fri, 10 Feb 2023 16:38:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Limit-observation-count-by-variable/m-p/858299#M339119</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-02-10T16:38:52Z</dc:date>
    </item>
    <item>
      <title>Re: Limit observation count by variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Limit-observation-count-by-variable/m-p/858317#M339127</link>
      <description>&lt;P&gt;I am assuming that you want the two latest dates, if present AND that your date is an actual SAS date value.&lt;/P&gt;
&lt;P&gt;Note the first data step to make a working data set we can use.&lt;/P&gt;
&lt;PRE&gt;data have;
   input LicenceStateCd 	State $	Date :mmddyy10.;
   format date mmddyy10.;
datalines;
1 	PA 	1/10/2022
2 	PA 	1/10/2010
3 	PA 	1/10/2020
4 	CA 	1/10/2000
5 	CA 	1/10/2000
6 	CA 	1/10/2010
7 	MI 	1/10/2022
8 	VA 	1/10/2022
9 	MD 	1/20/2022
10 	VA 	1/10/2022
;

Proc sort data=have;
   by state descending date;
run;

data want;
  set have;
  by state;
  retain counter;
  if first.state then counter=1;
  else counter+1;
  if counter le 2;
  drop counter;
run;&lt;/PRE&gt;
&lt;P&gt;The proc sort will group the state by decreasing (new to oldest) date.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The By statement creates automatic variables that indicate whether a record is first or last in a group and can be used to do things conditionally at the group boundary, in this case reset a counter that counts the number of records in each State group.&lt;/P&gt;
&lt;P&gt;The Retain means that the value of Counter is maintained across the data step boundary and accumulates a count.&lt;/P&gt;
&lt;P&gt;The If Counter le 2 mean only keep the first two records.&lt;/P&gt;</description>
      <pubDate>Fri, 10 Feb 2023 18:10:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Limit-observation-count-by-variable/m-p/858317#M339127</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-02-10T18:10:00Z</dc:date>
    </item>
    <item>
      <title>Re: Limit observation count by variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Limit-observation-count-by-variable/m-p/858422#M339162</link>
      <description>&lt;P&gt;Below SQL will per state return the row with the max date and the row with the max&amp;nbsp;LicenceStateCd.&lt;/P&gt;
&lt;P&gt;- If the two max are in the same row then only a single row will be returned.&lt;/P&gt;
&lt;P&gt;- ALL rows with a max value will be returned (so can be one or several). You would need to further define what should happen in such a case if that's not what you want (like below for VA).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input LicenceStateCd State $ Date :mmddyy10.;
  format date mmddyy10.;
  datalines;
1 PA 1/10/2022
2 PA 1/10/2010
3 PA 1/10/2020
4 CA 1/10/2000
5 CA 1/10/2000
6 CA 1/10/2010
7 MI 1/10/2022
8 VA 1/10/2022
9 MD 1/20/2022
10 VA 1/10/2022
;

proc sql;
  select *
  from have
  group by state
  having max(date)=date or max(LicenceStateCd)=LicenceStateCd
  order by state, date
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1676167537659.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/80340iCEC3FCEBCB67714E/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_0-1676167537659.png" alt="Patrick_0-1676167537659.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 12 Feb 2023 02:07:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Limit-observation-count-by-variable/m-p/858422#M339162</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-02-12T02:07:06Z</dc:date>
    </item>
  </channel>
</rss>

