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

    Innovate_SAS_Blue.png

    Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

    If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

    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.

    Get the $99 certification deal.jpg

     

     

    Back in the Classroom!

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

    View all other training opportunities.

    Discussion stats
    • 3 replies
    • 1058 views
    • 0 likes
    • 3 in conversation