BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Sinistrum
Quartz | Level 8

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

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

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

6 REPLIES 6
PGStats
Opal | Level 21

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
Ksharp
Super User
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;

Sinistrum
Quartz | Level 8

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.

Ksharp
Super User
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;


Rick_SAS
SAS Super FREQ

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;
Sinistrum
Quartz | Level 8

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.

 

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is ANOVA?

ANOVA, or Analysis Of Variance, is used to compare the averages or means of two or more populations to better understand how they differ. Watch this tutorial for more.

Find more tutorials on the SAS Users YouTube channel.

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