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.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.