Random numbers are more useful than you might imagine. They are used extensively in Monte Carlo studies, as well as in many other situations. We will look at two of SAS's random number functions.
- UNIFORM(SEED) - generates values from a random uniform distribution between 0 and 1
- NORMAL(SEED) - generates values from a random normal distribution with mean 0 and standard deviation 1
The statements if x>.5 then coin = 'heads' and else coin = 'tails' create a random variable called coins that has values 'heads' and 'tails'. The data sets random1 and random2 use a seed value of -1. Negative seed values will result in different random numbers being generated each time.
DATA random1; x = UNIFORM(-1); y = 50 + 3*NORMAL(-1); IF x>.5 THEN coin = 'heads'; ELSE coin = 'tails'; RUN; DATA random2; x = UNIFORM(-1); y = 50 + 3*NORMAL(-1); IF x>.5 THEN coin = 'heads'; ELSE coin = 'tails'; RUN; PROC PRINT DATA=random1; VAR x y coin; RUN; PROC PRINT DATA=random2; VAR x y coin; RUN; OBS X Y COIN 1 0.24441 49.7470 heads OBS X Y COIN 1 0.16922 49.1155 tails
Sometimes we will want to generate the same random numbers each time so that we can debug our programs. To do this we just enter the same positive number as the seed value. The data sets random3 and random4 illustrate how to generate the same results each time.
data random3; x = UNIFORM(123456); y = 50 + 3*NORMAL(123456); IF x>.5 THEN coin = 'heads'; ELSE coin = 'tails'; RUN; data random4; x = UNIFORM(123456); y = 50 + 3*NORMAL(123456); IF x>.5 THEN coin = 'heads'; ELSE coin = 'tails'; RUN; PROC PRINT DATA=random3; VAR x y coin; RUN; PROC PRINT DATA=random4; VAR x y coin; RUN; OBS X Y COIN 1 0.73902 48.7832 heads OBS X Y COIN 1 0.73902 48.7832 heads
Now let's generate 100 random coin tosses and compute a frequency table of the results.
DATA random5; DO i=1 to 100; x = UNIFORM(123456); IF x>.5 THEN coin = 'heads'; ELSE coin = 'tails'; OUTPUT; END; RUN; PROC FREQ DATA=random5; table coin; RUN; Cumulative Cumulative COIN Frequency Percent Frequency Percent --------------------------------------------------- heads 48 48.0 48 48.0 tails 52 52.0 100 100.0
Watch out for math errors, such as division by zero, square root of a negative number and taking the log of a negative number.
For information on functions is SAS consult the SAS Language manual.