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

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 2677 views
  • 0 likes
  • 3 in conversation