Programming the statistical procedures from SAS

PROC SURVEYSELECT - sampling without replacement with consecutive randomly permutating

Accepted Solution Solved
Reply
Contributor
Posts: 42
Accepted Solution

PROC SURVEYSELECT - sampling without replacement with consecutive randomly permutating

Dear community,

 

 

I would be glad, if you could help me with the following issue.

 

My aim is to draw N observations from a N-observation containing data-set, without replacement.

("I want to draw the urn empty, without replacement")

 

Utilizing proc iml, I am only capable of drawing a single value from a column-vector.

 

proc iml;
s = sample(1:20, {20}, "WOR");
print s;
quit;

However, "proc iml" has this nice "WOR" option -

 

"specifies simple random sampling without replacement. After elements are randomly selected, their order is randomly permuted. "

 

This is exactly, what I would like to have.

 

With "PROC SURVEYSELECT", utilizing the "srs" method, I only get the result I would get as with specifying “NoReplace" in the "P

ROC IML" statement -

 

* N denotes the sample size;
%LET N = 100
proc surveyselect data = input method=srs sampsize = &N. out = output; id _all_; run;

 

- just a copy of my input-data.

 

In short, I am looking for the "method=" correspondent to "WOR" in "PROC IML" - that is, that the input data set is "drawn empty".

Likewise, I would like to know, how I can use the "PROC IML"  "sample" function, in order to sample vectors.

 

I would be very glad, if you could help me.

 

Yours sincerely,

Sinisturm

 

 

 


Accepted Solutions
Solution
‎08-14-2016 02:33 AM
Respected Advisor
Posts: 5,056

Re: PROC SURVEYSELECT - sampling without replacement with consecutive randomly permutating

Posted in reply to Sinistrum

So, what you want is to reorder a sample randomly. Simply sort your sample by a random variable:

data myClass;
set sashelp.class;
rnd = rand("uniform");
run;

proc sort data=myClass out=myClass(drop=rnd); 
by rnd; run;

 

PG

View solution in original post


All Replies
Solution
‎08-14-2016 02:33 AM
Respected Advisor
Posts: 5,056

Re: PROC SURVEYSELECT - sampling without replacement with consecutive randomly permutating

Posted in reply to Sinistrum

So, what you want is to reorder a sample randomly. Simply sort your sample by a random variable:

data myClass;
set sashelp.class;
rnd = rand("uniform");
run;

proc sort data=myClass out=myClass(drop=rnd); 
by rnd; run;

 

PG
Super User
Posts: 10,214

Re: PROC SURVEYSELECT - sampling without replacement with consecutive randomly permutating

Posted in reply to Sinistrum
For your second question:


proc iml;
use sashelp.class;
read all var _num_ into x[c=vnames];
close;

v=sample(vnames,1,'wor');
want=x[,v];

print want[c=v l=''];
quit;

Contributor
Posts: 42

Re: PROC SURVEYSELECT - sampling without replacement with consecutive randomly permutating

Thank you very much to both of you -

PG for simplifying and solving my issue (I only want to reorder the sample randomly)

and

Xia Keshan for dealing with my "proc iml" issue.

 

Either, I am too clumsy to get your solution to work, or, I do not have expressed my wish well enough.

What I want to do, is "draw from the rows of a NxE-matrix ", such that I get N row-vectors (matrix is "drawn empty") and write this draws to a data set with N rows and E columns.

 

I just came across this solution, which, I hope, does implement what I strive for:

proc iml;
call randseed(1);
use Sashelp.Cars nobs N;
varNames = {"MPG_City" "Length" "Weight"};
read all var varNames into x[rowname=Model];
close Sashelp.Cars;
 
obsIdx = sample(1:nrow(x), N);   /* sample size=N, rows chosen from 1:NumRows */
s5 = x[obsidx, ];                /* extract subset of rows */
print s5[rowname=(Model[obsIdx]) colname=varNames];

(in the original, sample size is "5"; "nobs N" is added 5 changed to N, by me).

 

Again, thank you a lot.

Super User
Posts: 10,214

Re: PROC SURVEYSELECT - sampling without replacement with consecutive randomly permutating

Posted in reply to Sinistrum
So both question are the same ?
You want draw row vector not column vector ?




proc iml;
use sashelp.class nobs n;
read all var _num_ into x;
close;

idx=ranperm(n);
want=x[idx,];

print want[l=''];
quit;


SAS Super FREQ
Posts: 3,839

Re: PROC SURVEYSELECT - sampling without replacement with consecutive randomly permutating

Posted in reply to Sinistrum

Your syntax will be simpler and your meaning clearer if you use the RANPERM function in IML instead of the SAMPLE function.

Sampling N items without replacement from a set that contains N elements is a permutation of the elements:

proc iml;
s = ranperm(1:20);  /* equivalent to sample(1:20, 20, "WOR"); */
print s;
Contributor
Posts: 42

Re: PROC SURVEYSELECT - sampling without replacement with consecutive randomly permutating

Hello.

Ksharp wrote:
So both question are the same ?
You want draw row vector not column vector ?

Yes, both questions are the same - that is, what I meant with "likewise". I should have expressed my question better - I am sorry for that.


Rick_SAS wrote:

Your syntax will be simpler and your meaning clearer if you use the RANPERM function in IML instead of the SAMPLE function.

Sampling N items without replacement from a set that contains N elements is a permutation of the elements:

proc iml;
s = ranperm(1:20);  /* equivalent to sample(1:20, 20, "WOR"); */
print s;

 

So, this eventually brings together PG's comment and my desire to solve it via "proc iml".

 

Thank you for

- showing me the right commands in SAS, as well as,

- clarifying, what I am actually aiming for (which I should have known by myself).

 

Once again, I am really happy to see that great kind of a help.

 

 

☑ This topic is solved.

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

Discussion stats
  • 6 replies
  • 311 views
  • 6 likes
  • 4 in conversation