Statistical programming, matrix languages, and more

Random combination of a subset of data set

Accepted Solution Solved
Reply
Contributor
Posts: 74
Accepted Solution

Random combination of a subset of data set

Input id $ x y;

 

Datalines;

00-01 5 3

00-02 4 6

00-03 6 4

00-04 7 5

00-05 9 2

00-06 4 6

00-07 5 8

00-08 1 8

00-09 7 3

00-10  7 4

;

run;

Please, I want to generate Z random subset of data set CO chosen observation 4 at a time (10 chose 4). Let’s take Z in this case=7; I like to add additional column to identify each of the random subset—let call that variable REPS. I will appreciate a code that can run fast because the actual code might end up having up to 10,000 REPS. And number of variables can be larger.

 

I read Rick's blog and saw some examples, but I did not see an example where the combination involve actual data set. If it's not possible for more than a variable, I can do only the id (character variable) and merge back to the original data set.

 

Any help will be appreciated!

 

Thanks in Advance

Jack


Accepted Solutions
Solution
‎04-27-2016 07:09 PM
Super User
Posts: 9,875

Re: Random combination of a subset of data set

[ Edited ]

Does PG's code make some sense ?

it IS possible for more than a variable for IML, But need some more code , and might not be friendly . You really want IML code ?

 

data have;
Input id $ x y;
Datalines;
00-01 5 3
00-02 4 6
00-03 6 4
00-04 7 5
00-05 9 2
00-06 4 6
00-07 5 8
00-08 1 8
00-09 7 3
00-10  7 4
;
run;
proc iml;
z=7;
k=4;

use have nobs nobs;
read all var{id x y};
close;
r=rancomb(nobs,k,z);



 new_id=j(k,1,blankstr(nleng(id)));
create want var{reps new_id new_x new_y};
do i=1 to nrow(r);
 idx=r[i,];
 reps=j(k,1,i);
 new_id=id[idx];
 new_x=x[idx];
 new_y=y[idx];
 append;
end;
close;

quit;

 

View solution in original post


All Replies
Super User
Posts: 19,167

Re: Random combination of a subset of data set

Proc surveyselect will work. 

Super User
Posts: 19,167

Re: Random combination of a subset of data set

Selection with or without replacement?

Contributor
Posts: 74

Re: Random combination of a subset of data set

It's basicacally to obtain a random subset of possible permutations of the data set. The reason is that in some settings, computer memory might limit the enumeration of all permutations. Therefor, a sufficiently large number of permutaion of the data set should suffice.

 

Thanks!

J

Respected Advisor
Posts: 4,820

Re: Random combination of a subset of data set

As @Reeza suggested:

 

data CO;
Input id $ x y;
Datalines;
00-01 5 3
00-02 4 6
00-03 6 4
00-04 7 5
00-05 9 2
00-06 4 6
00-07 5 8
00-08 1 8
00-09 7 3
00-10  7 4
;

proc surveyselect data=CO 
    method=srs /* Simple Random sampling without replacement */
    /*method=urs outhits*/ /* Simple Random sampling with replacement */
    reps=10 sampsize=4 
    out=COsamples seed=896358; 
run;

proc print data=COsamples noobs; run;
PG
Contributor
Posts: 74

Re: Random combination of a subset of data set

Incredible!!! 

 

Let me evalute to this. 

Solution
‎04-27-2016 07:09 PM
Super User
Posts: 9,875

Re: Random combination of a subset of data set

[ Edited ]

Does PG's code make some sense ?

it IS possible for more than a variable for IML, But need some more code , and might not be friendly . You really want IML code ?

 

data have;
Input id $ x y;
Datalines;
00-01 5 3
00-02 4 6
00-03 6 4
00-04 7 5
00-05 9 2
00-06 4 6
00-07 5 8
00-08 1 8
00-09 7 3
00-10  7 4
;
run;
proc iml;
z=7;
k=4;

use have nobs nobs;
read all var{id x y};
close;
r=rancomb(nobs,k,z);



 new_id=j(k,1,blankstr(nleng(id)));
create want var{reps new_id new_x new_y};
do i=1 to nrow(r);
 idx=r[i,];
 reps=j(k,1,i);
 new_id=id[idx];
 new_x=x[idx];
 new_y=y[idx];
 append;
end;
close;

quit;

 

Contributor
Posts: 74

Re: Random combination of a subset of data set

You guys are just incredible!!!

 

Many thanks. Let me digest this. 

Super User
Posts: 9,875

Re: Random combination of a subset of data set

This is vectorize operation . Could be very fast.

 

data have;
Input id $ x y;
Datalines;
00-01 5 3
00-02 4 6
00-03 6 4
00-04 7 5
00-05 9 2
00-06 4 6
00-07 5 8
00-08 1 8
00-09 7 3
00-10  7 4
;
run;
proc iml;
z=7;
k=4;

use have nobs nobs;
read all var{id x y};
close;
r=rancomb(nobs,k,z);


 idx=colvec(r);
 reps =colvec(row(r));
 new_id=id[idx];
 new_x=x[idx];
 new_y=y[idx];
create want var{reps new_id new_x new_y};
append;
close;

quit;
Contributor
Posts: 74

Re: Random combination of a subset of data set

All the answers are essentially correct. Thank you all very very much. 

SAS Super FREQ
Posts: 3,630

Re: Random combination of a subset of data set

This blog post explains to how to sample with or without replacement: "Four essential sampling methods in SAS".  It links to other articles that provide details. It shows how to do the sampling by using PROC SURVEYSELECT and by using the SAMPLE function in SAS/IML.

Contributor
Posts: 74

Re: Random combination of a subset of data set

Thanks Rick!

Contributor
Posts: 74

Re: Random combination of a subset of data set

This is correct answer as well
☑ This topic is solved.

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

Discussion stats
  • 12 replies
  • 589 views
  • 2 likes
  • 5 in conversation