Calcite | Level 5

## Need help in performing simulation in SAS

Hi guys ! I am new to sas and need some help in simulation. I have a dataset that looks like this (name : simul_input)

ID   col_A   col_B   col_C
101    2        80        20
102    4       40         22
103    5       20         25
104    3       90         20
105    8       10         22
.
.
.
.
100 data points

step 1) The simulation takes col_C and and a random number (rand_no) and computes another number N (N = col_C*rand_no)

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  separate dataset (lets say : simul_output).

so if I run this simulation say 10,000 times, my 'simul_output' should contain 10,000 entries.

(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)

I have a pretty standard code in sas which lets me select  "k" random entries from a variable with "n" observation

here is the code :

 ```data want(keep = id x); retain k 5; set have nobs = n; if rand ("uniform") < k/n then do; output; k = k-1; end; n = n-1; run;```

(code obtained from : https://sasnrd.com/sas-random-sampling-without-replacement/)

4 REPLIES 4
Diamond | Level 26

## Re: How to perform simulation in SAS

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.

--
Paige Miller
Super User

## Re: Need help in performing simulation in SAS

I don't understand if you are using "(without replacement)" , that stands for N<=100 .
How do you make sure "N = col_C*rand_no" <=100 ?
How do you define "rand_no"? it is uniform random or integer random ?
Calcite | Level 5

## Re: Need help in performing simulation in SAS

N will always be <= 100 !....it is something like N = min(100, some function of (col_C*rand_no)) [I did not want to get into the idea behind the computation of N]

Yes ! the rand_no is a uniform random

Super User

## Re: Need help in performing simulation in SAS

You need calling @Rick_SAS

Here is an example if you have SAS/IML .

```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;
close;
want=j(&n_simulation.,1,.);
call randseed(12345678);

do i=1 to &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;
```