BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Xusheng
Obsidian | Level 7

Hi, I am writing a code for sequential simulation with N=5000, state 1 = N(0,3), state 2 = N(5,10), P(state1)=70%, P(state2)=30%. I wrote a code for this simulation and I also want to show how does this simulation look like. However, in my "showing simulation" part, the result given is 10,000 rather than 5,000. Could anyone provide some hint? Thank you.

P.S. Does streaminit (12345) and streaminit(123) make any significant difference? Althogh I can see the results differentiate from different streaminits, I don't quite understand how my result is going to be affected, so any further explanation would be appreciate.

Here is my code:

/* sample from a mixture distribution */
%let N = 5000;
data States(drop=i);
call streaminit(12345);
array prob [2] _temporary_ (0.7 0.3);
do i = 1 to &N;
type = rand("Table", of prob[*]);
if type=1 then Time = rand("Normal", 0, 3); /* state 1 */
else Time = rand("Normal", 5, 10); /* steta 2 */
output;
end;
run;
proc univariate data=States;
ods select Histogram;
histogram Time / vscale=proportion
kernel(lower=0 c=SJPI);
run;
/* Showing simulation */
data A(drop=i);
call streaminit(123);
do i = 1 to 5000;
S1 = rand("Normal", 0, 3); output;
S2 = rand("Normal", 5, 10); output;
end;
run;
proc print data=A;
run;

1 ACCEPTED SOLUTION

Accepted Solutions
s_lassen
Meteorite | Level 14

Xusheng,

You are getting 10000 rows in your "showing simulation" table, because you have an OUTPUT statement after each of the assignment statements

S1 = rand("Normal", 0, 3); output;
S2 = rand("Normal", 5, 10); output;

 

The STREAMINIT function is mostly used if you want to replicate a series of pseudo-random numbers - using STREAMINIT means that you will get the same series if you use the same value in the call. If you just want a different series of pseudo-random numbers each time, there is no need for STREAMINIT.

 

Regards,

Søren

View solution in original post

6 REPLIES 6
art297
Opal | Level 21

There are difference between where you create your sample and where you show your sample.

 

In the one case you assign 2500 to each group .. in the other you run the loop 5000 times, but assign samples to each group on every iteration.

 

Your use of two different call streaminit(x) calls will also cause a difference of which subjects are assigned. It is simply forcing the randomization table to a particular table. If you want the one to be exactly like the other, use the same value in each call.

 

Art, CEO, AnalystFinder.com

 

Xusheng
Obsidian | Level 7

Thank you art297, your answer helped me to understanding the first part better. 

For the second part where I run 5000 loops, why the print result show 10000 numbers for each state?

s_lassen
Meteorite | Level 14

Xusheng,

You are getting 10000 rows in your "showing simulation" table, because you have an OUTPUT statement after each of the assignment statements

S1 = rand("Normal", 0, 3); output;
S2 = rand("Normal", 5, 10); output;

 

The STREAMINIT function is mostly used if you want to replicate a series of pseudo-random numbers - using STREAMINIT means that you will get the same series if you use the same value in the call. If you just want a different series of pseudo-random numbers each time, there is no need for STREAMINIT.

 

Regards,

Søren

Xusheng
Obsidian | Level 7

Thank you Soren,

After read your explanation I realized I was wrong and totally misunderstand the loop. Now I rewrite the code and it works, but does this loop have the same indication as the simulation? 

Here is my code:

/* Showing simulation */
data A(drop=i);
call streaminit(12345);
do i = 1 to 3500;
S1 = rand("Normal", 0, 3); output;
end;
run;
data B(drop=i);
call streaminit(12345);
do i = 1 to 1500;
S2 = rand("Normal", 5, 10); output;
end;
run;
data AllRand;
merge A B;
run;
proc print
data=AllRand;
run;

art297
Opal | Level 21

Why risk there being a difference? Could you just print the data set you initially created, i.e. states?

 

Art, CEO, AnalystFinder.com

 

Xusheng
Obsidian | Level 7

Yes, I printed the result from the first part. And it makes sense. 

Thank you.

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 846 views
  • 0 likes
  • 3 in conversation