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/)
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.
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
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; read all var _all_ ; 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;
Edited: Adding MIN() function.
Catch the best of SAS Innovate 2025 — anytime, anywhere. Stream powerful keynotes, real-world demos, and game-changing insights from the world’s leading data and AI minds.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.