<?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: SAS simulate with random variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/SAS-simulate-with-random-variable/m-p/724445#M224922</link>
    <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/372804"&gt;@schnuz&lt;/a&gt;&amp;nbsp;and welcome to the SAS Support Communities!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do you want something like this?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Create sample data for demonstration */

data table1;
input col1 $ col2;
cards;
A 1
A 2.3
B 5
B 0.7
C 8
D 1.6
;

%let num=10; /* number of simulations */

/* Prepare lookup table of random numbers */

data rand_list;
call streaminit(27182818);
retain i;
set table1(keep=col1);
by col1;
if first.col1;
do i=1 to &amp;amp;num;
  rv=rand('uniform');
  output;
end;
run;

proc sort data=rand_list;
by i;
run;

/* Replicate dataset TABLE1 to have &amp;amp;num copies */

proc surveyselect data=table1 noprint
  out=monte_carlo(rename=(replicate=i))
  rep=&amp;amp;num samprate=1;
run;

/* Simulate function */

data monte_carlo;
merge monte_carlo
      rand_list;
by i col1;
f=2*col2+rv;
drop rv; /* optional */
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Dataset RAND_LIST:&lt;/P&gt;
&lt;PRE&gt;Obs     i    col1        rv

  1     1     A      0.5321
  2     1     B      0.0085
  3     1     C      0.8897
  4     1     D      0.0685
  5     2     A      0.4181
  6     2     B      0.5169
...
 39    10     C      0.6392
 40    10     D      0.1856&lt;/PRE&gt;
&lt;P&gt;Dataset MONTE_CARLO:&lt;/P&gt;
&lt;PRE&gt;Obs     i    col1    col2       f

  1     1     A       1.0     2.5321
  2     1     A       2.3     5.1321
  3     1     B       5.0    10.0085
  4     1     B       0.7     1.4085
  5     1     C       8.0    16.8897
  6     1     D       1.6     3.2685
  7     2     A       1.0     2.4181
  8     2     A       2.3     5.0181
  9     2     B       5.0    10.5169
 10     2     B       0.7     1.9169
...
 59    10     C       8.0    16.6392
 60    10     D       1.6     3.3856&lt;/PRE&gt;</description>
    <pubDate>Mon, 08 Mar 2021 10:01:15 GMT</pubDate>
    <dc:creator>FreelanceReinh</dc:creator>
    <dc:date>2021-03-08T10:01:15Z</dc:date>
    <item>
      <title>SAS simulate with random variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-simulate-with-random-variable/m-p/724407#M224900</link>
      <description>&lt;P&gt;1) Table1 has Col1 and Col2&lt;/P&gt;&lt;P&gt;2) Table2 is distinct Col1 as Col3 and generated random variable Col4&lt;/P&gt;&lt;P&gt;3) Simulate a function Col2*2 + Col4 where Col4 is based on Table1.Col1 = Table2.Col3&lt;/P&gt;&lt;P&gt;4) Col4 is diff for each trial&lt;/P&gt;&lt;P&gt;5) I don't really know how to do this but tried the below. Any help will be really appreciated.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data monte_carlo;
%let num = 10;
do i = 1 to &amp;amp;num;
	do while(not eof);
		set Table2 end=eof;
		Col4 = rand('uniform'); 
		put Col4;
	end;
	do while(not eof);
		set Table1 end=eof;
		Col2*2 + Col4;
	end;
end;
run;2&lt;/CODE&gt;&amp;nbsp;2&amp;nbsp;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 08 Mar 2021 09:46:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-simulate-with-random-variable/m-p/724407#M224900</guid>
      <dc:creator>schnuz</dc:creator>
      <dc:date>2021-03-08T09:46:51Z</dc:date>
    </item>
    <item>
      <title>Re: SAS simulate with random variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-simulate-with-random-variable/m-p/724410#M224901</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;delete and repost&lt;/P&gt;</description>
      <pubDate>Mon, 08 Mar 2021 09:42:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-simulate-with-random-variable/m-p/724410#M224901</guid>
      <dc:creator>schnuz</dc:creator>
      <dc:date>2021-03-08T09:42:19Z</dc:date>
    </item>
    <item>
      <title>Re: SAS simulate with random variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-simulate-with-random-variable/m-p/724429#M224912</link>
      <description>&lt;P&gt;I do not see any description of how Col4 depends on other variable(s).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please do not post the description of your problem in the code or text boxes as those do not wrap text and it is extremely hard to read a narrative. The code/text box is for code and text data. The main message window is for long narrative.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 08 Mar 2021 08:45:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-simulate-with-random-variable/m-p/724429#M224912</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-03-08T08:45:11Z</dc:date>
    </item>
    <item>
      <title>Re: SAS simulate with random variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-simulate-with-random-variable/m-p/724441#M224919</link>
      <description>&lt;P&gt;Hi, apologies for the formatting as I'm new here. Will repost my question.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="schnuz_0-1615195649140.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/55635i3F1A0FB4471A0C94/image-size/medium?v=v2&amp;amp;px=400" role="button" title="schnuz_0-1615195649140.png" alt="schnuz_0-1615195649140.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="schnuz_1-1615195656820.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/55636i12EB27A4D143D8EB/image-size/medium?v=v2&amp;amp;px=400" role="button" title="schnuz_1-1615195656820.png" alt="schnuz_1-1615195656820.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;1) Table1 has Col1 and Col2&lt;/P&gt;&lt;P&gt;2) Table2 is distinct Col1 as Col3 and generated random variable Col4&lt;/P&gt;&lt;P&gt;3) Simulate a function Col2*2 + Col4 where Col4 is based on Table1.Col1 = Table2.Col3&lt;/P&gt;&lt;P&gt;4) Col4 is diff for each trial&lt;/P&gt;&lt;P&gt;5) I don't really know how to do this but tried the below. Any help will be really appreciated.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data monte_carlo;
%let num = 10;
do i = 1 to &amp;amp;num;
	do while(not eof);
		set Table2 end=eof;
		Col4 = rand('uniform'); 
		put Col4;
	end;
	do while(not eof);
		set Table1 end=eof;
		Col2*2 + Col4;
	end;
end;
run;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 08 Mar 2021 09:45:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-simulate-with-random-variable/m-p/724441#M224919</guid>
      <dc:creator>schnuz</dc:creator>
      <dc:date>2021-03-08T09:45:59Z</dc:date>
    </item>
    <item>
      <title>Re: SAS simulate with random variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-simulate-with-random-variable/m-p/724445#M224922</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/372804"&gt;@schnuz&lt;/a&gt;&amp;nbsp;and welcome to the SAS Support Communities!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do you want something like this?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Create sample data for demonstration */

data table1;
input col1 $ col2;
cards;
A 1
A 2.3
B 5
B 0.7
C 8
D 1.6
;

%let num=10; /* number of simulations */

/* Prepare lookup table of random numbers */

data rand_list;
call streaminit(27182818);
retain i;
set table1(keep=col1);
by col1;
if first.col1;
do i=1 to &amp;amp;num;
  rv=rand('uniform');
  output;
end;
run;

proc sort data=rand_list;
by i;
run;

/* Replicate dataset TABLE1 to have &amp;amp;num copies */

proc surveyselect data=table1 noprint
  out=monte_carlo(rename=(replicate=i))
  rep=&amp;amp;num samprate=1;
run;

/* Simulate function */

data monte_carlo;
merge monte_carlo
      rand_list;
by i col1;
f=2*col2+rv;
drop rv; /* optional */
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Dataset RAND_LIST:&lt;/P&gt;
&lt;PRE&gt;Obs     i    col1        rv

  1     1     A      0.5321
  2     1     B      0.0085
  3     1     C      0.8897
  4     1     D      0.0685
  5     2     A      0.4181
  6     2     B      0.5169
...
 39    10     C      0.6392
 40    10     D      0.1856&lt;/PRE&gt;
&lt;P&gt;Dataset MONTE_CARLO:&lt;/P&gt;
&lt;PRE&gt;Obs     i    col1    col2       f

  1     1     A       1.0     2.5321
  2     1     A       2.3     5.1321
  3     1     B       5.0    10.0085
  4     1     B       0.7     1.4085
  5     1     C       8.0    16.8897
  6     1     D       1.6     3.2685
  7     2     A       1.0     2.4181
  8     2     A       2.3     5.0181
  9     2     B       5.0    10.5169
 10     2     B       0.7     1.9169
...
 59    10     C       8.0    16.6392
 60    10     D       1.6     3.3856&lt;/PRE&gt;</description>
      <pubDate>Mon, 08 Mar 2021 10:01:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-simulate-with-random-variable/m-p/724445#M224922</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2021-03-08T10:01:15Z</dc:date>
    </item>
    <item>
      <title>Re: SAS simulate with random variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-simulate-with-random-variable/m-p/724446#M224923</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;do you want something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input col1 $ col2;
cards;
a 1 
a 2.3
b 5
b 0.7
c 8
d 1.6
;
run;


%let num = 3;
data want;
  do until(last.col1);
    set have curobs=curobs;
    by col1; /* I assume that have is sorted by col1 */
    if first.col1 then start = curobs;
    if last.col1  then end = curobs;
  end;

  call streaminit(42);
  do i = 1 to &amp;amp;num.;
    col4 = rand('uniform');

    do point = start to end;
      set have point = point;
      y = col2 * 2 + col4;
      output;
    end;
  end;

  /* drop i start end; */
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Mon, 08 Mar 2021 10:06:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-simulate-with-random-variable/m-p/724446#M224923</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2021-03-08T10:06:03Z</dc:date>
    </item>
    <item>
      <title>Re: SAS simulate with random variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-simulate-with-random-variable/m-p/724471#M224931</link>
      <description>&lt;P&gt;Thank you! I tried your solution and it works. However, I hit a problem when it's based on 3 tables.&lt;BR /&gt;I tried the following but have problem merging the 3rd table in as it's not sorted the same way.&lt;/P&gt;&lt;P&gt;So the function = Col3*2 + Col5 + Col7&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="schnuz_0-1615203436451.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/55644iED359430523C673F/image-size/medium?v=v2&amp;amp;px=400" role="button" title="schnuz_0-1615203436451.png" alt="schnuz_0-1615203436451.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="schnuz_1-1615203445262.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/55645i390AC6D5BEA90197/image-size/medium?v=v2&amp;amp;px=400" role="button" title="schnuz_1-1615203445262.png" alt="schnuz_1-1615203445262.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="schnuz_2-1615203454688.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/55646i4A87C798EA075647/image-size/medium?v=v2&amp;amp;px=400" role="button" title="schnuz_2-1615203454688.png" alt="schnuz_2-1615203454688.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;proc sort data=table1 out= table1;
by col1;
run;

proc sort data=table1 out= table1_1;
by col2;
run;

%let num=10; /* number of simulations */

/* Prepare lookup table of random numbers for ABCD */
data rand_list1;
call streaminit(27182818);
retain i;
set table1(keep=col1);
by col1;
if first.col1;
do i=1 to &amp;amp;num;
  rv1=rand('uniform');
  output;
end;
run;

/* Prepare lookup table of random numbers for XYZ */
data rand_list1;
call streaminit(42);
retain j;
set table1(keep=col2);
by col2;
if first.col2;
do j=1 to &amp;amp;num;
  rv2=rand('uniform');
  output;
end;
run;

proc sort data=rand_list1;
by i;
run;

proc sort data=rand_list2;
by j;
run;

/* Replicate dataset TABLE1 to have &amp;amp;num copies */
proc surveyselect data=table1 noprint
  out=monte_carlo1(rename=(replicate=i))
  rep=&amp;amp;num samprate=1;
run;

/* Replicate dataset TABLE1_1 to have &amp;amp;num copies */
proc surveyselect data=table1_1 noprint
  out=monte_carlo2(rename=(replicate=j))
  rep=&amp;amp;num samprate=1;
run;

/* Simulate function */
data monte_carlo1;
merge monte_carlo1
      rand_list1;
by i col1;
run;

data monte_carlo2;
merge monte_carlo2
rand_list2;
by j col2;
run;

&lt;/PRE&gt;&lt;P&gt;I reckon I need to sort and merge the 2 tables, however, i and j are not common. How should I proceed?&amp;nbsp;&lt;/P&gt;&lt;P&gt;Also Table1 is 15k records and I plan to simulate 100k trials. This seems to get really big.&lt;/P&gt;&lt;P&gt;Many thanks again.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 08 Mar 2021 11:58:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-simulate-with-random-variable/m-p/724471#M224931</guid>
      <dc:creator>schnuz</dc:creator>
      <dc:date>2021-03-08T11:58:55Z</dc:date>
    </item>
    <item>
      <title>Re: SAS simulate with random variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-simulate-with-random-variable/m-p/724472#M224932</link>
      <description>&lt;P&gt;Hi Bart, thanks. I tried Freelance's solution first and it worked. I'm struggling with the problem if there are 3 tables instead of 2. Will try out your solution too later.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 08 Mar 2021 11:42:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-simulate-with-random-variable/m-p/724472#M224932</guid>
      <dc:creator>schnuz</dc:creator>
      <dc:date>2021-03-08T11:42:33Z</dc:date>
    </item>
    <item>
      <title>Re: SAS simulate with random variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-simulate-with-random-variable/m-p/724493#M224935</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/372804"&gt;@schnuz&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Table1 is 15k records and I plan to simulate 100k trials. This seems to get really big.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I see. Then you should turn to more efficient techniques, e.g., use a hash object for the lookup, as shown below.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data table1;
input col1 $ col2 $ col3;
cards;
A X 1
A Y 2.3
A Z 5
B X 0.7
B Z 8
C Y 1.6
C Y 5.6
D X 3.9
;

%let num=100000;

sasfile table1 load;

data monte_carlo;
call streaminit(27182818);
dcl hash h1();
h1.definekey('col1');
h1.definedata('rv1');
h1.definedone();

dcl hash h2();
h2.definekey('col2');
h2.definedata('rv2');
h2.definedone();

do i=1 to &amp;amp;num;
  do _n_=1 to n;
    set table1 nobs=n point=_n_;
    if h1.find() ne 0 then do;
      rv1=rand('uniform');
      h1.add();
    end;
    if h2.find() ne 0 then do;
      rv2=rand('uniform');
      h2.add();
    end;
    f=2*col3+rv1+rv2;
    output;
  end;
  h1.clear();
  h2.clear();
end;
stop;
drop rv:;
run;

sasfile table1 close;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 08 Mar 2021 13:13:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-simulate-with-random-variable/m-p/724493#M224935</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2021-03-08T13:13:52Z</dc:date>
    </item>
    <item>
      <title>Re: SAS simulate with random variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-simulate-with-random-variable/m-p/724751#M225032</link>
      <description>&lt;P&gt;Thank you that works great! I have modified it to have a total that sums f and only output total. The method of doing this is beyond my SAS knowledge so thank you so much for helping and sharing your expertise. I have so much to learn.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;One more question. Along the concept of VaR, if I want to find the total at 99% confidence level, do I use PROC FREQ? Or do I sort the total and calculate manually?&lt;/P&gt;</description>
      <pubDate>Tue, 09 Mar 2021 04:47:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-simulate-with-random-variable/m-p/724751#M225032</guid>
      <dc:creator>schnuz</dc:creator>
      <dc:date>2021-03-09T04:47:45Z</dc:date>
    </item>
    <item>
      <title>Re: SAS simulate with random variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-simulate-with-random-variable/m-p/724777#M225044</link>
      <description>&lt;P&gt;You can compute percentiles, e.g., the lower and upper limit of an interval that contains 99% of the simulated totals (excluding only the lower and upper 0.5% tails of the empirical distribution), with &lt;A href="https://documentation.sas.com/?docsetId=procstat&amp;amp;docsetVersion=9.4&amp;amp;docsetTarget=procstat_univariate_syntax.htm&amp;amp;locale=en" target="_blank" rel="noopener"&gt;PROC UNIVARIATE&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc univariate data=sashelp.stocks;
var volume;
output out=perc pctlpts=0.5 99.5 pctlpre=p;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Resulting dataset &lt;FONT face="courier new,courier"&gt;perc&lt;/FONT&gt;:&lt;/P&gt;
&lt;PRE&gt;Obs      p0_5       p99_5

 1     4477828    140382836&lt;/PRE&gt;</description>
      <pubDate>Tue, 09 Mar 2021 08:31:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-simulate-with-random-variable/m-p/724777#M225044</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2021-03-09T08:31:23Z</dc:date>
    </item>
  </channel>
</rss>

