SAS:将行变成矩阵

SAS: turn rows into a matrix

你好,我正在做一个暑期学校的项目,老师喜欢按行工作。我认为将其转换为矩阵并使用数组将是有益的。

当前数据集如下所示:

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;

我想把上面的变成这样,最好通过一个 by 语句:

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;

Proc transpose 非常适合将分类数据转换为其他布局形式。

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

尽管数据看起来像一个数组,但实际上并不是一个数组。要在单个隐式数据步迭代期间成为 4x4 数组,传入数据集实际上需要每行 16 个变量,比如 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;