## question about using rand function

Occasional Contributor
Posts: 7

# question about using rand function

I'm trying to generate a data set with 1,000 observations with random integers from 1 to 5. Does anyone know how I would do this using the rand function?

Here's the question posed:

Create a temporary SAS data set (Random) consisting of 1,000 observations, each

with a random integer from 1 to 5. Make sure that all integers in the range are

equally likely. Run PROC FREQ to test this assumption

Super Contributor
Posts: 1,636

## Re: question about using rand function

try this:

data have;

do i=1 to 1000;

x=int(rand('uniform')*5)+1;output ;end;

run;

proc freq;

tables x/missing;

run;

Cumulative    Cumulative

x    Frequency     Percent     Frequency      Percent

1         200       20.00           200        20.00

2         216       21.60           416        41.60

3         203       20.30           619        61.90

4         187       18.70           806        80.60

5         194       19.40          1000       100.00

Posts: 5,524

## Re: question about using rand function

or x = ceil(5*rand("UNIFORM"));

PG

PG
Occasional Contributor
Posts: 7

## Re: question about using rand function

THank you very much!!

Posts: 3,852

## Re: question about using rand function

I would use table distribution but what do I know.

rand('TABLE',.2,.2,.2,.2);

Most useful I suppose if you want to fiddle with the probs.

Regular Contributor
Posts: 184

## Re: question about using rand function

One method which assures exactly equal likelihoods:

data notrandom ;

do val = 1 to 5 ; do rep = 1 to 200 ;

order = ranuni(2468) ;

output ;

end ; end ;

run ;

proc sort data=notrandom out=random(keep=val) ;

by order ;

run ;

proc freq data=random ; run ;

```pma85 wrote:

I'm trying to generate a data set with 1,000 observations with random integers from 1 to 5. Does anyone know how I would do this using the rand function?

Here's the question posed:

Create a temporary SAS data set (Random) consisting of 1,000 observations, each
with a random integer from 1 to 5. Make sure that all integers in the range are
equally likely. Run PROC FREQ to test this assumption
```
Posts: 5,524

## Re: question about using rand function

Howles, that technique generates a random ordering, not random values. Given any subset of 999 values from that set, the 1000th value is known without error. In other words, the values are not totally independent.

PG

PG
Discussion stats
• 6 replies
• 449 views
• 0 likes
• 5 in conversation