Hello. I need to take a sample of 10 random numbers from the Uniform(0,1) distribution then calculate the sample mean. I then need to do this 100 times so that I get 100 sample means all from samples of size 10. Then I need 100 sample means from sample size 100. Then continues on for a total 7 different sample sizes. The resulting data set should have two columns:
1. The sample size of the sample that the sample mean was calculated from
2. The sample mean
It should have 700 entries. 100 for each sample size. I started with nested for loops but I am stuck on how to calculate the sample mean once I obtain the sample. See code below
data uniform (drop = x);
do n = 10, 100, 200, 500, 1000, 5000, 10000 ;
do i = 1 to 100 ;
do a = 1 to n ;
x = rand('Uniform');
end ;
y = mean(x); /* not right.*/
output ;
end;
end;
run ;
I know that this does not work because y = mean(x) will just take the mean of one value- the value of x on the nth loop. I can't figure out how to get the mean of just the sample taken in the third do loop.
Thanks.
You can do this in a data step, you add up all the 10 random numbers and divide by 10.
I would output the individual x values, and then compute the means in PROC SUMMARY
proc summary nway data=have;
class n i;
var x;
output out=means mean=x_mean;
run;
You can do this in a data step, you add up all the 10 random numbers and divide by 10.
I would output the individual x values, and then compute the means in PROC SUMMARY
proc summary nway data=have;
class n i;
var x;
output out=means mean=x_mean;
run;
Thanks, I summed and divided in the data step.
data uniform1 (drop = x);
do n = 10, 100, 200, 500, 1000, 5000, 10000 ;
do i = 1 to 100 ;
sumx = 0 ;
do a = 1 to n ;
x = rand('Uniform');
sumx + x ;
meanx = sumx / n ;
end ;
y = meanx ;
output ;
end;
end;
run ;
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.