BookmarkSubscribeRSS Feed
MikeBurton
Calcite | Level 5

I want to generate a data set from scratch that has j number of variables (n=10) and i number of observations (n=100) with random numbers populating each variable. I have attempted to run do loops, but I am not sure how to incorporate randgen into the code to get random observations.

 

If anyone can help point me in the right direction I'd appreciate it!

8 REPLIES 8
Reeza
Super User

I'm fairly sure that RANDGEN is an IML function not base.

In Base, you can use RAND.

 

data want;
    array myRand(10);

    do nrows=1 to 100;
         do nvars=1 to dim(myRand);
             myRand(i) = rand('normal', 50, 2);
         end;
         output;
    end;
run;
MikeBurton
Calcite | Level 5

I got an error with that code:

 ERROR: Array subscript out of range at line 6 column 14.

ballardw
Super User

@MikeBurton wrote:

I got an error with that code:

 ERROR: Array subscript out of range at line 6 column 14.


An error message without code doesn't help much. Post the log with the code and error into a code box opened with the forum {i} menu icon so we have chance of finding column 14. The forum will reformat text in the main message window.

MikeBurton
Calcite | Level 5

it was from the code Reeza posted above:

 

data want;
    array myRand(10);

    do nrows=1 to 100;
         do nvars=1 to dim(myRand);
             myRand(i) = rand('normal', 50, 2);
         end;
         output;
    end;
run;

Error from log:

1    data want;
2        array myRand(10);
3
4        do nrows=1 to 100;
5             do nvars=1 to dim(myRand);
6                 myRand(i) = rand('normal', 50, 2);
7             end;
8             output;
9        end;
10   run;

NOTE: Variable i is uninitialized.
ERROR: Array subscript out of range at line 6 column 14.
myRand1=. myRand2=. myRand3=. myRand4=. myRand5=. myRand6=. myRand7=. myRand8=. myRand9=.
myRand10=. nrows=1 nvars=1 i=. _ERROR_=1 _N_=1
NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.WANT may be incomplete.  When this step was stopped there were 0
         observations and 13 variables.
NOTE: DATA statement used (Total process time):
      real time           0.19 seconds
      cpu time            0.01 seconds
MikeBurton
Calcite | Level 5

yes, that works now - exactly what I needed. Thanks Reeza

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 8 replies
  • 1261 views
  • 0 likes
  • 3 in conversation