<?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: Random sampling in different groups in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Random-sampling-in-different-groups/m-p/501218#M133612</link>
    <description>&lt;P&gt;Not following your description as it is hard to read as pasted into code box.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would suggest adding a variable that holds a group identifier. Then you can use proc surveyselect with a strata variable to control sampling by "group".&lt;/P&gt;
&lt;P&gt;A brief example that randomly selects records using the SEX&amp;nbsp;variable&amp;nbsp;to define "groups"&amp;nbsp;for &amp;nbsp;3 females and 5 males from the SASHELP.CLASS data set.&lt;/P&gt;
&lt;PRE&gt;proc sort data=sashelp.class out=work.class;
  by sex;
run;

proc surveyselect data=work.class out=work.sample
   sampsize= (3 5);
strata sex;
run;&lt;/PRE&gt;
&lt;P&gt;The strata statement in surveyselect requires the data to be sorted by the strata variable. The Sampsize= tells SAS how many records to select. With the sampsize=(3 5) it says take 3 from the first level of the strata variable and 5 from the second level. A single numeral would indicate the same number from each. Or similarly you can use SAMPRATE to indicate a percentage of each strata.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There are additional options as well to use other than Simple Random Sample (the default) and including additional information and variables wanted in the output set.&lt;/P&gt;
&lt;P&gt;The output data set as above provides variables containing the SelectionProb(ability) and SamplingWeight for use in procedures that might want a weight.&lt;/P&gt;</description>
    <pubDate>Wed, 03 Oct 2018 17:28:14 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2018-10-03T17:28:14Z</dc:date>
    <item>
      <title>Random sampling in different groups</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Random-sampling-in-different-groups/m-p/501211#M133609</link>
      <description>&lt;P&gt;Hi There,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Below is the example data, there will be 4 groups with the same values of variables B and C.&lt;/P&gt;
&lt;P&gt;For example: id 001 (50 20), id 005 (50 20), id 007 (50 20) categories as the same group, because&amp;nbsp; they have the same values of B and C, and so on ...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to do random sampling for 2 samples from each group.&lt;/P&gt;
&lt;P&gt;so in this example data, I will get 8 samples from 15 observations.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My actual data set have thousands of observations.&lt;/P&gt;
&lt;P&gt;I wonder if there are codes to run the sampling at once by group.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you in advance.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have;&lt;BR /&gt;input ID $ B C;&lt;BR /&gt;datalines;&lt;BR /&gt;001 50 20&lt;BR /&gt;002 50 30&lt;BR /&gt;003 40 20&lt;BR /&gt;004 70 30&lt;BR /&gt;005 50 20&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;BR /&gt;006 40 20&lt;BR /&gt;007 50 20&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;BR /&gt;008 40 20&lt;BR /&gt;009 50 30&lt;BR /&gt;010 70 30&lt;BR /&gt;011 40 20&lt;BR /&gt;012 70 30&lt;BR /&gt;013 40 20&lt;BR /&gt;014 50 30&lt;BR /&gt;015 70 30&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Wed, 03 Oct 2018 17:55:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Random-sampling-in-different-groups/m-p/501211#M133609</guid>
      <dc:creator>ursula</dc:creator>
      <dc:date>2018-10-03T17:55:14Z</dc:date>
    </item>
    <item>
      <title>Re: Random sampling in different groups</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Random-sampling-in-different-groups/m-p/501218#M133612</link>
      <description>&lt;P&gt;Not following your description as it is hard to read as pasted into code box.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would suggest adding a variable that holds a group identifier. Then you can use proc surveyselect with a strata variable to control sampling by "group".&lt;/P&gt;
&lt;P&gt;A brief example that randomly selects records using the SEX&amp;nbsp;variable&amp;nbsp;to define "groups"&amp;nbsp;for &amp;nbsp;3 females and 5 males from the SASHELP.CLASS data set.&lt;/P&gt;
&lt;PRE&gt;proc sort data=sashelp.class out=work.class;
  by sex;
run;

proc surveyselect data=work.class out=work.sample
   sampsize= (3 5);
strata sex;
run;&lt;/PRE&gt;
&lt;P&gt;The strata statement in surveyselect requires the data to be sorted by the strata variable. The Sampsize= tells SAS how many records to select. With the sampsize=(3 5) it says take 3 from the first level of the strata variable and 5 from the second level. A single numeral would indicate the same number from each. Or similarly you can use SAMPRATE to indicate a percentage of each strata.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There are additional options as well to use other than Simple Random Sample (the default) and including additional information and variables wanted in the output set.&lt;/P&gt;
&lt;P&gt;The output data set as above provides variables containing the SelectionProb(ability) and SamplingWeight for use in procedures that might want a weight.&lt;/P&gt;</description>
      <pubDate>Wed, 03 Oct 2018 17:28:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Random-sampling-in-different-groups/m-p/501218#M133612</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-10-03T17:28:14Z</dc:date>
    </item>
    <item>
      <title>Re: Random sampling in different groups</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Random-sampling-in-different-groups/m-p/501226#M133615</link>
      <description>&lt;P&gt;sorry, I know it hard to read.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here I repeated it again the data set&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Below is the example data, there will be 4 groups with the same values of variables B and C.&lt;/P&gt;
&lt;P&gt;For example: id 001 (50 20), id 005 (50 20), id 007 (50 20) categories as the same group, because&amp;nbsp; they have the same values of B and C, and so on ...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to do random sampling for 2 samples from each group.&lt;/P&gt;
&lt;P&gt;so in this example data, I will get 8 samples from 15 observations.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My actual data set have thousands of observations.&lt;/P&gt;
&lt;P&gt;I wonder if there are codes to run the sampling at once by group.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you in advance.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have;&lt;BR /&gt;input ID $ B C;&lt;BR /&gt;datalines;&lt;BR /&gt;001 50 20&lt;BR /&gt;002 50 30&lt;BR /&gt;003 40 20&lt;BR /&gt;004 70 30&lt;BR /&gt;005 50 20 &lt;BR /&gt;006 40 20&lt;BR /&gt;007 50 20 &lt;BR /&gt;008 40 20&lt;BR /&gt;009 50 30&lt;BR /&gt;010 70 30&lt;BR /&gt;011 40 20&lt;BR /&gt;012 70 30&lt;BR /&gt;013 40 20&lt;BR /&gt;014 50 30&lt;BR /&gt;015 70 30&lt;BR /&gt;;&lt;BR /&gt;run;&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;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 03 Oct 2018 17:52:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Random-sampling-in-different-groups/m-p/501226#M133615</guid>
      <dc:creator>ursula</dc:creator>
      <dc:date>2018-10-03T17:52:24Z</dc:date>
    </item>
    <item>
      <title>Re: Random sampling in different groups</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Random-sampling-in-different-groups/m-p/501229#M133617</link>
      <description>&lt;P&gt;For your case, &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;proposed method would amount to&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id $3. B C;
datalines;
001 50 20
002 50 30
003 40 20
004 70 30
005 50 20
006 40 20 
007 50 20
008 40 20 
009 50 30
010 70 30
011 40 20
012 70 30
013 40 20
014 50 20
015 70 30
;

proc sort data=have; by b c; run;

proc surveyselect data=have out=want sampsize=2;
strata b c;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 03 Oct 2018 17:57:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Random-sampling-in-different-groups/m-p/501229#M133617</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2018-10-03T17:57:05Z</dc:date>
    </item>
    <item>
      <title>Re: Random sampling in different groups</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Random-sampling-in-different-groups/m-p/501234#M133618</link>
      <description>&lt;P&gt;Thank you so much, PG.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;it works!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 03 Oct 2018 18:09:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Random-sampling-in-different-groups/m-p/501234#M133618</guid>
      <dc:creator>ursula</dc:creator>
      <dc:date>2018-10-03T18:09:24Z</dc:date>
    </item>
  </channel>
</rss>

