Indexing a pair of variables (a dyad)

Reply
New Contributor
Posts: 3

Indexing a pair of variables (a dyad)

Hi,

 

I have a dataset with two variables (X1,X2)  that denote source-destination pairs. I want to create a new variable X3 that indexes each dyad. That is (1,2) and (2,1) get assigned the same X3. The value of X3 itself is not important. Below is an example.

 

X1 X2  X3

1    2    1

1   12   2

1   15   3

2    1    1

2    4    4 

3    5    5

4    2    4 

 

How can I achieve this in SAS? Thanks in advance for your help.

Super User
Posts: 7,405

Re: Indexing a pair of variables (a dyad)

Say you know the possible maximum value of each variable is 100.

 

Then I'd do

data want;
set have;
x3 = min(x1,x2) * 1000 + max(x1,x2);
run;

One could also do something similar by creating a character variable and putting the Zw. formatted values of X1 and X2 into nibbles of this character variable:

%let maxdigits=4;
data want;
set have;
length x3 $ %eval(&maxdigits*2);
x3 = put(min(x1,x2),z&maxdigits..) !! put(max(x1,x2),z&maxdigits..);
run;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
New Contributor
Posts: 3

Re: Indexing a pair of variables (a dyad)

amazing! thanks a ton for this.

Super User
Posts: 9,865

Re: Indexing a pair of variables (a dyad)

data have;
input X1 X2 ;
cards;
1    2    1
1   12   2
1   15   3
2    1    1
2    4    4 
3    5    5
4    2    4 
;
run;

data have;
 set have;
 _x1=x1;_x2=x2;
 call sortn(_x1,_x2);
run;
proc sort data=have;
 by _x1 _x2;
run;
data want;
 set have;
 by _x1 _x2;
 x3+first._x2;
 drop _:;
run;

New Contributor
Posts: 3

Re: Indexing a pair of variables (a dyad)

Perfect!

 

Once I include this unique X3 as a categorical variable in PROC Genmod as a class variable and interact it with another category variable, I run into MEMSIZE issues.

 

I tried to increase memory in-line by using

 

options memsize=0;

 

But get the following message "WARNING 30-12: SAS option MEMSIZE is valid only at startup of the SAS System.
The SAS option is ignored"


I am somewhat new to SAS hence now sure how to increase memsize.  How do I assign additional memory?

Super User
Posts: 7,405

Re: Indexing a pair of variables (a dyad)

You need to set that either on the commandline of the SAS process, or in the configuration file.

 

This depends on how you use SAS (native through display manager, SAS Studio, Enterprise Guide).

For workspace servers (Studio, EG) it is best to modify the related sasv9_usermods.cfg file.

 

You could also try to first sort by x3, and then use by instead of class.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Posts: 9,865

Re: Indexing a pair of variables (a dyad)


http://blogs.sas.com/content/iml/2015/07/31/large-matrices/


Valued Guide
Posts: 947

Re: Indexing a pair of variables (a dyad)

You want a category assignment for all n-tuples, such that re-ordering elements of the n-tuple yields the same category.  This (untested) program passes through the data once, and does that for dyads, triads, etc:

 

 

data want (drop=_x: I rc);
  set have;
  array x {4};
  array _x {4};

  if _n_=1 then do;
    declare hash base();
      base.definekey('_x1','_x2','_x3','_x4');
      base.definedata('cat');
      base.definedone();
  end;

  do i=1 to 4; _x{I}=x{I};end;
  call sortn(of _x{*});

  rc=base.find();
  if rc^=0 then do ;
     cat=base.num_items+1;
     rc=base.add();
  end;
run;

 

 

    

   

Ask a Question
Discussion stats
  • 7 replies
  • 198 views
  • 2 likes
  • 4 in conversation