I need to Use SAS random number generation functions `RAND()` and a `DO....END loop` to create 100 obs in variable named X then I want to use another DO loop of 500 rounds to generate a total of 500 samples, each with 100 obs. a sample is basically sampling from a standard normal distribution.
I tried the following code but it does not give me what I need:
data A; call streaminit(123); /* set random number seed */ do i = 1 to 100; X = rand("Normal"); /* random number generator */ output; end; do r = 1 to 500 ; if i then X = rand("Normal"); output; end; run;
Any input will be greatly appreciated.
Do I understand you correctly that you want 50,000 records with random X where the first 100 have I=1, the next 100 have I=2, and so on till the last 100 with I=500? If so, you need to nest the DO loops, like so:
data want ;
call streaminit(123) ;
do I = 1 to 500 ;
do _n_ = 1 to 100 ;
X = rand ("normal") ;
output ;
end ;
end ;
run ;
Kind regards
Paul D.
Do I understand you correctly that you want 50,000 records with random X where the first 100 have I=1, the next 100 have I=2, and so on till the last 100 with I=500? If so, you need to nest the DO loops, like so:
data want ;
call streaminit(123) ;
do I = 1 to 500 ;
do _n_ = 1 to 100 ;
X = rand ("normal") ;
output ;
end ;
end ;
run ;
Kind regards
Paul D.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.