<?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: Choose the last observations in the same firm-year in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Choose-the-last-observations-in-the-same-firm-year/m-p/588671#M168275</link>
    <description>&lt;P&gt;if the data are in order as your example implies:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;Data want;
   set have;
   by cikcode cyear;
   if last.cyear;
run;&lt;/PRE&gt;</description>
    <pubDate>Fri, 13 Sep 2019 22:05:29 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2019-09-13T22:05:29Z</dc:date>
    <item>
      <title>Choose the last observations in the same firm-year</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Choose-the-last-observations-in-the-same-firm-year/m-p/588669#M168273</link>
      <description>&lt;P&gt;Hi Madam/Sir,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to choose the last observation out of two observations in the same firm-year. For instance, I would like to choose observations # 2, 4, and 6 and drop observations #1,2, and 3. It will be highly appreciative if you can provide the programming code. Thank you. Joon1&lt;/P&gt;&lt;DIV class="branch"&gt;&lt;DIV&gt;&lt;DIV align="center"&gt;Obs CIKCODE cyear ANNUALREPORTDATE dirind123456 &lt;TABLE cellspacing="0" cellpadding="5"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;0000002024&lt;/TD&gt;&lt;TD&gt;2016&lt;/TD&gt;&lt;TD&gt;20160101&lt;/TD&gt;&lt;TD&gt;10&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;0000002024&lt;/TD&gt;&lt;TD&gt;2016&lt;/TD&gt;&lt;TD&gt;20161201&lt;/TD&gt;&lt;TD&gt;11&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;0000010254&lt;/TD&gt;&lt;TD&gt;2014&lt;/TD&gt;&lt;TD&gt;20140301&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;0000010254&lt;/TD&gt;&lt;TD&gt;2014&lt;/TD&gt;&lt;TD&gt;20141201&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;0000018169&lt;/TD&gt;&lt;TD&gt;2011&lt;/TD&gt;&lt;TD&gt;20110101&lt;/TD&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;0000018169&lt;/TD&gt;&lt;TD&gt;2011&lt;/TD&gt;&lt;TD&gt;20111201&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Fri, 13 Sep 2019 21:57:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Choose-the-last-observations-in-the-same-firm-year/m-p/588669#M168273</guid>
      <dc:creator>joon1</dc:creator>
      <dc:date>2019-09-13T21:57:34Z</dc:date>
    </item>
    <item>
      <title>Re: Choose the last observations in the same firm-year</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Choose-the-last-observations-in-the-same-firm-year/m-p/588670#M168274</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have;
by cikcode cyear annualReportdate;
run;

data want;
set have;
by cikcode cyear annualreportdate;
if last.cyear;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/107110"&gt;@joon1&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi Madam/Sir,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would like to choose the last observation out of two observations in the same firm-year. For instance, I would like to choose observations # 2, 4, and 6 and drop observations #1,2, and 3. It will be highly appreciative if you can provide the programming code. Thank you. Joon1&lt;/P&gt;
&lt;DIV class="branch"&gt;
&lt;DIV&gt;
&lt;DIV align="center"&gt;Obs CIKCODE cyear ANNUALREPORTDATE dirind123456
&lt;TABLE cellspacing="0" cellpadding="5"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD&gt;0000002024&lt;/TD&gt;
&lt;TD&gt;2016&lt;/TD&gt;
&lt;TD&gt;20160101&lt;/TD&gt;
&lt;TD&gt;10&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;0000002024&lt;/TD&gt;
&lt;TD&gt;2016&lt;/TD&gt;
&lt;TD&gt;20161201&lt;/TD&gt;
&lt;TD&gt;11&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;0000010254&lt;/TD&gt;
&lt;TD&gt;2014&lt;/TD&gt;
&lt;TD&gt;20140301&lt;/TD&gt;
&lt;TD&gt;3&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;0000010254&lt;/TD&gt;
&lt;TD&gt;2014&lt;/TD&gt;
&lt;TD&gt;20141201&lt;/TD&gt;
&lt;TD&gt;5&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;0000018169&lt;/TD&gt;
&lt;TD&gt;2011&lt;/TD&gt;
&lt;TD&gt;20110101&lt;/TD&gt;
&lt;TD&gt;4&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;0000018169&lt;/TD&gt;
&lt;TD&gt;2011&lt;/TD&gt;
&lt;TD&gt;20111201&lt;/TD&gt;
&lt;TD&gt;5&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 13 Sep 2019 22:04:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Choose-the-last-observations-in-the-same-firm-year/m-p/588670#M168274</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-09-13T22:04:27Z</dc:date>
    </item>
    <item>
      <title>Re: Choose the last observations in the same firm-year</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Choose-the-last-observations-in-the-same-firm-year/m-p/588671#M168275</link>
      <description>&lt;P&gt;if the data are in order as your example implies:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;Data want;
   set have;
   by cikcode cyear;
   if last.cyear;
run;&lt;/PRE&gt;</description>
      <pubDate>Fri, 13 Sep 2019 22:05:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Choose-the-last-observations-in-the-same-firm-year/m-p/588671#M168275</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-09-13T22:05:29Z</dc:date>
    </item>
    <item>
      <title>Re: Choose the last observations in the same firm-year</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Choose-the-last-observations-in-the-same-firm-year/m-p/588696#M168283</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/107110"&gt;@joon1&lt;/a&gt;:&lt;/P&gt;
&lt;P&gt;If your file is already properly sorted, just use the DATA step posted by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;and&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If it's not sorted, a single-step solution can be:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;                                                                                                                                                                                                                                                     
  input CIKCODE cyear ANNUALREPORTDATE dirind123456 ;                                                                                                                                                                                                           
  cards ;                                                                                                                                                                                                                                                       
0000002024  2016  20160101  10                                                                                                                                                                                                                                  
0000002024  2016  20161201  11                                                                                                                                                                                                                                  
0000010254  2014  20140301   3                                                                                                                                                                                                                                  
0000010254  2014  20141201   5                                                                                                                                                                                                                                  
0000018169  2011  20110101   4                                                                                                                                                                                                                                  
0000018169  2011  20111201   5                                                                                                                                                                                                                                  
;                                                                                                                                                                                                                                                               
run ;                                                                                                                                                                                                                                                           
                                                                                                                                                                                                                                                                
data _null_ ;                                                                                                                                                                                                                                                   
  dcl hash h (dataset:"have", duplicate:"r", ordered:"a") ;                                                                                                                                                                                                     
  h.definekey ("cikcode", "cyear") ;                                                                                                                                                                                                                            
  h.definedata (all:"y") ;                                                                                                                                                                                                                                      
  h.definedone () ;                                                                                                                                                                                                                                             
  h.output (dataset:"want") ;                                                                                                                                                                                                                                   
  stop ;                                                                                                                                                                                                                                                        
  set have ;                                                                                                                                                                                                                                                    
run ;                             
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This assumes that you have enough memory to hold the output data set, which for the sheet in your Excel workbook is guaranteed. But if in reality you have a much bigger file, here is another version requiring the hash table to hold only the record identifier in its data portion (rather than all the non-key variables):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;                                                                                                                                                                                                                                                     
  dcl hash h (ordered:"a") ;                                                                                                                                                                                                                                    
  h.definekey ("cikcode", "cyear") ;                                                                                                                                                                                                                            
  h.definedata ("rid") ;                                                                                                                                                                                                                                        
  h.definedone () ;                                                                                                                                                                                                                                             
  do rid = 1 by 1 until (z) ;                                                                                                                                                                                                                                   
    set have end = z ;                                                                                                                                                                                                                                          
    h.replace() ;                                                                                                                                                                                                                                               
  end ;                                                                                                                                                                                                                                                         
  dcl hiter hi ("h") ;                                                                                                                                                                                                                                          
  do while (hi.next() = 0) ;                                                                                                                                                                                                                                    
    set have point = rid ;                                                                                                                                                                                                                                      
    output ;                                                                                                                                                                                                                                                    
  end ;                                                                                                                                                                                                                                                         
run ;                 
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Suit yourself.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 14 Sep 2019 22:26:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Choose-the-last-observations-in-the-same-firm-year/m-p/588696#M168283</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-09-14T22:26:37Z</dc:date>
    </item>
  </channel>
</rss>

