Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- Home
- /
- Programming
- /
- SAS Procedures
- /
- randomly selection of records

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

🔒 This topic is **solved** and **locked**.
Need further help from the community? Please
sign in and ask a **new** question.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Posted 02-11-2016 03:31 PM
(935 views)

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? 🙂

1 ACCEPTED SOLUTION

Accepted Solutions

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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;
```

5 REPLIES 5

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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;
```

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

thanks ballardw

works perfectly

interesting approach

thx again

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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;
```

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

this is even shorter.

Thanks Data_Null, thanks guys

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

**If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. **

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.