Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sas 将行转换为列_Sas - Fatal编程技术网

Sas 将行转换为列

Sas 将行转换为列,sas,Sas,我需要在SAS中将行转换为列。我的问题和这个差不多: 主要的区别是我需要使用数组来实现。我不完全确定如何处理这个问题。我已经研究了转置,但这并不符合我问题的标准。如果您能就如何开始这个问题或如何解决这个问题提供任何建议,我将不胜感激 谢谢 编辑: 我希望它看起来像什么: 1998 1999 2000 1 20 30 40 2 20 21 25 3 32 33 我没有得到这个结果,而是在c3列中得到了一个成本列表。我做错了

我需要在SAS中将行转换为列。我的问题和这个差不多:

主要的区别是我需要使用数组来实现。我不完全确定如何处理这个问题。我已经研究了转置,但这并不符合我问题的标准。如果您能就如何开始这个问题或如何解决这个问题提供任何建议,我将不胜感激

谢谢

编辑:

我希望它看起来像什么:

       1998 1999 2000

1      20    30   40
2      20    21   25
3      32    33
我没有得到这个结果,而是在c3列中得到了一个成本列表。我做错了什么?
请注意,c1-c3表示年份。

看起来您的想法是正确的,但是您只能在
c3
列中获取值,因为语句
allcost(3)
只指向数组中的第三个位置,所以您需要使用
i
的值作为索引

让我们对代码做一个小的修改,看看会发生什么

data new;
set old;
by id;
retain _1998-_2000(drop=year cost i);
array costs(3) _1998-_2000;
if first.id then i = 1;
else i + 1;
costs(i) = cost;         * Adding the 'i' index will point to the correct cost variable.;
if last.id then output;  * This will output the array as a row.;
run;
这段代码似乎非常接近,但让我们检查输出

id    _1998    _1999    _2000

 1      20       30       40
 2      20       21       25
 3      32       33       25
除了
\u 2000
的第三行外,所有内容都显示在这里。这是因为在上一组中,
\u 2000
的值从未被替换。为了解决这个问题,我们可以在每个组的开头清除数组

data new(drop=year cost i j);
set old;
by id;
retain _1998-_2000;
array costs(3) _1998-_2000;
if first.id then do;
    do j = 1 to 3;
        costs(j) = .; * set each value in array to missing.;
    end;
    i = 1;
end;
else i + 1;
costs(i) = cost;
if last.id then output;
run;
现在,生成的数据集看起来是正确的

id    _1998    _1999    _2000

 1      20       30       40
 2      20       21       25
 3      32       33        .

对于旋转桌子,答案是肯定的。您正在谈论阵列,但尚未告诉我们原因。很可能,您会想看一看。谢谢,我不太确定我需要在哪里使用数组来实现该结果,因为我需要回答的问题没有具体说明,因此会产生混淆。请发布一些示例数据,以及您尝试生成的输出数据集的示例,两者都以文本形式在您的问题中键入,也许这会让我们更清楚proc转置是否合适?如果你必须使用数组,那么在这个网站上搜索“sas转置数组”以获得一些答案(有很多)。除了发布一些示例数据,您还应该显示您尝试的代码。请显示一些数据。非常感谢@J_Lard极好的答案-我希望有一种方法可以找到最喜欢的答案。不知道为什么没有。。。
id    _1998    _1999    _2000

 1      20       30       40
 2      20       21       25
 3      32       33        .