【问题标题】:convert univariate table to multivariate table in sas [duplicate]在sas中将单变量表转换为多变量表[重复]
【发布时间】:2015-01-16 16:29:53
【问题描述】:

我正在努力将单变量表转换为 sas 中的多变量表。我会说“单变量表”,我的意思是也许仍然是一个多变量表......但这里有一个例子:

a b c 1001 1 4 8 1001 2 3 7 1002 11 9 6 1002 5 14 15

我希望它是这样的:

a1 b1 c1 a2 b2 c3 1001 1 4 8 2 3 7 1002 11 9 6 5 14 15

因为我有数千个 ID(例如 1001-3000)。有没有一种简单的方法可以翻转桌子??

非常感谢!

【问题讨论】:

  • 每个ID的记录数是否相同?如果没有,您是否提前知道最大数量?
  • Proc transpose 可以做到这一点。这是一个教程:ats.ucla.edu/stat/sas/modules/ltow_transpose.htm
  • 非常感谢贾莫尔! @.@ 我在想可能是一步法..

标签: sql sas


【解决方案1】:

这不是一个简单的方法,因为您正在执行多个值。 您可以进行 3 次转置 - 每个值一次并通过来自@jaamor 的链接进行合并。或者你可以做一个半手动的数据步骤转置。这假设最大计数为 2。如果每个 ID 有超过 2 个,则可以计算最大值并将其放入宏变量中。然后将代码中的 2 替换为宏变量。

data want;
set have;
by id;

array _a(2) a1-a2;
array _b(2) b1-b2;
array _c(2) c1-c2;

retain a: b: c:;


if first.id then count=1;
else count+1;

_a(count)=a;
_b(count)=b;
_c(count)=c;

if last.id then output;

 drop a b c count;

run;

【讨论】:

    猜你喜欢
    • 2015-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-27
    • 1970-01-01
    • 1970-01-01
    • 2019-10-11
    • 2017-12-21
    相关资源
    最近更新 更多