<?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 Grouping by a variable in SAS Enterprise Guide</title>
    <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Random-Grouping-by-a-variable/m-p/553252#M33536</link>
    <description>&lt;P&gt;Maybe rand("table")?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  array ccc[0:5] $ 20 _TEMPORARY_ ("Poland", "Australia", "UK", "Japan", "Mexico", "Canada");

  do I = 1 to 2e6;
    country = ccc[mod(I, 6)];
    output;
  end;
run;

proc sort 
  data = have 
  out = tmp
;
  by country;
run;

data want;
  set tmp;
  call streaminit(123);
  GRP = RAND('TABLE', 3/20,3/20,3/20,3/20,3/20,3/20,2/20);
run;

proc freq data = want;
table country * GRP;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;all the best&lt;/P&gt;&lt;P&gt;Bart&lt;/P&gt;</description>
    <pubDate>Tue, 23 Apr 2019 13:44:23 GMT</pubDate>
    <dc:creator>yabwon</dc:creator>
    <dc:date>2019-04-23T13:44:23Z</dc:date>
    <item>
      <title>Random Grouping by a variable</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Random-Grouping-by-a-variable/m-p/553197#M33532</link>
      <description>&lt;P&gt;Hi all!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a data set with 2million obs and I want to make 7 groups out of it (6 groups with 300,000 obs and one control group with 200,000 obs). It has to be a random selection, however it has to follow this rule:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;There is 'country' input in the data and the random groups must have the same country percentages as the big data itself, i.e. if United States is 40% of the data, then all of these subgroups must have 40% United States in the country column.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any idea is highly appreciated!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 23 Apr 2019 12:45:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Random-Grouping-by-a-variable/m-p/553197#M33532</guid>
      <dc:creator>juniore</dc:creator>
      <dc:date>2019-04-23T12:45:28Z</dc:date>
    </item>
    <item>
      <title>Re: Random Grouping by a variable</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Random-Grouping-by-a-variable/m-p/553252#M33536</link>
      <description>&lt;P&gt;Maybe rand("table")?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  array ccc[0:5] $ 20 _TEMPORARY_ ("Poland", "Australia", "UK", "Japan", "Mexico", "Canada");

  do I = 1 to 2e6;
    country = ccc[mod(I, 6)];
    output;
  end;
run;

proc sort 
  data = have 
  out = tmp
;
  by country;
run;

data want;
  set tmp;
  call streaminit(123);
  GRP = RAND('TABLE', 3/20,3/20,3/20,3/20,3/20,3/20,2/20);
run;

proc freq data = want;
table country * GRP;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;all the best&lt;/P&gt;&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Tue, 23 Apr 2019 13:44:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Random-Grouping-by-a-variable/m-p/553252#M33536</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2019-04-23T13:44:23Z</dc:date>
    </item>
    <item>
      <title>Re: Random Grouping by a variable</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Random-Grouping-by-a-variable/m-p/553260#M33537</link>
      <description>&lt;P&gt;Hi and welcome to the SAS Communities &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is one way&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Example Data */
data have;
   array c{3} $20 _temporary_ ("United States", "Canada", "Italy");
   do ID=1 to 2e6;
      Country=c[ceil(rand("Uniform")*3)];
      var1=rand('integer', 1, 100);
      var2=rand('integer', 1, 100);
      output;
   end;
run;

proc sort data=have;
   by Country;
run;

proc surveyselect data=have seed=125 out=one n=200000 outall noprint;
     strata Country / alloc=prop;
run;

data control;
   set one;
   where Selected=1;
   GroupID=0;
   keep ID Country var1 var2 GroupID;
run;

proc surveyselect data=one(where=(Selected=0)) seed=123 out=two noprint
     groups=(3e5, 3e5, 3e5, 3e5, 3e5, 3e5);
run;

data want;
   set control two(keep=ID Country var1 var2 GroupID);
run;

proc sort data=want;
   by GroupID;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 23 Apr 2019 13:57:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Random-Grouping-by-a-variable/m-p/553260#M33537</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-04-23T13:57:50Z</dc:date>
    </item>
    <item>
      <title>Re: Random Grouping by a variable</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Random-Grouping-by-a-variable/m-p/553289#M33539</link>
      <description>&lt;P&gt;I would start with something like:&lt;/P&gt;
&lt;PRE&gt;proc surveyselect data=have out=want groups=(300000, 300000, 300000, 300000, 300000, 300000, 200000);
run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 23 Apr 2019 14:55:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Random-Grouping-by-a-variable/m-p/553289#M33539</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-04-23T14:55:34Z</dc:date>
    </item>
    <item>
      <title>Re: Random Grouping by a variable</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Random-Grouping-by-a-variable/m-p/553326#M33542</link>
      <description>&lt;P&gt;1. Use PROC SURVEYSELECT&lt;/P&gt;
&lt;P&gt;2. Since you want to match by proportion of country, make your strata country.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The documentation has a fully worked example of proportional allocation here:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://documentation.sas.com/?docsetId=statug&amp;amp;docsetVersion=14.3&amp;amp;docsetTarget=statug_surveyselect_examples04.htm&amp;amp;locale=en"&gt;https://documentation.sas.com/?docsetId=statug&amp;amp;docsetVersion=14.3&amp;amp;docsetTarget=statug_surveyselect_examples04.htm&amp;amp;locale=en&lt;/A&gt;&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>Tue, 23 Apr 2019 16:10:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Random-Grouping-by-a-variable/m-p/553326#M33542</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-04-23T16:10:51Z</dc:date>
    </item>
    <item>
      <title>Re: Random Grouping by a variable</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Random-Grouping-by-a-variable/m-p/553523#M33552</link>
      <description>&lt;P&gt;this worked perfectly, thanks a lot!&lt;/P&gt;</description>
      <pubDate>Wed, 24 Apr 2019 05:49:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Random-Grouping-by-a-variable/m-p/553523#M33552</guid>
      <dc:creator>juniore</dc:creator>
      <dc:date>2019-04-24T05:49:52Z</dc:date>
    </item>
    <item>
      <title>Re: Random Grouping by a variable</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Random-Grouping-by-a-variable/m-p/553536#M33553</link>
      <description>&lt;P&gt;Anytime, glad to help &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 24 Apr 2019 07:04:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Random-Grouping-by-a-variable/m-p/553536#M33553</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-04-24T07:04:55Z</dc:date>
    </item>
  </channel>
</rss>

