BookmarkSubscribeRSS Feed
KevinC_
Fluorite | Level 6
Hello,

I hope I posted this in the right forum. I am trying to figure out how RANUNI works in the following pgm.

Data BB (drop=ran1);
set AA;
ran1=ranuni(1234);
if ran1 < 0.50 then do;
test_cell = 'PM400';
end;

else if ran1 >= 0.50 then do;
test_cell = 'PM401';
end;
run;


Proc Freq data = BB; tables test_cell;

** here is the part of the output that I dont understand*****

test_cell frequency percent
------------------------------------------------
PM400 4194 50.65
PM401 4087 49.35


I understand RANUNI assignes a random number to a variable, in this case 'ran1'. But how does the value of 'ran1' control what records get what test_cell?

Thank you so much for any input !
4 REPLIES 4
KevinC_
Fluorite | Level 6
let me rephrase my question a little:

according to the result (50.65% vs 49.35%) I can see that the value of 'ran1' was evenly distributed between < 0.50 and >= 0.50. Why is that? Is this by design?

Thanks!
Doc_Duke
Rhodochrosite | Level 12
Yes, it is by design. RANUNI() is one of many pseudo random number generators in SAS. In particular, RANUNI is designed to generate pseudo random numbers on the real line between 0 and 1 with a uniform distribution.

From the 9.1.3 documentaiton, "The RANUNI function returns a number that is generated from the uniform distribution on the interval (0,1) using a prime modulus multiplicative generator with modulus 231- and multiplier 397204094 (Fishman and Moore 1982) (See References)."
data_null__
Jade | Level 19
To me the RANTBL function makes this much easier.

[pre]
Data BB;
/*set AA;*/
do id = 1 to 100;
*ran1=ranuni(1234);
*if ran1 < 0.50 then test_cell = 'PM400';
*else if ran1 >= 0.50 then test_cell = 'PM401';

ran1 = rantbl(1234,.5);
length test_cell $5;
test_cell = scan('PM400 PM401',ran1);
output;
end;
run;
Proc Freq data = BB; tables test_cell;
run;
[/pre]
KevinC_
Fluorite | Level 6
Thank you both for your input !

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 5074 views
  • 0 likes
  • 3 in conversation