DATA Step, Macro, Functions and more

Matrix or Array Arrangement

Reply
Occasional Contributor
Posts: 14

Matrix or Array Arrangement

Hello.

I've a matrix of order 61312*3.

1st column represent Origin (from 1 to 81)

2nd column represent Destination (from 1 to 81)

3rd column represent location of survey (1 to 246)

I need a matrix of 3 columns (but rows can increased now instead of 61312), of below type;

Origin Destination Location

1             1                   1

1                2                2 or can be none

1                .                     .

1                81                  246

2                1                     1

.                        .                     .

81                81                     246

Kindly, let me know how should I proceed.

Super User
Posts: 9,681

Re: Matrix or Array Arrangement

Why not post it at   

Rick WicklinRick Wicklin

is good at it .

Occasional Contributor
Posts: 14

Re: Matrix or Array Arrangement

Thank you very much for this.

I am very new to SAS. I don't know what is SAS/IML, I am using SAS 9.4.

Kindly let me know how can I switch to SAS/IML? or it's a different software?

Super User
Posts: 9,681

Re: Matrix or Array Arrangement

Yeah. That is a different language designed special for Matrix .

You didn't need to switch it , you run it as data step did, if you already have SAS/IML .

Occasional Contributor
Posts: 14

Re: Matrix or Array Arrangement

OK, thanks for this.

Let me find SAS/IML software first, and then will run it.

SAS Super FREQ
Posts: 3,477

Re: Matrix or Array Arrangement

You need to explain the data better.  81*81 = 6561, so where is the number 61312 coming from? Do you have duplicates in the (origin, destination) ordered pairs?

I initially thought that you wanted to put the locations into an 81x81 matrix.  That is easy in SAS/IML---although you did not post to that forum, so perhaps you are seeking a DATA step solution?  If so, maybe create the full 81x81 data set and merge with a sorted version of your data?

Here is a solution in PROC IML to packing this data into an array. Perhaps others can build on it or it can be used to better understand what you want and do not want:

data Have;
input origin destination location;
datalines;
1             1                   1
1             2                   2
1            81                 246
2             1                   1
81           81                 246
;

proc iml;
use Have;
read all var {origin destination location};
close Have;

A = j(81,81,.);      /* use missing values */
idx = sub2ndx(dimension(A), origin || destination);
A[idx] = location;  /* put specified locations in 81x81 matrix */ 

/* create full set of subscripts and write data */
grid = expandgrid(1:81, 1:81);
origin = grid[,1];
destination = grid[,2];
location = A;    /* rename */


create Want var {origin destination location};
append;
close Want;

Occasional Contributor
Posts: 14

Re: Matrix or Array Arrangement

Dear Rick,

Thanks for your reply.

Yes I've duplication in the data i.e. origin destination pairs, which are 61312 instead of (81*81)=6561.

But for this case rows should be 81*81*246=1614006 (because 246 are the number of locations where survey conducted)

It will be like 4 columns: Origin, Destination, Survey_Locations(246),  Interception_Pt(1 if intercepted or 0 otherwise)

I am going to install SAS/IML in a few days, and will read first the "Getting Started with the SAS/IML Matrix Programming Language", then I will come back to you, via SAS/IML forum.


Thanks.

Occasional Contributor
Posts: 14

Re: Matrix or Array Arrangement

The sequence will be like this:

Origin (1)  Destination(1)  Survey_Location(1)  Interception_Pt (0 or 1)

Origin (1)  Destination(1)  Survey_Location(2)  Interception_Pt (0 or 1)

.......

Origin (1)  Destination(1)  Survey_Location(246)  Interception_Pt (0 or 1)

Origin (1)  Destination(2)  Survey_Location(1)   Interception_Pt (0 or 1)

......

Origin (2)  Destination(1)  Survey_Location(1)  Interception_Pt (0 or 1)

.........

Origin (81)  Destination(81)  Survey_Location(246)  Interception_Pt (0 or 1)

For this case rows should be 81*81*246=1614006

Super User
Super User
Posts: 6,500

Re: Matrix or Array Arrangement

What are you trying to do with the data?  Are you sure that thinking about it as if it was a matrix is helpful in getting that done?

Almost any data set can be thought of as one or more arrays.  For example you have N variables of which D are used as key variables and M are results or measures such that N=D+M. Then this data set could be thought as a series of M arrays of D dimensions.  Of if the measures are all of the same type then a (D+1) dimensional array where the last dimension specifies the measure.

Ask a Question
Discussion stats
  • 8 replies
  • 298 views
  • 6 likes
  • 4 in conversation