Help using Base SAS procedures

randomly selection of records

Accepted Solution Solved
Reply
Super Contributor
Super Contributor
Posts: 440
Accepted Solution

randomly selection of records

i have a  file of students and  each student can appear more than once in it.

 

student score

100        A

100       B

100       C

101       A

101      B

102     A

103    B

103    A

103    C

.......

.........

i need to randomly select maximum to 2 records per student

 

Any idea how to achieve this guys please? Smiley Happy


Accepted Solutions
Solution
‎02-11-2016 04:02 PM
Super User
Posts: 10,550

Re: randomly selection of records

I think this might get you started. Since you may have some students with only one score if we want to use surveyselect which is designed for random sampling we need to describe how many records to pull for each student so the Proc Freq and Data step create a record for each student and if they only have one score then you get one requested, otherwise 2. Note that the output dataset from Surveyselect, Want, will have additional variables that can be used for weighting and tell what the probability of selection was.

proc freq data=have noprint;
   tables student/list out=temp;
run;

data samplesize;
   set temp;
   if count > 1 then _nsize_ = 2;
   else _nsize_ = 1;
run;

proc surveyselect data=have out=want
  sampsize=samplesize ;
  strata student;
run;

View solution in original post


All Replies
Solution
‎02-11-2016 04:02 PM
Super User
Posts: 10,550

Re: randomly selection of records

I think this might get you started. Since you may have some students with only one score if we want to use surveyselect which is designed for random sampling we need to describe how many records to pull for each student so the Proc Freq and Data step create a record for each student and if they only have one score then you get one requested, otherwise 2. Note that the output dataset from Surveyselect, Want, will have additional variables that can be used for weighting and tell what the probability of selection was.

proc freq data=have noprint;
   tables student/list out=temp;
run;

data samplesize;
   set temp;
   if count > 1 then _nsize_ = 2;
   else _nsize_ = 1;
run;

proc surveyselect data=have out=want
  sampsize=samplesize ;
  strata student;
run;
Super Contributor
Super Contributor
Posts: 440

Re: randomly selection of records

thanks ballardw

works  perfectly

interesting approach

 

thx again

Respected Advisor
Posts: 3,777

Re: randomly selection of records

The SELECTALL option provides a simplier solution.  No counting required.

data student;
   input (student score)($);
   cards;
100 A
100 B
100 C
101 A
101 B
102 A
103 B
103 A
103 C
;;;;
   run;
proc print;
   run;
proc surveyselect data=student seed=4354 n=2 out=sample2 selectall;
   strata student;
   run;
proc print;
   run;

Capture.PNG

Super User
Posts: 10,550

Re: randomly selection of records

Good point @data_null__ I wish I could say that I was providing a more general solution where you had different selection rules based on counts within strata. But I just forgot the SELECTALL option as none of my stratified data gets close to the using all of the records in a stratum.

Super Contributor
Super Contributor
Posts: 440

Re: randomly selection of records

this is even shorter.

Thanks Data_Null, thanks guys

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 386 views
  • 8 likes
  • 3 in conversation