BookmarkSubscribeRSS Feed
NonSleeper
Quartz | Level 8

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?

6 REPLIES 6
Reeza
Super User

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

PGStats
Opal | Level 21

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

PG
NonSleeper
Quartz | Level 8

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.

Reeza
Super User

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.

Astounding
PROC Star

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.

PGStats
Opal | Level 21

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

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!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 710 views
  • 0 likes
  • 4 in conversation