DATA Step, Macro, Functions and more

How do I Generate Random data with seed and restriction?

Accepted Solution Solved
Reply
Contributor
Posts: 58
Accepted Solution

How do I Generate Random data with seed and restriction?

So I am creating a scoring Macro for a questionnaire while I wait for my data to arrive. 

 

I would like to create a dataset to test out the scoring macro that I have created. 

 

Basically, the dataset would be ID  (1,2,3,4) and Var1-var30, where Var1-30 have randomly generated whole number values between 1 and 6. Also I would like to have a seed to start so I can replicate the same dataset again.  I need maybe 10 records at most. 

 

I did some quick googling and can't seem to figure this out. The rand function I found seem to come up with numbers with decimal values (which is not what I want, I want whole numbers 1,2,3,4,5 or 6).  I think this should be a basic task but I can't seem to figure it out Smiley Sad So I am hoping someone can help me


Accepted Solutions
Solution
‎07-07-2016 01:52 PM
Super User
Posts: 5,513

Re: How do I Generate Random data with seed and restriction?

Low seeds generate approximately random numbers, but that's probably all you will need for test data. 

 

data want;

array var {30};

do obsno=1 to 10;

   id = ceil(ranuni(12345) * 4);

   do seed=1 to 30;

      var{seed} = ceil(ranuni(seed) * 6);

   end;

   output;

end;

run;

View solution in original post


All Replies
Super User
Posts: 11,343

Re: How do I Generate Random data with seed and restriction?

See if this gives you a start.

 



data want;
   array v var1-var30;
   call streaminit (123); /* this makes the random results reproducable*/
   do id=1 to 10;
      do i=1 to dim(v);
         /* the decimal values refer to the proportion of responses that should have
            the order value. ('table', .4, .3, .2, .1) would have 40% with value 1, 
            30% with 2, 20% with 3 and 10% with 1.. The values should total to 1.
            adjust as desired
            The following line says each of the varaibles with have the same overall
            distribution but the values per individual record will vary.
            If you want to have different distributions than you could have 
            single line per var and skip the i loop for array, OR have multiple 
            arrays each group with a similar distribution 
         */
         v[i]= rand('table', .25, .15, .18, .12, .2, .1); 
         
      end;
      output;
   end;
   drop i;
run;
Solution
‎07-07-2016 01:52 PM
Super User
Posts: 5,513

Re: How do I Generate Random data with seed and restriction?

Low seeds generate approximately random numbers, but that's probably all you will need for test data. 

 

data want;

array var {30};

do obsno=1 to 10;

   id = ceil(ranuni(12345) * 4);

   do seed=1 to 30;

      var{seed} = ceil(ranuni(seed) * 6);

   end;

   output;

end;

run;

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 266 views
  • 1 like
  • 3 in conversation