Generate data set with many variables with random numbers as observations.

Reply
Occasional Contributor
Posts: 7

Generate data set with many variables with random numbers as observations.

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!

Super User
Posts: 21,572

Re: Generate data set with many variables with random numbers as observations.

Posted in reply to MikeBurton
Occasional Contributor
Posts: 7

Re: Generate data set with many variables with random numbers as observations.

Base

Super User
Posts: 21,572

Re: Generate data set with many variables with random numbers as observations.

Posted in reply to MikeBurton

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;
Occasional Contributor
Posts: 7

Re: Generate data set with many variables with random numbers as observations.

I got an error with that code:

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

Super User
Posts: 12,148

Re: Generate data set with many variables with random numbers as observations.

Posted in reply to MikeBurton

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.

Occasional Contributor
Posts: 7

Re: Generate data set with many variables with random numbers as observations.

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
Super User
Posts: 21,572

Re: Generate data set with many variables with random numbers as observations.

Posted in reply to MikeBurton

i should be Nvar

Occasional Contributor
Posts: 7

Re: Generate data set with many variables with random numbers as observations.

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

Ask a Question
Discussion stats
  • 8 replies
  • 151 views
  • 0 likes
  • 3 in conversation