BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
SC_1991
Fluorite | Level 6

I have a table that that has the 'i', 'j', 'tot' columns and I want to convert this table into a 2x2 matrix with i as the row index and j as the column index and assign the tot as value. Is there a PROC that i can use? i, j range from 1 to 26.

I need to do this so that I can use the resultant matrix and perform matrix operations with PROC IML.

 

Example:

 

I      J       tot

1     1       250

1     2      545

1     3       654

2     1         54

2     2       458

2     3         878

3     1         87

3     2         447

3     3        562

 

my output should be like :

 

      1            2          3

1   250      545       654

2   54        458      878

3   87        447      562 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Rick_SAS
SAS Super FREQ

You can use the FULL function in SAS/IML to convert the data into a rectangular form:

 

data Have;
input I      J       tot;
datalines;
1     1      250
1     2      545
1     3      654
2     1       54
2     2      458
2     3      878
3     1       87
3     2      447
3     3      562
;

 proc iml;
use Have;
read all var {tot i j} into S;
close;

X = full(S);
print X;

View solution in original post

7 REPLIES 7
PaigeMiller
Diamond | Level 26

PROC REPORT will do this.

 

Define variable I as a GROUP variable and J as an across variable.

 

There are functions in PROC IML (such as the SHAPE function) that should be able to do this as well, meaning you can skip the intermediate step and go straight to PROC IML.

--
Paige Miller
Rick_SAS
SAS Super FREQ

You can use the FULL function in SAS/IML to convert the data into a rectangular form:

 

data Have;
input I      J       tot;
datalines;
1     1      250
1     2      545
1     3      654
2     1       54
2     2      458
2     3      878
3     1       87
3     2      447
3     3      562
;

 proc iml;
use Have;
read all var {tot i j} into S;
close;

X = full(S);
print X;
SC_1991
Fluorite | Level 6
that works! thanks.
Ksharp
Super User
data have;
input I      J       tot;
cards;
1     1       250
1     2      545
1     3      654
2     1         54
2     2       458
2     3         878
3     1         87
3     2         447
3     3        562
;

proc iml;
use have;
read all var {i j tot};
close;
x=tot||i||j;
want=full(x);
print want;
quit;

SC_1991
Fluorite | Level 6
is there a way to change the matrix index into
1 2 3
1
2
3

instead of

col1 col2 col 3
row1
row2
row 3

thanks for your help
Ksharp
Super User

Sure.

data have;
input I      J       tot;
cards;
1     1       250
1     2      545
1     3      654
2     1         54
2     2       458
2     3         878
3     1         87
3     2         447
3     3        562
;

proc iml;
use have;
read all var {i j tot};
close;
x=tot||i||j;
want=full(x);
idx=char(1:nrow(want));
print want[r=idx c=idx l=''];
quit;

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
  • 7 replies
  • 4173 views
  • 4 likes
  • 4 in conversation