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!
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!
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!
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?
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?
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 =).
@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.
Wonderful, thanks!
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!
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.
Ready to level-up your skills? Choose your own adventure.