<?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 Generating numbers randomly from a table in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Generating-numbers-randomly-from-a-table/m-p/608217#M17646</link>
    <description>&lt;P&gt;I want to generate numbers randomly from a csv table of 40,000 number. I've attached an sample of the table of numbers "sample.csv".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to then use the numbers generated to produce 100 iterations of the formula:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;iteration(i)&amp;nbsp; = sum(random number (j)) from j = 1 to x&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So, the first cell in the table below will generate 15 numbers from the table sample.csv and then sum them together and output to the cell, the second iteration will do the same but it will be a new set of 15 random numbers from the table sample.csv, and so on.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In the second row, x is 144 so each iteration will sum 144 random numbers from the sample.csv table.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is the code to import the csv:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc import datafile="C:\Users\mbrophy\Downloads\sample.csv" &lt;BR /&gt;out=mydata dbms=csv replace; &lt;BR /&gt;getnames=no; &lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="pic.PNG" style="width: 518px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/34322iF8705A039B7A15BA/image-size/large?v=v2&amp;amp;px=999" role="button" title="pic.PNG" alt="pic.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
    <pubDate>Fri, 29 Nov 2019 10:38:21 GMT</pubDate>
    <dc:creator>brophymj</dc:creator>
    <dc:date>2019-11-29T10:38:21Z</dc:date>
    <item>
      <title>Generating numbers randomly from a table</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Generating-numbers-randomly-from-a-table/m-p/608217#M17646</link>
      <description>&lt;P&gt;I want to generate numbers randomly from a csv table of 40,000 number. I've attached an sample of the table of numbers "sample.csv".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to then use the numbers generated to produce 100 iterations of the formula:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;iteration(i)&amp;nbsp; = sum(random number (j)) from j = 1 to x&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So, the first cell in the table below will generate 15 numbers from the table sample.csv and then sum them together and output to the cell, the second iteration will do the same but it will be a new set of 15 random numbers from the table sample.csv, and so on.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In the second row, x is 144 so each iteration will sum 144 random numbers from the sample.csv table.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is the code to import the csv:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc import datafile="C:\Users\mbrophy\Downloads\sample.csv" &lt;BR /&gt;out=mydata dbms=csv replace; &lt;BR /&gt;getnames=no; &lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="pic.PNG" style="width: 518px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/34322iF8705A039B7A15BA/image-size/large?v=v2&amp;amp;px=999" role="button" title="pic.PNG" alt="pic.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 29 Nov 2019 10:38:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Generating-numbers-randomly-from-a-table/m-p/608217#M17646</guid>
      <dc:creator>brophymj</dc:creator>
      <dc:date>2019-11-29T10:38:21Z</dc:date>
    </item>
    <item>
      <title>Re: Generating numbers randomly from a table</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Generating-numbers-randomly-from-a-table/m-p/608218#M17647</link>
      <description>&lt;P&gt;Please attach the csv file, and post the code used to read it into SAS.&lt;/P&gt;</description>
      <pubDate>Fri, 29 Nov 2019 10:24:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Generating-numbers-randomly-from-a-table/m-p/608218#M17647</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-11-29T10:24:39Z</dc:date>
    </item>
    <item>
      <title>Re: Generating numbers randomly from a table</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Generating-numbers-randomly-from-a-table/m-p/608219#M17648</link>
      <description>&lt;P&gt;HI Kurt, I've done that now.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 29 Nov 2019 10:29:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Generating-numbers-randomly-from-a-table/m-p/608219#M17648</guid>
      <dc:creator>brophymj</dc:creator>
      <dc:date>2019-11-29T10:29:25Z</dc:date>
    </item>
    <item>
      <title>Re: Generating numbers randomly from a table</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Generating-numbers-randomly-from-a-table/m-p/608234#M17650</link>
      <description>&lt;P&gt;HI&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31948"&gt;@brophymj&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can try the below code. Let me know if this works. Hope this help!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	input x @@;
	cards;
15 144 3 4 66 2 4 5 5
;
run;

data sample;/* &amp;lt;== put the csv file */
	input r @@;
	cards;
-2 -2 -2 -2 -2 -6 -2 -2 -2 -2 
;
run;

/*Create macro-variables*/
data _null_;
	set have;
	call symputx ("nb_obs"||left(_n_),x); /* store each value of x in a macrovariable*/
	call symputx ("nb_row",_n_); /* store the total number of rows in the dataset "have"*/
run;

/*Generate random numbers from sample data and create a table per draw with the sum:
name of the dataset = out_&amp;lt;sample size&amp;gt;_&amp;lt;iteration number&amp;gt; */

%macro random;

	%do i=1 %to &amp;amp;nb_row;
		%do j=1 %to 100;

			proc surveyselect data=sample method=srs n=&amp;amp;&amp;amp;nb_obs&amp;amp;i out=out_&amp;amp;&amp;amp;nb_obs&amp;amp;i.._&amp;amp;j.;
			run;
			
			proc means data=out_&amp;amp;&amp;amp;nb_obs&amp;amp;i.._&amp;amp;j. sum;
				output out=out_&amp;amp;&amp;amp;nb_obs&amp;amp;i.._&amp;amp;j. sum=it&amp;amp;j.;
			run;

		%end;
	%end;

%mend;

%random;

/*Initialize empty tables */
		
%macro null_table;

	%do i=1 %to &amp;amp;nb_row;
	
		data have_&amp;amp;i.;
			set have;
		run;
	%end;
	
%mend;

%null_table

/*Creation of one table per draw by retrieving all iterations for one sample size */

%macro append;

	%do i=1 %to &amp;amp;nb_row;
		%do j=1 %to 100;
			data have_&amp;amp;i. ;
				set have_&amp;amp;i. ;
				set out_&amp;amp;&amp;amp;nb_obs&amp;amp;i.._&amp;amp;j. (drop=_type_ _freq_);
			run;
		%end;
	%end;
	
%mend;

%append;

/*Append all draws */

data have_compil (drop=x);
	set have_:;
run;

data want;
	set have;
	set have_compil;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 29 Nov 2019 12:16:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Generating-numbers-randomly-from-a-table/m-p/608234#M17650</guid>
      <dc:creator>ed_sas_member</dc:creator>
      <dc:date>2019-11-29T12:16:20Z</dc:date>
    </item>
    <item>
      <title>Re: Generating numbers randomly from a table</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Generating-numbers-randomly-from-a-table/m-p/608256#M17653</link>
      <description>&lt;P&gt;Thanks&amp;nbsp;&lt;A id="link_27" class="lia-link-navigation lia-page-link lia-user-name-link" href="https://communities.sas.com/t5/user/viewprofilepage/user-id/292097" target="_self"&gt;&lt;SPAN class="login-bold"&gt;ed_sas_member&lt;/SPAN&gt;&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That works really well. The only small thing is that the random numbers generated are the same rows which have the same x. So, in the dataset "have", I'm getting the same number for the last two rows (where x = 5). Any ideas what's causing this? Thanks&lt;/P&gt;</description>
      <pubDate>Fri, 29 Nov 2019 14:38:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Generating-numbers-randomly-from-a-table/m-p/608256#M17653</guid>
      <dc:creator>brophymj</dc:creator>
      <dc:date>2019-11-29T14:38:43Z</dc:date>
    </item>
    <item>
      <title>Re: Generating numbers randomly from a table</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Generating-numbers-randomly-from-a-table/m-p/608286#M17655</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31948"&gt;@brophymj&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you for your feedback!&lt;/P&gt;
&lt;P&gt;I think you can replace &lt;FONT color="#FF0000"&gt;out_&amp;amp;&amp;amp;nb_obs&amp;amp;i.._&amp;amp;j.&lt;/FONT&gt; by &lt;FONT color="#339966"&gt;&lt;SPAN&gt;out_&amp;amp;i._&amp;amp;j.&lt;/SPAN&gt; &lt;/FONT&gt;in the code as follows. In fact, in the previous code, the name of the datasets created for each draw was dependent on the x variable. So when you had multiple similar values for x, the datasets where erased.&lt;/P&gt;
&lt;P&gt;Best,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	input x @@;
	cards;
15 144 3 4 66 2 4 5 5
;
run;

data sample;/* &amp;lt;== put the csv file */
	input r @@;
	cards;
-2 -2 -2 -2 -2 -6 -2 -2 -2 -2 
;
run;

/*Create macro-variables*/
data _null_;
	set have;
	call symputx ("nb_obs"||left(_n_),x); /* store each value of x in a macrovariable*/
	call symputx ("nb_row",_n_); /* store the total number of rows in the dataset "have"*/
run;

/*Generate random numbers from sample data and create a table per draw with the sum:
name of the dataset = out_&amp;lt;sample size&amp;gt;_&amp;lt;iteration number&amp;gt; */

%macro random;

	%do i=1 %to &amp;amp;nb_row;
		%do j=1 %to 100;

			proc surveyselect data=sample method=srs n=&amp;amp;&amp;amp;nb_obs&amp;amp;i out=&lt;SPAN&gt;out_&amp;amp;i._&amp;amp;j.&lt;/SPAN&gt;;
			run;
			
			proc means data=&lt;SPAN&gt;out_&amp;amp;i._&amp;amp;j.&lt;/SPAN&gt; sum;
				output out=&lt;SPAN&gt;out_&amp;amp;i._&amp;amp;j.&lt;/SPAN&gt; sum=it&amp;amp;j.;
			run;

		%end;
	%end;

%mend;

%random;

/*Initialize empty tables */
		
%macro null_table;

	%do i=1 %to &amp;amp;nb_row;
	
		data have_&amp;amp;i.;
			set have;
		run;
	%end;
	
%mend;

%null_table

/*Creation of one table per draw by retrieving all iterations for one sample size */

%macro append;

	%do i=1 %to &amp;amp;nb_row;
		%do j=1 %to 100;
			data have_&amp;amp;i. ;
				set have_&amp;amp;i. ;
				set &lt;SPAN&gt;out_&amp;amp;i._&amp;amp;j.&lt;/SPAN&gt; (drop=_type_ _freq_);
			run;
		%end;
	%end;
	
%mend;

%append;

/*Append all draws */

data have_compil (drop=x);
	set have_:;
run;

data want;
	set have;
	set have_compil;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 29 Nov 2019 16:28:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Generating-numbers-randomly-from-a-table/m-p/608286#M17655</guid>
      <dc:creator>ed_sas_member</dc:creator>
      <dc:date>2019-11-29T16:28:23Z</dc:date>
    </item>
    <item>
      <title>Re: Generating numbers randomly from a table</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Generating-numbers-randomly-from-a-table/m-p/608294#M17656</link>
      <description>&lt;P&gt;Thanks Ed_sas_member&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It's not working for me and it's probably because i didn't show you the files i'm using. I've attached the sas code and two input files. When i run it, the first row is repeated for all 18 observations. It seems it's almost there but I'm missing something.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 29 Nov 2019 17:06:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Generating-numbers-randomly-from-a-table/m-p/608294#M17656</guid>
      <dc:creator>brophymj</dc:creator>
      <dc:date>2019-11-29T17:06:55Z</dc:date>
    </item>
    <item>
      <title>Re: Generating numbers randomly from a table</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Generating-numbers-randomly-from-a-table/m-p/608408#M17667</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31948"&gt;@brophymj&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Using the files you've attached here an approach which doesn't require macro coding. I've stored the data from windist.csv in a variable called &lt;EM&gt;number&lt;/EM&gt;. If you call it the same and you call the source tables &lt;EM&gt;work.analysis2_ind&lt;/EM&gt; and &lt;EM&gt;work.windist&lt;/EM&gt; then below code should run for you without any changes required.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
%let n_iter=100;
data want(drop=_:);

  if _n_=1 then
    do;
      /* define hash for number list */
      dcl hash numbers();
      numbers.defineKey('_n');
      numbers.defineData('_number');
      numbers.defineDone();

      /* load hash with number list */
      do while(not last);
        set windist(keep=number rename=(number=_number)) end=last;
        _n+1;
        numbers.add();
        /* keep total sum in retained variable
           used later for exception handling
        */
        _tot_sum_num=sum(_tot_sum_num,_number);
        retain _tot_sum_num;
      end;

      /* define hash to hold _n of already selected items */
      dcl hash selected();
      selected.defineKey('_n');
      selected.defineDone();
    end;

    /* read master table */
    set analysis2_ind;

    array sum_num_ {&amp;amp;n_iter} 8;

    /* &amp;amp;n_iter iterations */
    do _iterInd=1 to dim(sum_num_);
      selected.clear();

      /*  pick no_of_spins items from hash randomly without replacement */ 

      if no_of_spins&amp;gt;numbers.num_items then
        do;
          /* exception handling */
          put 'More items requested than available in number list ';
          put 'All items will get selected for summarization';
          sum_num_[_iterInd]=_tot_sum_num;
          continue;
        end;

      do _spinInd=1 to no_of_spins;
        /* generate random item number not yet used */
        do while(1);
          _n=RAND('INTEGER',numbers.num_items);
          if selected.check() ne 0 then
            do;
              selected.add();
              leave;
            end;
        end;

        /* read new item from hash and sum */
        numbers.find();
        sum_num_[_iterInd]=sum(sum_num_[_iterInd], _number);
      end;
    end;
run;

proc print data=want;
  var no_of_spins sum_num_:;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.JPG" style="width: 392px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/34335i0EAD7255D4D14DC2/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture.JPG" alt="Capture.JPG" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I wasn't really sure where &lt;EM&gt;without replacement&lt;/EM&gt; applies for randomly picking a number from your list. In the code above it's per cell summation but the same number could get picked for the sum in the next cell. Is that what you're after?&lt;/P&gt;</description>
      <pubDate>Sat, 30 Nov 2019 04:02:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Generating-numbers-randomly-from-a-table/m-p/608408#M17667</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2019-11-30T04:02:48Z</dc:date>
    </item>
    <item>
      <title>Re: Generating numbers randomly from a table</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Generating-numbers-randomly-from-a-table/m-p/608424#M17671</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31948"&gt;@brophymj&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you for sharing your documents.&lt;/P&gt;
&lt;P&gt;I have gone through the code and I have corrected the below elements:&lt;/P&gt;
&lt;P&gt;- keep no_of_spin in the macro %null_table&lt;/P&gt;
&lt;P&gt;- put out_&amp;amp;i._&amp;amp;j. in the macro %append&lt;/P&gt;
&lt;P&gt;- update the SET statement in the last step to be sure that table will be append in the right order&lt;/P&gt;
&lt;P&gt;It should work now.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Let me know&amp;nbsp;&lt;img id="smileyhappy" class="emoticon emoticon-smileyhappy" src="https://communities.sas.com/i/smilies/16x16_smiley-happy.png" alt="Smiley Happy" title="Smiley Happy" /&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best,&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*Create macro-variables*/
data _null_;
	set analysis2_ind;
	call symputx ("nb_obs"||left(_n_),no_of_spins); /* store each value of x in a macrovariable*/
	call symputx ("nb_row",_n_); /* store the total number of rows in the dataset "have"*/
run;

/*Generate random numbers from sample data and create a table per draw with the sum:
name of the dataset = out_&amp;lt;sample size&amp;gt;_&amp;lt;iteration number&amp;gt; */



%macro random;

	%do i=1 %to &amp;amp;nb_row;
		%do j=1 %to 5;

			proc surveyselect data=sample method=srs n=&amp;amp;&amp;amp;nb_obs&amp;amp;i out=out_&amp;amp;i._&amp;amp;j.;
			run;
			
			proc means data=out_&amp;amp;i._&amp;amp;j. sum;
				output out=out_&amp;amp;i._&amp;amp;j. sum=it&amp;amp;j.;
			run;

		%end;
	%end;

%mend;

%random;

/*Initialize empty tables */
		
%macro null_table;

	%do i=1 %to &amp;amp;nb_row;
	
		data analysis2_ind_&amp;amp;i.;
			set analysis2_ind (keep=no_of_spins);
		run;
	%end;
	
%mend;

%null_table

/*Creation of one table per draw by retrieving all iterations for one sample size */

%macro append;

	%do i=1 %to &amp;amp;nb_row;
		%do j=1 %to 5;
			data analysis2_ind_&amp;amp;i. ;
				set analysis2_ind_&amp;amp;i. ;
				set out_&amp;amp;i._&amp;amp;j. (drop=_type_ _freq_); /*&amp;lt;=== put out_&amp;amp;i._&amp;amp;j. and not out_&amp;amp;&amp;amp;nb_obs&amp;amp;i.._&amp;amp;j.*/
			run;
		%end;
	%end;
	
%mend;

%append;

/*Append all draws */

data analysis2_compil (drop=no_of_spins);
	set analysis2_ind_1-analysis2_ind_&amp;amp;nb_row;
run;

data want;
	set analysis2_ind;
	set analysis2_compil;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 30 Nov 2019 10:08:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Generating-numbers-randomly-from-a-table/m-p/608424#M17671</guid>
      <dc:creator>ed_sas_member</dc:creator>
      <dc:date>2019-11-30T10:08:52Z</dc:date>
    </item>
  </channel>
</rss>

