Help using Base SAS procedures

Ranuni ( )

Regular Contributor
Posts: 173

Ranuni ( )


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;
if ran1 < 0.50 then do;
test_cell = 'PM400';

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

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 !
Regular Contributor
Posts: 173

Re: Ranuni ( )

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?

Trusted Advisor
Posts: 2,125

Re: Ranuni ( )

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)."
Respected Advisor
Posts: 3,852

Re: Ranuni ( )

To me the RANTBL function makes this much easier.

Data BB;
/*set AA;*/
do id = 1 to 100;
*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);
Proc Freq data = BB; tables test_cell;
Regular Contributor
Posts: 173

Re: Ranuni ( )

Posted in reply to data_null__
Thank you both for your input !
Ask a Question
Discussion stats
  • 4 replies
  • 3 in conversation