BookmarkSubscribeRSS Feed
Hugo123
Calcite | Level 5


I am trying to simulate combinations based on 5 coordinates (datset below):

data coord;

input x 1. y 1.;

datalines;

11

32

55

91

14

run;

Every combination must begin with (1,1) and end with (1,1) i.e.

(1,1) -> (3,2)-> (5,5)-> (9,1)-> (1,4)-> (1,1)

and all combinations must be unique and finally that there can't be the above route and

(1,1) ->(1,4)-> (9,1)-> (5,5)-> (3,2)-> (1,1)

as both routes are the same but in opposite directions.

Is there a way to do this in sas?

3 REPLIES 3
ballardw
Super User

What do expect the output data set to look like?

ballardw
Super User

How many points ( pairs ) are you going to be using.

The following creates a text ordered pair variable and permutes them. Uniqueness I'm not sure quite what your rules may be. Note that if you are using many pairs that the number of records for output goes up as N!.

data coord;
input x 1. y 1. ;
length coord $ 8;
coord=catt(x,',',y);
retain start ;
if _n_= 1 then start= coord;
datalines;
11
32
55
91
14
run;

/* flip the column to a row to get the ordered pairs in one record to permute later*/

proc transpose data=coord out=coordtrans;
by start;
var coord;
run;

/* you could trop some of the variables n, nfact, j. also the COL1 or start will have your first pair

so you also have it available as an end */

data next;
   set coordtrans;
   array c Col2-col5; /* change the 5 to the number of pairs used*/
   n=dim(c);
   nfact=fact(n);
   do j=1 to nfact;
      call lexperm(j,  of c

  • );
          output;
       end;
    run;
  • Ksharp
    Super User

    It is called all permutations not combinations.

    Want data step code or IML code ?

    Suggest you post it at IML forum, since it is question about simulating data.

    data coord;
    input x 1. y 1.;
    datalines;
    11
    32
    55
    91
    14
    ;
    run;
    proc sql noprint;
     select count(*) into : n
      from coord
       where x ne 1 or y ne 1;
     select quote(cats(x,y)) into : list separated by ' '
      from coord
       where x ne 1 or y ne 1;
    quit;
    
    data _null_;
    array x{&n} $2 (&list);
    n=dim(x);
    nfact=fact(n);
    do i=1 to nfact;
     change=allperm(i, of x{*});
     put i 5. +2 '11' +2 x{*} '11';
    end;
    run;
    
    
    
    
    
    

    Xia Keshan

    sas-innovate-2024.png

    Don't miss out on SAS Innovate - Register now for the FREE Livestream!

    Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

     

    Register now!

    What is Bayesian Analysis?

    Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

    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
    • 3 replies
    • 1168 views
    • 0 likes
    • 3 in conversation