SAS: turn rows into a matrix

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



SAS: turn rows into a matrix



Hi i'm working on a project for summer school and the person teaching likes to work by row. I think it would be beneficial to turn it into a matrix and work with arrays.



The current dataset looks like this:


data longdata;
input var1 var2 Value;
datalines;

rowone colone 1
rowone coltwo 1
rowone colthree 2
rowone colfour 3
rowtwo colone 1
rowtwo coltwo 1
rowtwo colthree 1
rowtwo colfour 2
rowthree colone 3
rowthree coltwo 3
rowthree colthree 3
rowthree colfour 4
rowfour colone 1
rowfour coltwo 3
rowfour colthree 3
rowfour colfour 3
;
run;



I'd like to turn the above into this, preferably through a by statement:


data matrixdata;
input var1-var4;
datalines;

colone coltwo colthree colfour
rowone 1 1 2 2
rowtwo 1 1 1 2
rowthree 3 3 3 4
rowfour 1 3 3 3
;
run;





What have you tried ? The original data is a very good structure for SAS reporting or analytics that involve class or by statements. The matrix layout of the data may be 'easier' for some programming tasks but may also be cumbersome problematic
– Richard
Aug 6 at 16:30





The end result is a transition matrix for a credit risk project and so, this is the end of the project - I would prefer to see my work than it still be in abstract form. I've been trying to give the columns an id, then turn it into a list via proc sql , then create the array using the list and outputting the value into the right columns, I've not finished that code just yet, will post when done & after trying your method
– 78282219
Aug 6 at 16:52





you can also look into stats.idre.ucla.edu/sas/modules/…
– Kiran
Aug 6 at 17:00




1 Answer
1



Proc transpose is excellent for transforming categorical data into other layout forms.


Proc transpose


proc transpose data=have out=want;
by var1 notsorted;
id var2;
var value;
run;



Despite looking like an array the data is not implicitly an array. To be a 4x4 array during a single implicit data step iteration the incoming data set would really want 16 variables per row, say v1-v16, with values 1 1 2 3 1 1 1 2 3 3 3 4 1 3 3 3. Each row would correspond one instance of the array.


v1-v16


1 1 2 3 1 1 1 2 3 3 3 4 1 3 3 3


data want2;
retain v1-v16;
array v (16) v1-v16;
set have end=end;
v(_n_) = value;
if end;
keep v:;
run;

data _null_;
set want2;
array m(4,4) v1-v16;
do row = 1 to dim(m,1);
do col = 1 to dim(m,2);
put row= col= m(row,col)=;
end;
end;
run;





wow, I actually can't believe how simple that proc transpose was... I was spending a long time trying to manually create the columns, then outputting the value in the right place
– 78282219
Aug 6 at 16:55






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard