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

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;

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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;

View solution in original post

2 REPLIES 2
Reeza
Super User

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;
talzy7
Calcite | Level 5

Thank you this was very helpful!

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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
  • 2 replies
  • 1025 views
  • 0 likes
  • 2 in conversation