turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- Analytics
- /
- Stat Procs
- /
- PROC SURVEYSELECT - sampling without replacement w...

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

08-13-2016 08:34 AM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

08-13-2016 01:40 PM

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

All Replies

Solution

08-14-2016
02:33 AM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

08-13-2016 01:40 PM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

08-13-2016 10:54 PM

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;

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

08-14-2016 02:31 AM

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

08-14-2016 05:43 AM

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;

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

08-14-2016 06:41 AM

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;
```

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

08-14-2016 08:10 AM

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.