I am having trouble with a homework exercise regarding geometric distribution.The exercise states:
Use a DATA STEP to generate a random sample of 1,000 observations from the geometric distribution for each of p=.2,.4,.6. Use a PROC SQL step to create a data set geomnum that contains the number of occurrences for each combination of p and x (the random geometric). Use PROC SGPLOT to graph the distribution of the number of trials needed for each probability. All of the plots should appear on the same graph. Use a series plot and include markers on the plot.
My understanding is that I am counting the number of times a certain x exists for each p value but I don't believe my code is doing this. Any tips is greatly appreciated. I have attached my code:
%let seed=54321; %let numobs=1000; data geometrics; call streaminit(&seed); do i=1 to &numobs; x1=rand("geometric",0.2); output; end; do i=1 to &numobs; x2=rand("geometric",0.4); output; end; do i=1 to &numobs; x3=rand("geometric",0.6); output; end; run; proc sql; create table geomnum as select x1, count(x1) as freq1, x2, count(x2) as freq2, x3, count(x3) as freq3 from geometrics group by x1,x2,x3 ; run;
Try restructuring your data to be long instead of wide - and then group by p and x instead of dealing with multiple values..
data geometrics;
call streaminit(&seed);
do p=0.2 to 0.6 by 0.2;
do i=1 to &numobs;
x=rand("geometric", p);
output;
end;
end;
run;
proc sql;
create table want as
select p, x, count(*) as freq
from geometrics
group by x, p;
quit;
Try restructuring your data to be long instead of wide - and then group by p and x instead of dealing with multiple values..
data geometrics;
call streaminit(&seed);
do p=0.2 to 0.6 by 0.2;
do i=1 to &numobs;
x=rand("geometric", p);
output;
end;
end;
run;
proc sql;
create table want as
select p, x, count(*) as freq
from geometrics
group by x, p;
quit;
Thank you this was very helpful!
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.