BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
FastLearner89
Calcite | Level 5

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;

Untitled.png
1 ACCEPTED SOLUTION

Accepted Solutions
Rwon
Obsidian | Level 7

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;

 

View solution in original post

6 REPLIES 6
Rwon
Obsidian | Level 7

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;

 

FastLearner89
Calcite | Level 5
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.

Also, thanks for your help!
Rwon
Obsidian | Level 7

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'.

 

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

ballardw
Super User

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.

 

FastLearner89
Calcite | Level 5
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)?
Rwon
Obsidian | Level 7

Yes, that would still work.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 2730 views
  • 1 like
  • 3 in conversation