## How to create (rather true) permutations

Frequent Contributor
Posts: 75

# How to create (rather true) permutations

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?

Super User
Posts: 23,776

## Re: How to create (rather true) permutations

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

Posts: 5,541

## Re: How to create (rather true) permutations

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

PG
Frequent Contributor
Posts: 75

## Re: How to create (rather true) permutations

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.

Super User
Posts: 23,776

## Re: How to create (rather true) permutations

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.

Super User
Posts: 6,785

## Re: How to create (rather true) permutations

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.

Posts: 5,541

## Re: How to create (rather true) permutations

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
Discussion stats
• 6 replies
• 255 views
• 0 likes
• 4 in conversation