BookmarkSubscribeRSS Feed
pma85
Calcite | Level 5

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

6 REPLIES 6
Linlin
Lapis Lazuli | Level 10

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

PGStats
Opal | Level 21

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

PG

PG
pma85
Calcite | Level 5

THank you very much!!

data_null__
Jade | Level 19

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.

Howles
Quartz | Level 8

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

PGStats
Opal | Level 21

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 1464 views
  • 0 likes
  • 5 in conversation