<?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: Extracting ID's in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Extracting-ID-s/m-p/718294#M222269</link>
    <description>&lt;P&gt;You really want the &lt;EM&gt;&lt;STRONG&gt;FIRST 5&lt;/STRONG&gt;&lt;/EM&gt; of the sorted dataset?&amp;nbsp; Then:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have out=want;
  by id;
run;&lt;BR /&gt;data&amp;nbsp;want5;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;set&amp;nbsp;want&amp;nbsp;(obs=5);&lt;BR /&gt;run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But why not a random 5?&amp;nbsp; (eidt: I added the overlooked "%let size=5;").&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let size=5;
data want5 (drop=i already_sampled: );
  array already_sampled {&amp;amp;size} ; /*Record obs num of selected obs */

  i=1;   /*To index the array already_sampled */

  /* Keep going until already_sampled array is full */
  do until (nmiss(of already_sampled{*})=0);
    p=ceil(rand('uniform',nrecs));                       /*Random integer from 1 through NRECS */
    if whichn(p,of already_sampled{*})^=0 then continue; /*Skip below if this P already retrieved*/
    set have nobs=nrecs point=p;                         /*Read the p'th obs, note NRECS is size of dataset HAVE*/
    output;
    already_sampled{i}=p;
    i=i+1;
  end;
  stop;
run;&lt;/CODE&gt;&lt;/PRE&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;</description>
    <pubDate>Wed, 10 Feb 2021 16:25:55 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2021-02-10T16:25:55Z</dc:date>
    <item>
      <title>Extracting ID's</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extracting-ID-s/m-p/718269#M222250</link>
      <description>&lt;P&gt;Dear All:&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;I have over a hundred ID values.&amp;nbsp; I want to extract just 5 to conduct a preliminary analysis.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The code I wrote&lt;/P&gt;&lt;P&gt;Proc sort data = have ; by ID ; run;&lt;/P&gt;&lt;P&gt;Data want; set have;&lt;/P&gt;&lt;P&gt;If ID = '345' or ID = '650' or ID = '656' or ID = '700' or ID = '725' then output;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;Is there a shorter way of writing this code to extract the first 5 ID values.&lt;/P&gt;&lt;P&gt;Randy&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 10 Feb 2021 15:06:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extracting-ID-s/m-p/718269#M222250</guid>
      <dc:creator>RandyStan</dc:creator>
      <dc:date>2021-02-10T15:06:06Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting ID's</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extracting-ID-s/m-p/718291#M222267</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have out=havesorted;&lt;BR /&gt;   by ID; &lt;BR /&gt;run;
data want; 
   set havesorted(obs=5);
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 10 Feb 2021 16:17:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extracting-ID-s/m-p/718291#M222267</guid>
      <dc:creator>SASJedi</dc:creator>
      <dc:date>2021-02-10T16:17:25Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting ID's</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extracting-ID-s/m-p/718294#M222269</link>
      <description>&lt;P&gt;You really want the &lt;EM&gt;&lt;STRONG&gt;FIRST 5&lt;/STRONG&gt;&lt;/EM&gt; of the sorted dataset?&amp;nbsp; Then:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have out=want;
  by id;
run;&lt;BR /&gt;data&amp;nbsp;want5;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;set&amp;nbsp;want&amp;nbsp;(obs=5);&lt;BR /&gt;run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But why not a random 5?&amp;nbsp; (eidt: I added the overlooked "%let size=5;").&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let size=5;
data want5 (drop=i already_sampled: );
  array already_sampled {&amp;amp;size} ; /*Record obs num of selected obs */

  i=1;   /*To index the array already_sampled */

  /* Keep going until already_sampled array is full */
  do until (nmiss(of already_sampled{*})=0);
    p=ceil(rand('uniform',nrecs));                       /*Random integer from 1 through NRECS */
    if whichn(p,of already_sampled{*})^=0 then continue; /*Skip below if this P already retrieved*/
    set have nobs=nrecs point=p;                         /*Read the p'th obs, note NRECS is size of dataset HAVE*/
    output;
    already_sampled{i}=p;
    i=i+1;
  end;
  stop;
run;&lt;/CODE&gt;&lt;/PRE&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;</description>
      <pubDate>Wed, 10 Feb 2021 16:25:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extracting-ID-s/m-p/718294#M222269</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2021-02-10T16:25:55Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting ID's</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extracting-ID-s/m-p/718749#M222467</link>
      <description>&lt;P&gt;If you just want a random sample. PROC SURVEYSELECT is probably the best choice.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc surveyselect data = sashelp.cars 
   sampsize = 5 
   out = work.cars_sample;
   ;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you want the sample to be repeatable, i.e. to get the same "random" sample the next time you run the program, you must specify a seed:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc surveyselect data = sashelp.cars 
   seed=12345
   sampsize = 5 
   out = work.cars_sample;
   ;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 11 Feb 2021 20:49:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extracting-ID-s/m-p/718749#M222467</guid>
      <dc:creator>SASJedi</dc:creator>
      <dc:date>2021-02-11T20:49:44Z</dc:date>
    </item>
  </channel>
</rss>

