<?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: select the N first observations by id in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/select-the-N-first-observations-by-id/m-p/791953#M81474</link>
    <description>_N_ does not exist in SQL only in data steps. It's a counter for the number of loops the data set has iterated, not a row counter though often synomous with that. &lt;BR /&gt;&lt;BR /&gt;_N_ does not use ay by groups but you say you want it by ID so _N_ will not work. &lt;BR /&gt;&lt;BR /&gt;You need to create your own counter variable and use that. &lt;BR /&gt;&lt;BR /&gt;&lt;A href="https://stats.oarc.ucla.edu/sas/faq/how-can-i-create-an-enumeration-variable-by-groups/" target="_blank"&gt;https://stats.oarc.ucla.edu/sas/faq/how-can-i-create-an-enumeration-variable-by-groups/&lt;/A&gt;</description>
    <pubDate>Mon, 24 Jan 2022 18:48:48 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2022-01-24T18:48:48Z</dc:date>
    <item>
      <title>select the N first observations by id</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/select-the-N-first-observations-by-id/m-p/791952#M81473</link>
      <description>&lt;P&gt;Hello,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to select&amp;nbsp;the N first observations by id. I tried these programs but they did not work&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*****select 1200 first observations by id*/
proc sql outobs=1200;
create table US_Stock_IPO_1200 as
select *
from US_stock
group by GVKEY;
run;
data US_Stock_IPO_1200; 
set US_stock; 
if _N_&amp;lt;=1200 then output; 
by GVKEY;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 24 Jan 2022 18:44:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/select-the-N-first-observations-by-id/m-p/791952#M81473</guid>
      <dc:creator>sasphd</dc:creator>
      <dc:date>2022-01-24T18:44:48Z</dc:date>
    </item>
    <item>
      <title>Re: select the N first observations by id</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/select-the-N-first-observations-by-id/m-p/791953#M81474</link>
      <description>_N_ does not exist in SQL only in data steps. It's a counter for the number of loops the data set has iterated, not a row counter though often synomous with that. &lt;BR /&gt;&lt;BR /&gt;_N_ does not use ay by groups but you say you want it by ID so _N_ will not work. &lt;BR /&gt;&lt;BR /&gt;You need to create your own counter variable and use that. &lt;BR /&gt;&lt;BR /&gt;&lt;A href="https://stats.oarc.ucla.edu/sas/faq/how-can-i-create-an-enumeration-variable-by-groups/" target="_blank"&gt;https://stats.oarc.ucla.edu/sas/faq/how-can-i-create-an-enumeration-variable-by-groups/&lt;/A&gt;</description>
      <pubDate>Mon, 24 Jan 2022 18:48:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/select-the-N-first-observations-by-id/m-p/791953#M81474</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-01-24T18:48:48Z</dc:date>
    </item>
    <item>
      <title>Re: select the N first observations by id</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/select-the-N-first-observations-by-id/m-p/791957#M81475</link>
      <description>&lt;P&gt;You need to establish your own counter. The automatic variable _n_ is how many times the data step iterates and does not reset for by variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think this may help but does require the&amp;nbsp; US_Stock data set to be sorted by GVKEY. If your records are grouped but not sorted you could use the By Notsorted GVKEY;&lt;/P&gt;
&lt;PRE&gt;data US_Stock_IPO_1200; 
   set US_stock; 
   retain reccount;
   by GVKEY;
   if first.gvkey then reccount=0;
   reccount+1;
   if reccount le 1200;
   /* drop reccount; after verifying code is working*/
run;&lt;/PRE&gt;
&lt;P&gt;Retain establishes a variable whose values are kept across data step boundaries. The FIRST. is a boolean value for variables on a By statement. So you can use that to test if the current record is the first of a group to reset the counter.&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;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4327"&gt;@sasphd&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hello,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to select&amp;nbsp;the N first observations by id. I tried these programs but they did not work&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*****select 1200 first observations by id*/
proc sql outobs=1200;
create table US_Stock_IPO_1200 as
select *
from US_stock
group by GVKEY;
run;
data US_Stock_IPO_1200; 
set US_stock; 
if _N_&amp;lt;=1200 then output; 
by GVKEY;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 24 Jan 2022 18:53:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/select-the-N-first-observations-by-id/m-p/791957#M81475</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-01-24T18:53:51Z</dc:date>
    </item>
    <item>
      <title>Re: select the N first observations by id</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/select-the-N-first-observations-by-id/m-p/791958#M81476</link>
      <description>&lt;P&gt;Since you use the terminology "N first observations" let's assume that the number can vary.&amp;nbsp; Put the number into a macro variable to make it easier to modify.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let N=1200;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then use the value of that macro variable to decide whether or not to output any given observation.&lt;/P&gt;
&lt;P&gt;You need to make your own counter for how many observations you have seen for this BY group.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you don't want that counter to become part of the resulting dataset use _N_ as the variable name.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data US_Stock_IPO_&amp;amp;n; 
  do _n_=1 by 1 until (last.gvkey);
    set US_stock; 
    by GVKEY;
    if _n_ &amp;lt;=&amp;amp;N then output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 24 Jan 2022 18:57:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/select-the-N-first-observations-by-id/m-p/791958#M81476</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-01-24T18:57:23Z</dc:date>
    </item>
  </channel>
</rss>

