Simple Random Sampling in SAS

Simple Random sampling in SAS is accomplished using ranuni() function or by using PROC SURVEYSELECT Statement. We will see an Example of each method. Also we will look at an example on how to select random n% percentage of samples in SAS, Select N random samples in SAS, Simple Random sampling in SAS with replacement.

  • Select simple random samples using ranuni() Function.
  • Simple random sampling using PROC SURVEYSELECT
  • Select random n samples using ranuni() and PROC SURVEYSELECT
  • Select random n percentage samples using ranuni() and PROC SURVEYSELECT
  • Simple Random sample in SAS with replacement

Simple random sampling in SAS 0

So we will be using CARS Table in our example

Simple random sampling in SAS 1

 

 

Simple Random Sample in SAS – Random n percentage samples : Method 1

Selecting Random n percentage in SAS is accomplished using ranuni() function with condition as shown below

/* Method 1: n percentage samples*/ 

proc sql; 
create table cars_sample_nperc as 
select * from cars where ranuni (100) < 0.5; 
quit; 

So the random 50% of population will be

Simple random sampling in SAS 2

 

 

Simple Random Sample in SAS – Random n samples : Method 1

Selecting Random N samples in SAS is accomplished using ranuni() function. by specifying outobs = N in PROC SQL as shown below


/*Method 1; n samples */ 

proc sql outobs=10; 
create table cars_sample_n as 
select * from cars order by ranuni(100); 
quit;

So the random 10 sample of population will be

Simple random sampling in SAS 3

 

 

Simple Random Sample in SAS – Random n percentage samples : PROC SURVEYSELECT

Selecting Random n percentage in SAS is accomplished using PROC SURVEYSELECT function with samprate = n% as shown below

/* Method 2: proc survey select n percentage samples*/ 

proc surveyselect data=cars  
out = cars_sample_60perc 
method=srs 
samprate=0.6; 
run;

So the random 60 % sample of population will be

Simple random sampling in SAS 4

 

 

Simple Random Sample in SAS – Random n samples : PROC SURVEYSELECT

Selecting Random N samples in SAS is accomplished using PROC SURVEYSELECT function. by specifying method = srs & sampsize = N as shown below

/* Method2: proc survey select n samples*/ 

proc surveyselect data=cars  
out = cars_sample_n 
method=srs 
sampsize=10; 
run; 


So the random 10 sample of population will be

Simple random sampling in SAS 5

 

 

Simple Random Sample with Replacement in SAS : PROC SURVEYSELECT

Simple Random sample with replacement in SAS is accomplished using PROC SURVEYSELECT function. by specifying method=srs & sampsize = N and rep=1 as shown below which indicates 10 samples with repetition will be selected.

/* simple random sampling with replacement */

proc surveyselect data=cars method = srs sampsize = 10 
rep=1 seed=12345 out=cars_rep_n; 
run; 

Simple random sampling with repetition will be

Simple random sampling in SAS 6

 

                                                                                         

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.