<?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: Need help in performing simulation in SAS in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Need-help-in-performing-simulation-in-SAS/m-p/766211#M242811</link>
    <description>&lt;P&gt;You need calling&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13684"&gt;@Rick_SAS&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is an example if you have SAS/IML .&lt;/P&gt;
&lt;PRE&gt;data have;
input ID   col_A   col_B   col_C;
cards;
101    2        80        20
102    4       40         22
103    5       20         25
104    3       90         20
105    8       10         22
101    2        80        20
102    4       40         22
103    5       20         25
104    3       90         20
105    8       10         22
101    2        80        20
102    4       40         22
103    5       20         25
104    3       90         20
105    8       10         22
104    3       90         20
105    8       10         22
101    2        80        20
102    4       40         22
103    5       20         25
104    3       90         20
105    8       10         22
102    4       40         22
103    5       20         25
104    3       90         20
105    8       10         22
;

%let n_simulation=10000;

proc iml;
use have nobs nobs;
read all var _all_ ;
close;
want=j(&amp;amp;n_simulation.,1,.);
call randseed(12345678);

do i=1 to &amp;amp;n_simulation.;
  call randgen(rand_no,'uniform');
  N=min(nobs,int(sample(col_C,1)*rand_no)+1); 
  want[i]=sample(col_B,N,'wor')[:];
end;

create want var{want};
append;
close;
quit;
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Edited: Adding MIN() function.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 06 Sep 2021 12:39:18 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2021-09-06T12:39:18Z</dc:date>
    <item>
      <title>Need help in performing simulation in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-help-in-performing-simulation-in-SAS/m-p/766198#M242802</link>
      <description>&lt;P&gt;Hi guys ! I am new to sas and need some help in simulation. I have a dataset that looks like this (name : simul_input)&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;ID&amp;nbsp; &amp;nbsp;col_A&amp;nbsp; &amp;nbsp;col_B&amp;nbsp; &amp;nbsp;col_C&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;101&amp;nbsp; &amp;nbsp; 2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 80&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 20&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;102&amp;nbsp; &amp;nbsp; 4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;40&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;22&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;103&amp;nbsp; &amp;nbsp; 5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;20&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;25&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;104&amp;nbsp; &amp;nbsp; 3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;90&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;20&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;105&amp;nbsp; &amp;nbsp; 8&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;10&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;22&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;100 data points&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;step 1) The simulation takes col_C and and a random number (rand_no) and computes another number N (N = col_C*rand_no)&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;step 2) Now, for each step of my simulation i need to select N elements at random (without replacement) from col_B and compute their sum, and then store this sum in a&amp;nbsp; separate dataset (lets say : simul_output).&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;so if I run this simulation say 10,000 times, my 'simul_output' should contain 10,000 entries.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;(in general : If I am running the simulation for N number of times, step1 and step2 will take place N times. and 'simul_data' will contain N entries)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a pretty standard code in sas which lets me select&amp;nbsp;&lt;SPAN&gt;&amp;nbsp;"k" random entries from a variable with "n" observation&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;here is the code :&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;PRE class="sas"&gt;&lt;SPAN&gt;data&lt;/SPAN&gt; want&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;keep&lt;/SPAN&gt; = id &lt;SPAN&gt;x&lt;/SPAN&gt;&lt;SPAN&gt;)&lt;/SPAN&gt;;
   &lt;SPAN&gt;retain&lt;/SPAN&gt; k &lt;SPAN&gt;5&lt;/SPAN&gt;;
   &lt;SPAN&gt;set&lt;/SPAN&gt; have nobs = &lt;SPAN&gt;n&lt;/SPAN&gt;;
   &lt;SPAN&gt;if&lt;/SPAN&gt; &lt;SPAN&gt;rand&lt;/SPAN&gt; &lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;"uniform"&lt;/SPAN&gt;&lt;SPAN&gt;)&lt;/SPAN&gt; &amp;lt; k/&lt;SPAN&gt;n&lt;/SPAN&gt; &lt;SPAN&gt;then&lt;/SPAN&gt; &lt;SPAN&gt;do&lt;/SPAN&gt;;
      &lt;SPAN&gt;output&lt;/SPAN&gt;;
      k = k-&lt;SPAN&gt;1&lt;/SPAN&gt;;
   &lt;SPAN&gt;end&lt;/SPAN&gt;;
   &lt;SPAN&gt;n&lt;/SPAN&gt; = n-&lt;SPAN&gt;1&lt;/SPAN&gt;;
&lt;SPAN&gt;run&lt;/SPAN&gt;;&lt;/PRE&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&lt;SPAN&gt;(code obtained from :&amp;nbsp;&lt;A href="https://sasnrd.com/sas-random-sampling-without-replacement/" target="_blank" rel="nofollow noopener noreferrer"&gt;https://sasnrd.com/sas-random-sampling-without-replacement/&lt;/A&gt;)&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 06 Sep 2021 11:55:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-help-in-performing-simulation-in-SAS/m-p/766198#M242802</guid>
      <dc:creator>tumul</dc:creator>
      <dc:date>2021-09-06T11:55:43Z</dc:date>
    </item>
    <item>
      <title>Re: How to perform simulation in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-help-in-performing-simulation-in-SAS/m-p/766199#M242803</link>
      <description>&lt;P&gt;If I take your title "How to perform simulation in SAS" and then paste it into my favorite internet search engine, lots of answers appear.&lt;/P&gt;</description>
      <pubDate>Mon, 06 Sep 2021 11:48:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-help-in-performing-simulation-in-SAS/m-p/766199#M242803</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-09-06T11:48:55Z</dc:date>
    </item>
    <item>
      <title>Re: Need help in performing simulation in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-help-in-performing-simulation-in-SAS/m-p/766206#M242807</link>
      <description>I don't understand if you are using "(without replacement)" , that stands for N&amp;lt;=100 .&lt;BR /&gt;How do you make sure "N = col_C*rand_no" &amp;lt;=100 ? &lt;BR /&gt;How do you define "rand_no"? it is uniform random or integer random ?</description>
      <pubDate>Mon, 06 Sep 2021 12:13:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-help-in-performing-simulation-in-SAS/m-p/766206#M242807</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-09-06T12:13:05Z</dc:date>
    </item>
    <item>
      <title>Re: Need help in performing simulation in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-help-in-performing-simulation-in-SAS/m-p/766210#M242810</link>
      <description>&lt;P&gt;N will always be &amp;lt;= 100 !....it is something like N = min(100, some function of (&lt;SPAN&gt;col_C*rand_no)) [&lt;/SPAN&gt;I did not want to get into the idea behind the computation of N]&amp;nbsp;&lt;/P&gt;&lt;P&gt;Yes ! the rand_no is a uniform random&lt;/P&gt;</description>
      <pubDate>Mon, 06 Sep 2021 12:21:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-help-in-performing-simulation-in-SAS/m-p/766210#M242810</guid>
      <dc:creator>tumul</dc:creator>
      <dc:date>2021-09-06T12:21:20Z</dc:date>
    </item>
    <item>
      <title>Re: Need help in performing simulation in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-help-in-performing-simulation-in-SAS/m-p/766211#M242811</link>
      <description>&lt;P&gt;You need calling&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13684"&gt;@Rick_SAS&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is an example if you have SAS/IML .&lt;/P&gt;
&lt;PRE&gt;data have;
input ID   col_A   col_B   col_C;
cards;
101    2        80        20
102    4       40         22
103    5       20         25
104    3       90         20
105    8       10         22
101    2        80        20
102    4       40         22
103    5       20         25
104    3       90         20
105    8       10         22
101    2        80        20
102    4       40         22
103    5       20         25
104    3       90         20
105    8       10         22
104    3       90         20
105    8       10         22
101    2        80        20
102    4       40         22
103    5       20         25
104    3       90         20
105    8       10         22
102    4       40         22
103    5       20         25
104    3       90         20
105    8       10         22
;

%let n_simulation=10000;

proc iml;
use have nobs nobs;
read all var _all_ ;
close;
want=j(&amp;amp;n_simulation.,1,.);
call randseed(12345678);

do i=1 to &amp;amp;n_simulation.;
  call randgen(rand_no,'uniform');
  N=min(nobs,int(sample(col_C,1)*rand_no)+1); 
  want[i]=sample(col_B,N,'wor')[:];
end;

create want var{want};
append;
close;
quit;
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Edited: Adding MIN() function.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 06 Sep 2021 12:39:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-help-in-performing-simulation-in-SAS/m-p/766211#M242811</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-09-06T12:39:18Z</dc:date>
    </item>
  </channel>
</rss>

