Solved
New Contributor
Posts: 3

# Random Sampling

My original data set contains 7 records, with one "Newic-890426" containing 5 entries while the other "NEWIC-900120". I intend to randomly select 2 records (Criteria:UNIQUE NEWIC) using the proc survey select method. But i notice it is still possible that SAS picks two (2) of the same NEWIC.

I do NOT intend to remove the duplicates prior to selecting as the sole reason as to why certain NEWICs have multiple records because they have earlier fulfilled certain criteria, thus enabling them to stand a "higher chance" of being selected.

``````proc surveyselect data=test1 out=test2
method=srs
n=2 NOPRINT;
quit;``````

Accepted Solutions
Solution
‎06-16-2017 12:45 PM
Contributor
Posts: 23

## Re: Random Sampling

Could you elaborate on the number of unique records in the original dataset? You listed 2 unique records and ideally want to randomly select 2 unique records using surveyselect. Is this correct?

Regardless, I think you should look into 'method = pps', which is probability proportion to size, rather than srs. You would need to add an intermediary step to make the records unique, such as proc freq, while keeping their count which would enable them to stand a "higher chance" of being selected.

Here is SAS's explanation of the PPS method:

https://support.sas.com/documentation/cdl/en/statug/63033/HTML/default/viewer.htm#statug_surveyselec...

``````*I used ID to illustrate the method;
data test1;
input id \$3.;
datalines;
012
111
123
456
789
012
111
111
111
;
run;

*Unique ID level with count to be used as the probability of selection;
proc freq data = test1;
tables id / out = test_freq ( keep = id count );
run;

*Random selection with count as the probability of selection of ID;
proc surveyselect data = test_freq out = test2 method = pps n = 2 noprint;
size count;
quit;``````

All Replies
Solution
‎06-16-2017 12:45 PM
Contributor
Posts: 23

## Re: Random Sampling

Could you elaborate on the number of unique records in the original dataset? You listed 2 unique records and ideally want to randomly select 2 unique records using surveyselect. Is this correct?

Regardless, I think you should look into 'method = pps', which is probability proportion to size, rather than srs. You would need to add an intermediary step to make the records unique, such as proc freq, while keeping their count which would enable them to stand a "higher chance" of being selected.

Here is SAS's explanation of the PPS method:

https://support.sas.com/documentation/cdl/en/statug/63033/HTML/default/viewer.htm#statug_surveyselec...

``````*I used ID to illustrate the method;
data test1;
input id \$3.;
datalines;
012
111
123
456
789
012
111
111
111
;
run;

*Unique ID level with count to be used as the probability of selection;
proc freq data = test1;
tables id / out = test_freq ( keep = id count );
run;

*Random selection with count as the probability of selection of ID;
proc surveyselect data = test_freq out = test2 method = pps n = 2 noprint;
size count;
quit;``````

New Contributor
Posts: 3

## Re: Random Sampling

Indeed the original data set only has 2 unique records. I intentionally duplicated some of the entries (IDs) to show that i intend to have certain IDs picked more often than the others.

By the way, that the "size: count" have any contraints tied to it?Eg. Count needs to be more than 1? Or is "count" merely to tell SAS that the sampling needs to be done by taking into account *this* variable which is the frequency of records.

Contributor
Posts: 23

## Re: Random Sampling

The count is the ID frequency output from the proc freq. So, if an ID is in the dataset, then it would have at least a frequency of 1.

Yes, the latter. The 'size count;' is telling SAS that the sampling needs to be done by taking into account this variable which is the frequency of records. This statement needs to be in the procedure if you use 'method=pps'.

Super User
Posts: 13,889

## Re: Random Sampling

If you only want one record for each level of a variable you might try the STRATA statement;

Sort the data by the strata variable and then something like:

proc surveyselect data=have out=want sampsize=1;

strata stratavariablename;

run;

will select one record for each level of the given variable.

You can use any fixed value for sampsize as long as every group of records for a strata has at least that many records.

New Contributor
Posts: 3

## Re: Random Sampling

If i were to replace the "count" with some other identifier, say variable, and each records have different variable identifier, eg.
Id Var
Aa 0.3
Bb 0.1
Cc 2
Dd 3

Would the PPS method still work? I.e. could SAS recognize that ID=Dd stands the highest chance (largest value) followed by Cc, Aa and Bb(smallest value)?
Contributor
Posts: 23