BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Dinurik
Fluorite | Level 6

Dear SAS Programmers,

 

How do I generate an array and fill it with random binomial data? 

Say, I need an array with 100 rows and 5 columns, and I want the probability to be equal in all columns, say p=0.1.

My current code (sorry, if it's totally stupid) is:

 

data Table (keep=aids);
call streaminit(4321);
array p[5] _temporary_ (repeat(0.1, 5));
do i = 1 to 500;
aids = rand("Table", of p[*]);
output;
end; end;
run;

 

First of all, the repeat function in the array does not work (because it probably shouldn't be there). But when I replace it with (0.1, 0.1, 0.1, 0.1, 0.1) the code produces a single variable with 500 entries instead of arranging the results in an array. 

 

Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
data want;

array col(5);

do nrows=1 to 100;

    do i=1 to dim(col);

           col(i) = rand('binomial', 0.1);

      end;

     output;

end;

run;

Depends a bit on what you mean by 

I want the probability to be equal in all columns, say p=0.1.

 

Random usually means that you won't have equal distributions, but the probability should be 0.1 across all observations anyways, dependent on sample size of course.


@Dinurik wrote:

Dear SAS Programmers,

 

How do I generate an array and fill it with random binomial data? 

Say, I need an array with 100 rows and 5 columns, and I want the probability to be equal in all columns, say p=0.1.

My current code (sorry, if it's totally stupid) is:

 

data Table (keep=aids);
call streaminit(4321);
array p[5] _temporary_ (repeat(0.1, 5));
do i = 1 to 500;
aids = rand("Table", of p[*]);
output;
end; end;
run;

 

First of all, the repeat function in the array does not work (because it probably shouldn't be there). But when I replace it with (0.1, 0.1, 0.1, 0.1, 0.1) the code produces a single variable with 500 entries instead of arranging the results in an array. 

 

Thank you!


 

View solution in original post

6 REPLIES 6
Reeza
Super User
data want;

array col(5);

do nrows=1 to 100;

    do i=1 to dim(col);

           col(i) = rand('binomial', 0.1);

      end;

     output;

end;

run;

Depends a bit on what you mean by 

I want the probability to be equal in all columns, say p=0.1.

 

Random usually means that you won't have equal distributions, but the probability should be 0.1 across all observations anyways, dependent on sample size of course.


@Dinurik wrote:

Dear SAS Programmers,

 

How do I generate an array and fill it with random binomial data? 

Say, I need an array with 100 rows and 5 columns, and I want the probability to be equal in all columns, say p=0.1.

My current code (sorry, if it's totally stupid) is:

 

data Table (keep=aids);
call streaminit(4321);
array p[5] _temporary_ (repeat(0.1, 5));
do i = 1 to 500;
aids = rand("Table", of p[*]);
output;
end; end;
run;

 

First of all, the repeat function in the array does not work (because it probably shouldn't be there). But when I replace it with (0.1, 0.1, 0.1, 0.1, 0.1) the code produces a single variable with 500 entries instead of arranging the results in an array. 

 

Thank you!


 

Dinurik
Fluorite | Level 6

Hi Reeza,

 

Thanks a lot. I tried:

 

data want;
     call streaminit(4321);
     array col(5);
       do nrows=1 to 100;
          do i=1 to dim(col);
          col(i) = rand('Bernoulli', 0.1);
       end;
  output;
  end;
run;

 

And it worked. 

 

Is there any resource/article, that you could refer me to so I can teach myself on this topic?

Reeza
Super User

Simulating data or just generate data step logic?

For simulating data, Rick Wicklins blog and book are the best references:

https://www.amazon.ca/Simulating-Data-SAS-Rick-Wicklin/dp/1612903320

 


@Dinurik wrote:

 

 

Is there any resource/article, that you could refer me to so I can teach myself on this topic?


 

Dinurik
Fluorite | Level 6

Thanks so much! 

 

What about data step logic? Data step completely throws me off, so I would really appreciate any recommendation of what to read =). 

Reeza
Super User

@novinosrin may have better suggestions. I'm an older user (before StackOverflow, FB or Twitter) so learned via reading the documentation.  

 

Which I would actually highly recommend. 

https://documentation.sas.com/?docsetId=lrcon&docsetTarget=n1lw46l098rlqsn1948oua32fnsl.htm&docsetVe...

Dinurik
Fluorite | Level 6

Wonderful, thanks!

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 6 replies
  • 832 views
  • 5 likes
  • 2 in conversation