Contributor
Posts: 28

# how to select random observations that may have duplicate id

I have data like this:

A       01/22/2012     2/22/2012     0

A                                                   23.00

A                                                   5.00

A                                                   -23.00

...

Each ID may have several hundred rows, but different values for Admit, Discharge and Paid_Amount.  I need to randomly select 10 IDs from the dataset.  If I use the SURVEYSELECT to, it will return 10 IDs but not all the observations associated with the ID.  Is there a way to select 10 IDs and all observations with the IDs?

Super User
Posts: 6,785

## Re: how to select random observations that may have duplicate id

Does it have to select exactly 10 IDs, or could it be approximately 10?

Do you know how many IDs are in the data?

Does the selection have to be 100% statistically random, or would a somewhat random selection be acceptable?

There are many ways to skin this cat, but your answers determine whether a quick, easy way is possible.

Contributor
Posts: 28

## Re: how to select random observations that may have duplicate id

It could be approximately 10, there are 1751 distinct IDs.  I just need to select around 10 to check some details, but the exact number isn't important.  Does not have to be statistically random, just any 10 mostly random is fine, so yes somewhat random would be definitely acceptable as long as all of the observations per ID is accounted for.

Contributor
Posts: 52

## Re: how to select random observations that may have duplicate id

One can use a 1-(DATA STEP) solution.

First, some simulated data;

Second, a proposed 1-(DATA STEP) solution. Here, one wants to chose the minimum of (10, (# unique id)) = zSize = min(10,zId); One can use some number other than 10.

/*****************************/
/**** some simulated data ****/
/*****************************/
data t_a;
do id = 100 to 120;
do store_id = 'A','B','C','D';
Sales = ceil(500*ranuni(3));
output;
end;
end;
run;

/*****************************/
/**** a proposed solution ****/
/*****************************/
data t_b(keep=id store_id Sales);
do until(zDone);
set t_a(keep=id) end=zDone;
by id;
if first.id then zId+1;
end;

zSize = min(10,zId);

do until(xDone);
set t_a end=xDone;
by id;

if first.id then do;
r1= ceil(zId*ranuni(3));
if (r1 <= zSize) then do; zOutput=1; zId+(-1); zSize+(-1); end;
else do; zOutput=0; zId+(-1); end;
end;

if (zOutput=1) then output;
end;
run;

Discussion stats
• 3 replies
• 233 views
• 0 likes
• 3 in conversation