<?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: Extract X years data out of a total of Y years randomly in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Extract-X-years-data-out-of-a-total-of-Y-years-randomly/m-p/815603#M34234</link>
    <description>How can I aviod of selecting other 3 years then</description>
    <pubDate>Sun, 29 May 2022 14:09:39 GMT</pubDate>
    <dc:creator>wudebao</dc:creator>
    <dc:date>2022-05-29T14:09:39Z</dc:date>
    <item>
      <title>Extract X years data out of a total of Y years randomly</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Extract-X-years-data-out-of-a-total-of-Y-years-randomly/m-p/815595#M34230</link>
      <description>&lt;P&gt;Hi, I am a starter of SAS.&lt;/P&gt;&lt;P&gt;I want to do a form a sample based on a dataset contains a total of Y years observations, and I only want a random subset years to do some tasks.&lt;/P&gt;&lt;P&gt;Here is my Example:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data xxx;&lt;BR /&gt;input YEAR data;&lt;BR /&gt;datalines;&lt;BR /&gt;2001 123&lt;BR /&gt;2002 123&lt;BR /&gt;2001 123&lt;BR /&gt;2003 123&lt;BR /&gt;2001 123&lt;BR /&gt;2003 123&lt;BR /&gt;2004 123&lt;BR /&gt;2005 123&lt;BR /&gt;2001 123&lt;BR /&gt;2003 123&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;For instance, it has 2001 to 2005, say if i want 2 random years data.&amp;nbsp;&lt;/P&gt;&lt;P&gt;say in the first run; 2001 and 2002 year' observations will be selected in a new dataset, the second time is another 2001 and 2005. It is totaly random.&lt;/P&gt;</description>
      <pubDate>Sun, 29 May 2022 13:46:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Extract-X-years-data-out-of-a-total-of-Y-years-randomly/m-p/815595#M34230</guid>
      <dc:creator>wudebao</dc:creator>
      <dc:date>2022-05-29T13:46:17Z</dc:date>
    </item>
    <item>
      <title>Re: Extract X years data out of a total of Y years randomly</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Extract-X-years-data-out-of-a-total-of-Y-years-randomly/m-p/815598#M34231</link>
      <description>&lt;P&gt;Assign a random number to each row, then select the observation with the X lowest (or highest) random numbers.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
    set have;
    r=rand('uniform');
run;
proc sort data=want;
    by r;
run;
data&amp;nbsp;want;
    set want(obs=2); /* Or any other number of observations that you want */
run;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Alternative: use PROC SURVEYSELECT, see this example: &lt;A href="https://documentation.sas.com/doc/en/pgmmvacdc/9.4/statug/statug_surveyselect_gettingstarted01.htm" target="_blank"&gt;https://documentation.sas.com/doc/en/pgmmvacdc/9.4/statug/statug_surveyselect_gettingstarted01.htm&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 29 May 2022 13:59:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Extract-X-years-data-out-of-a-total-of-Y-years-randomly/m-p/815598#M34231</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-05-29T13:59:21Z</dc:date>
    </item>
    <item>
      <title>Re: Extract X years data out of a total of Y years randomly</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Extract-X-years-data-out-of-a-total-of-Y-years-randomly/m-p/815602#M34233</link>
      <description>appreciate your support, but the thing is, I want to select random 2 years out of 5 years, within these random two years, all observations will be put togerther.</description>
      <pubDate>Sun, 29 May 2022 14:08:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Extract-X-years-data-out-of-a-total-of-Y-years-randomly/m-p/815602#M34233</guid>
      <dc:creator>wudebao</dc:creator>
      <dc:date>2022-05-29T14:08:15Z</dc:date>
    </item>
    <item>
      <title>Re: Extract X years data out of a total of Y years randomly</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Extract-X-years-data-out-of-a-total-of-Y-years-randomly/m-p/815603#M34234</link>
      <description>How can I aviod of selecting other 3 years then</description>
      <pubDate>Sun, 29 May 2022 14:09:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Extract-X-years-data-out-of-a-total-of-Y-years-randomly/m-p/815603#M34234</guid>
      <dc:creator>wudebao</dc:creator>
      <dc:date>2022-05-29T14:09:39Z</dc:date>
    </item>
    <item>
      <title>Re: Extract X years data out of a total of Y years randomly</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Extract-X-years-data-out-of-a-total-of-Y-years-randomly/m-p/815606#M34235</link>
      <description>&lt;P&gt;Do the random selection on just the set of years.&lt;/P&gt;
&lt;P&gt;First get a dataset with just the set of years&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=HAVE (keep=year) out=years nodupkey; run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then make a subset of the random years.&lt;/P&gt;
&lt;P&gt;Then merge back with the original and only keep the observations that match the years selected.&lt;/P&gt;</description>
      <pubDate>Sun, 29 May 2022 14:16:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Extract-X-years-data-out-of-a-total-of-Y-years-randomly/m-p/815606#M34235</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-05-29T14:16:11Z</dc:date>
    </item>
    <item>
      <title>Re: Extract X years data out of a total of Y years randomly</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Extract-X-years-data-out-of-a-total-of-Y-years-randomly/m-p/815609#M34236</link>
      <description>I considered to produce identical random integer number for observations in same years&lt;BR /&gt;then I can use&lt;BR /&gt;IF r&amp;lt;=2 to select 2 years observations,&lt;BR /&gt;But I failed, dont know how to produce identical random numbers.</description>
      <pubDate>Sun, 29 May 2022 14:18:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Extract-X-years-data-out-of-a-total-of-Y-years-randomly/m-p/815609#M34236</guid>
      <dc:creator>wudebao</dc:creator>
      <dc:date>2022-05-29T14:18:41Z</dc:date>
    </item>
    <item>
      <title>Re: Extract X years data out of a total of Y years randomly</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Extract-X-years-data-out-of-a-total-of-Y-years-randomly/m-p/815612#M34237</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
	create table years as select distinct year from xxx;
	create table rand as select year,rand('uniform') as r from years order by calculated r;
	create table want as select year,data from xxx 
	    where year in (select year from rand(obs=2)); 
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 29 May 2022 14:19:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Extract-X-years-data-out-of-a-total-of-Y-years-randomly/m-p/815612#M34237</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-05-29T14:19:30Z</dc:date>
    </item>
    <item>
      <title>Re: Extract X years data out of a total of Y years randomly</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Extract-X-years-data-out-of-a-total-of-Y-years-randomly/m-p/815721#M34244</link>
      <description>&lt;P&gt;Minor suggestion. When providing example data it is a good idea that other than a key variable, such as your year, has different values so you can see in the result what happens.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then actually show some example(s) of desired result.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Another way:&lt;/P&gt;
&lt;PRE&gt;data xxx;
input YEAR data;
/* to get different values of data for later examination*/
data= data+ _n_;
datalines;
2001 123
2002 123
2001 123
2003 123
2001 123
2003 123
2004 123
2005 123
2001 123
2003 123
;

proc sql;
   create table years as
   select distinct year
   from xxx
   order by year;
run;

proc surveyselect data=years out=selectedyears noprint
   sampsize=2;
run;

proc sql;
   create table onerandomsample as
   select b.*
   from selectedyears as a
        inner join
        xxx as b
        on a.year=b.year
   ;
quit;&lt;/PRE&gt;
&lt;P&gt;If you want to keep the generated random sample you would change the name of "onerandomsample" each time you do this process.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You would change the number of sampled years by changing SAMPSIZE in the Proc surveyselect. There is some additional information that could be generated by Surveyselect such as sampling weights if needed. There is also an option SELECTALL for Surveyselect in case you ask for more years of data than exist in your base data that will result in all the data selected.&lt;/P&gt;</description>
      <pubDate>Mon, 30 May 2022 16:16:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Extract-X-years-data-out-of-a-total-of-Y-years-randomly/m-p/815721#M34244</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-05-30T16:16:22Z</dc:date>
    </item>
  </channel>
</rss>

