使用动态变量将宽转换为长 [英] Transpose wide to long with dynamic variables
问题描述
我有一个广泛的数据集,每个月都列为一列.我想将数据转换为长格式,但问题是我的列名将来会发生变化.将动态变量传递给转置语句的最佳转置方式是什么?
I've got a wide dataset with each month listed as a column. I'd like to transpose the data to a long format, but the problem is that my column names will be changing in the future. What's the best way to transpose with a dynamic variable being passed to the transpose statement?
例如:
data have;
input subject $ "Jan-10"n $ "Feb-10"n $ "Mar-10"n $;
datalines;
1 12 18 22
2 13 19 23
;
run;
data want;
input subject month $ value;
datalines;
1 Jan-10 12
1 Feb-10 18
1 Mar-10 22
2 Jan-10 13
2 Feb-10 19
2 Mar-10 23
;
run;
推荐答案
只需运行转置过程,只提供 by
语句.
Simply run the transpose procedure and provide only the by
statement.
我已更新您的示例数据以将月份转换为数值(而不是无法转置的字符).我还通过删除连字符将它们更改为使用有效的基本 sas 名称.
I've updated your sample data to convert the months to numeric values (rather than character which can't be transposed). I've also changed them to use valid base-sas names by removing the hyphen.
data have;
input subject $ "Jan10"n "Feb10"n "Mar10"n ;
datalines;
1 12 18 22
2 13 19 23
;
run;
这是您需要的转置语法,默认情况下它将转置所有数值变量:
Here's the transpose syntax you need, it will transpose all numeric variables by default:
proc transpose data=have out=want;
by subject;
run;
您还可以做一些更明确但仍然动态的事情,例如:
You could also do something more explicit, but still dynamic such as:
proc transpose data=have out=want;
by subject;
var jan: feb: mar: ; * ETC;
run;
这将转置所有以 jan/feb/mar 等开头的变量...在您的表包含您不想包含在转置中的其他数字变量的情况下很有用.
This would transpose all vars that begin with jan/feb/mar etc... Useful in case your table contains other numeric variables that you don't want to include in the transpose.
这篇关于使用动态变量将宽转换为长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!