Assignmnet: Simple Random Sampling With Replacement

Download Report

Transcript Assignmnet: Simple Random Sampling With Replacement

Sampling With Replacement
How the program works
a54p10.sas
The Simulation part of SRSWR
N=3,
n=2
%LET total=6;
*Total number of elements in population;
•
•
%LET tsamp=3;
*Total number of elements in the sample;
•
%LET nsamp=40;
*Number of samples to Select
;
DATA d (KEEP=sample sid1-sid3 s1-s3 );
SET pop1;
ARRAY x{&total} ;
*Original values;
ARRAY id{&total};
*Original ids;
ARRAY s{&tsamp};
*Sample;
ARRAY sid{&tsamp};
DO sample=1 to &nsamp;
DO j=1 TO &tsamp;
*Randomly select permutations;
rn=INT(&total*RANUNI(54231))+1;
s{j}=x{rn};
sid{j}=rn;
END;
OUTPUT;
END;
RUN;
The Simulation part of SRSWR
N=3, n=2 (without macro vars)
DATA d (KEEP=sample sid1-sid3 s1-s3 );
SET pop1;
ARRAY x{6} ;
*Original values of response;
ARRAY id{6};
*Original subject ids;
ARRAY s{2};
*Sample values;
ARRAY sid{2};
*Sample subject ids;
DO sample=1 to 40;
* Number of samples to select;
DO j=1 TO 2;
* Randomly select a subject;
rn=INT(6*RANUNI(54231))+1;
s{j}=x{rn};
sid{j}=rn;
END;
OUTPUT;
END;
RUN;
Uniform random number generator
RANUNI(54231)
•A function in SAS
•It picks at random a number between 0 and 1
Example:
x=RANUNI(3242);
x=0.321234
Problem: Write a SAS program to generate 10 uniform random numbers
between 0 and 1, and list the numbers.
Uniform random number generator
Example:
x=RANUNI(3242);
x=0.321234
Goal: To randomly select a subject in a population of N=6 subjects.
Strategy:
Multiply random number by (N): x1=6*RANUNI (3242);
Example:
x1=1.9274
The random number now ranges from 0 to 6.
Take the integer part:
x2=INT(6*RANUNI(3242));
Example:
x2=1
Add 1 to make it go from 1 to N: x3=INT(6*RANUNI(3242))+1;
Example:
x3=2;