DATA Step, Macro, Functions and more

random selection of subjects

Reply
N/A
Posts: 0

random selection of subjects

hi all,

I have a dataset with multiple subjects, and each subjects have multiple entries. Now I need to select a subset of subjects and all entries of each selected subject will be kept in my output.
Eg, I have the 'subject' variable in the original dataset look like:
1111 2 2 2 3 3 4444 555555
If I need to get 2 random subjects and their corresponding entries in the output file, say subject 1 and 4, then the output file should have the 'subject' variable with
1111 4444.

How can I achieve it in SAS? Thanks!
N/A
Posts: 0

Re: random selection of subjects

Posted in reply to deleted_user
You can use ranuni(0) function to generate a random number and then POINT= option in a SET statement.
data random(keep =subject); sampsize=2; do i=1 to sampsize; pickit=ceil(ranuni(11111)*totobs); set work.table point=pickit nobs=totobs; output; end; stop; run;
Respected Advisor
Posts: 4,173

Re: random selection of subjects

Posted in reply to deleted_user
Ignore this post. Message was edited by: Patrick
Respected Advisor
Posts: 4,173

Re: random selection of subjects

First reorganise your data in a way that you have one subject per observation.

The following code then creates a sample with a given number of observations, selects all observations with the same likelyhood and doesn't select twice the same observation. The code is as provided by SAS.

data work.rsubset(drop=obsleft sampsize);
sampsize=10;
obsleft=totobs;
do while(sampsize>0);
pickit+1;
if ranuni(0) lt sampsize / obsleft then do;
set sasuser.revenue point=pickit
nobs=totobs;
output;
sampsize=sampsize-1;
end;
obsleft=obsleft-1;
end;
stop;
run;

Vasile: Your code could pick the same observation more than once (i.e. ranuni returns once 0.8... and once 0.7....).

HTH
Patrick Message was edited by: Patrick
N/A
Posts: 0

Re: random selection of subjects

Patrick, your observation is correct, thanks.
Ask a Question
Discussion stats
  • 4 replies
  • 170 views
  • 0 likes
  • 2 in conversation