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
- /
- SAS Programming
- /
- General Programming
- /
- How to create (rather true) permutations

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-23-2013 11:51 AM

I use proc plan to create permutation but feel not so satisfied with results. Consider data that have 1500 observations and I want to take 1000 permutations out of all possibilities. Here's the program I write:

data permute1;

**proc** **plan**;

factors n=**1000** obs=**1500** perm / noprint;

output out=permute2;

**run**;

**data** permute3;

set permute2;

set permute1 point=obs;

**run**;

What the permutation does is to do so one by one. That is, the first permutation changes the position of only one pair of variable (ID) and so on. Now I want to make it work a bit harder. E.g., for the above data, is there some way to force each permutation to have, let's say, 500 position changes?

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

Posted in reply to NonSleeper

08-23-2013 12:34 PM

What do you by 1000 permutations of 1500 obs? It sounds like you may want to look at proc surveyselect instead?

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

Posted in reply to NonSleeper

08-23-2013 02:34 PM

What do you mean by *true permutations*? Do you mean *truly random permutations*? - PG

PG

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

Posted in reply to NonSleeper

08-23-2013 03:07 PM

Let me try state the issue more generally. Suppose the data looks like:

data temp;

input ID Var;

1 11

2 22

3 33

......

(N-1) A

N B

;

run;

So we have N observations and their values in this original order. There are N! ways to change (or permute) the ordering of these observation, e.g. consider this (call it permutation 1):

1 11

2 22

3 33

......

N B

(N-1) A

The permutation 1 has only one change in the ordering of observations. I want to make the permutations with larger number of observations' reordering (e.g. 500 times). The **proc plan** above is able to produce all possible permutations but I just want to select some of them based on the number of reordering. Plus, if the number of observations is large (such as 1000) the number of permutations will be 'permutationally' huge. If you know another way to solve this issue, please advise.

If you wonder what it's used for, the next step I will apply the ordering of the original data to the permuted ones. That is equivalent to random assignment of Var to IDs.

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

Posted in reply to NonSleeper

08-23-2013 03:33 PM

That sounds just like a random ordering.

The typical "simple" solution is to create a random variable in the third column and then sort by the random column. If you need to generate a random ID there are other ways. Perhaps, state a bit more clearly the problem you're trying to solve and then people can propose different SAS solutions.

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

Posted in reply to NonSleeper

08-23-2013 04:30 PM

Here's an example for one permutation:

data have;

set have;

random_order = ranuni(12345);

run;

proc sort data=have (drop=id) out=without_id;

by random_order;

run;

data want;

merge have (keep=id) without_id;

run;

But replicating that 500 times becomes more involved. It would probably involve macro language. Also note that you can create many potential sorted orders in one pass of the data:

data have;

set have;

array orders {500} random_order_001 - random_order_500;

do _i_=1 to 500;

orders{_i_} = ranuni(98712345 + _i_);

end;

run;

Finally, note that it is possible (although extremely unlikely) that the same order would be generated more than once.

Good luck.

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

Posted in reply to NonSleeper

08-23-2013 06:03 PM

Try something like this:

**proc plan seed=8768756;**

**factors id=1000 ordered n=1500 random / noprint;**

**output out=permute;**

**run;**

Be careful... this will generate 1.5M observations.

PG

PG