SAS Programming

DATA Step, Macro, Functions and more
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-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 5440 views
  • 4 likes
  • 4 in conversation