DATA Step, Macro, Functions and more

Correct probability program

Reply
Contributor
Posts: 39

Correct probability program

data WORK.Dice;
	ntrials=10000;
	array freq[12];

	do i=2 to 12;
		freq[i]=0;
	end;

	/* zero the freq array */
	do trials=1 to ntrials;
		roll=0;

		do j=1 to 2;
			roll + floor(uniform(0)*6+1);

			/* generate a random integer between 1 and 6 */
		end;
		freq[roll]+1;
	end;
	keep roll frequency probability;
	format roll 4. frequency nlnum16. probability nlnum10.6;
	label roll="Value Rolled" frequency="Frequency" probability="Probability";

	do roll=2 to 12;
		frequency=freq[roll];
		probability=frequency/ntrials;
		output;
	end;
	roll=.;
	frequency=ntrials;
	probability=1;
	output;
	title 'Rolling 2 dice 10000 times';

proc print noobs label;
end;


data e4;
seed=10000;
do i=1 to 1000;
sum=0;
do j= 1 to 5;
x =ranbin(seed,1,.5);
sum=sum+x;
end;
prop=sum/5;
drop i j sum seed x;
output e4;
end;
datalines;
axis length =4 in;
proc gchart data =e4;
vbar prop / type =pct raxis=axis maxis =axis;
run;

data e5;
seed=10000;
do i=1 to 1000;
sum=0;
do j= 1 to 10;
x =ranbin(seed,1,.75);
sum=sum+x;
end;
prop=sum/10;
drop i j sum seed x;
output e5;
end;
datalines;
axis length =4 in;
proc gchart data =e5;
vbar prop / type =pct raxis=axis maxis =axis;
run;

data e6;
seed=10000;
do i=1 to 1000;
sum=0;
do j= 1 to 20;
x =ranbin(seed,1,.95);
sum=sum+x;
end;
prop=sum/20;
drop i j sum seed x;
output e6;
end;
datalines;
axis length =4 in;
proc gchart data =e6;
vbar prop / type =pct raxis=axis maxis =axis;
run;

Data simulal;
do j = 1 to 2000;
	do i = 1 to 5;
	sum=0;
	x[i]= 0+ 1*rannor(2452083);
    sum =sum+x;
    s=std(x[i]);
    avg=sum/5;
    y=s/avg;
    output;
    end;
end;

proc univariate data =simulal plot;
	title1 'Simulated Normal Variate';
    title2 'with Men 0 and Srandard Deviation 1';
    var x;
	var y;
run;

data one;
    seed=123;
	sum=0;
    do i = 1 to 1000;
         x1=ranuni(seed);
		 x2=ranuni(seed);
        if .1<x1+x2<.3 then 	sum=sum+1;
		p = sum/1000;
       standard_error= sqrt(p*(1-p)/1000);
        end;
run;
proc freq data=one;
     table p/standard_error;
run;

1. When I make the porgram run, the title of the first code,Rolling 2 dice 10000 times, will be used for the second. Where I should rewrite to avoid it?

2. In the second code, where I can rewrite the code to change the interval scale of main axis?

3. In the third code,when I do the loop, I want to store every value of x and then get the average and the stadard deviation of them five times a round. Where I can rewrite?

4. In the forth code,I don't wnat the table; I just want to get p and standard error. Where I can rewrite?

 

 

Super User
Posts: 10,466

Re: Correct probability program

First, instead of

floor(uniform(0)*6+1

you would be better off using:

rand('table', 1/6, 1/6, 1/6,1/6,1/6,1/6);

The UNIFORM function has some known issues and the RAND function is a bit better.

 

1) TITLE statements stay in effect until cleared. So if you do something like:

   Title "Some text";

   <code for procedure1>

   <datastep>

   <code for procedure2>

   ...

   <code for proceduren>

The same title will appear with all output through procedure n. Use a blank title statement to clear.

 

2) If you use Proc CHART then you create AXIS statements to define the characteristics such as numbers of tick marks, labels and fonts. Then inside the Proc Gchart reference the Axis in the raxis or maxis option. Often you have a separate axis definition for each. You can have at least up to AXIS99 for 99 concurrent definitions available. If you change to Proc SGPLOT then the YAXIS or Xaxis statements with different syntax perform the same function.

 

3) there are a couple of different ways to interpret your request for this. Please provide a small example maybe 10 records of what you want the output to look like.

 

4) You have a syntax error. I am not sure what you are requesting. Are you looking for an output data set of the frequencies of P and standard_error from data set one? If so you need to request each output set, one per variable from proc freq. Also data one only has one record as you did not include an output inside the do loop. Possibly you meant:

data one;
   seed=123;
   sum=0;
   do i = 1 to 1000;
      x1=ranuni(seed);
      x2=ranuni(seed);
      if .1<x1+x2<.3 then 	sum=sum+1;
      p = sum/1000;
      standard_error= sqrt(p*(1-p)/1000);
      output;
   end;

run;
proc freq data=one noprint;
     tables p / out= FreqP;
     tables standard_error / out = FreqSe;
run;
Contributor
Posts: 39

Re: Correct probability program

3.

	do i = 1 to 5;
	sum=0;
	x[i]= 0+ 1*rannor(2452083);
  

If I simulate the program, I want get 5 random numbers and 5 numbers are a group.

And then I want to get 2000 groups to calculate the average of each of them and then get the standard deviation.

Data simulal;
do j = 1 to 2000;
	do i = 1 to 5;
	sum=0;
	x[i]= 0+ 1*rannor(2452083);
    sum =sum+x;
    s=std(x[i]);
    avg=sum/5;
    y=s/avg;
    output;
    end;
end;

proc univariate data =simulal plot;
	title1 'Simulated Normal Variate';
    title2 'with Men 0 and Srandard Deviation 1';
    var x;
	var y;
run;
Ask a Question
Discussion stats
  • 2 replies
  • 164 views
  • 3 likes
  • 2 in conversation