从旧的 SAS 获取新数据

get new data from old one SAS

我是 SAS 新手 我有这个例子:

proc iml;  
x={1 2 3 4 5 6 7 8 9};
y={2,3,5,4,8,6,4,2,2};
z={1,1,1,1,2,2,2,2,2};
data=t(x)||y||z;
print data;    
run;
quit;

data 
1 2 1 
2 3 1 
3 5 1 
4 4 1 
5 8 2 
6 6 2 
7 4 2 
8 2 2 
9 2 2

如何创建只有 Z=1 和 Z=2 的新数据?

谢谢。

您可以使用 loc 函数对数据矩阵进行子集化。以下是函数的描述,摘自 Introduction to SAS/IML.

中的索引矩阵

the LOC function is often very useful for subsetting vectors and matrices. This function is used for locating elements which meet a given condition. The positions of the elements are returned in row-major order. For vectors, this is simply the position of the element. For matrices, some manipulation is often required in order to use the result of the LOC function as an index. The syntax of the function is:

matrix2=LOC(matrix1=value);

应用于您的示例:

proc iml;  
  x={1 2 3 4 5 6 7 8 9};
  y={2,3,5,4,8,6,4,2,2};
  z={1,1,1,1,2,2,2,2,2};
  data=t(x)||y||z;
  print data;
  z1rows=loc(data[,3]= 1);
  z1=data[z1rows,];
  print z1;
  z2rows=loc(data[,3]= 2);
  z2=data[z2rows,];
  print z2;
  run;
quit;

print z1;

的结果
+------------+
|     z1     |
+---+----+---+
| 1 |  2 | 1 |
| 2 |  3 | 1 |
| 3 |  5 | 1 |
| 4 |  4 | 1 |
+---+----+---+

print z2;

的结果
+------------+
|     z2     |
+---+----+---+
| 5 |  8 | 2 |
| 6 |  6 | 2 |
| 7 |  4 | 2 |
| 8 |  2 | 2 |
| 9 |  2 | 2 |
+---+----+---+