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!

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 6 replies
  • 1596 views
  • 5 likes
  • 2 in conversation