使用 SAS 中的数组将宽转换为长 [英] Transposing wide to long with an array in SAS
问题描述
我有一个 SAS 数据集,其中包含以下变量:ID、Var1_0、Var1_3、Var1_6、Var2_0、Var2_3、Var2_6,可以这样读取:Var1_0 是时间 0 的参数 1.对于每个主题,我有 2 个变量和 3 个时间点.我想使用数组将其转换为长格式,我这样做了:
I have a SAS dataset, with the following variables: ID, Var1_0, Var1_3, Var1_6, Var2_0, Var2_3, Var2_6, which can be read like this: Var1_0 is parameter 1 at time 0. For every subjects I have 2 variables and 3 time points. I want to transpose this into a long format using an array, I did this:
data long;
set wide;
array a Var1_0 Var1_3 Var1_6 Var2_0 Var_3 Var_6;
Do i=1 to dim(a);
outcome = a[i];
*Var = ???;
if (mod(i,3)=1) then Time = 0;
else if (mod(i,3)=2) then Time = 3;
else Time = 6;
Output;
end;
keep ID Outcome Time;
run;
问题是我不知道如何计算参数变量,即我想添加一个为1或2的变量,具体取决于该值与哪个参数相关.有没有更好的方法来做到这一点?谢谢!
The problem is that I don't know how to calculate the parameter variable, i.e., I want to add a variables that is either 1 or 2, depending on which parameter the value is related to. Is there a better way of doing this? Thank you !
推荐答案
Reeza 在她的评论中给了你答案.这是打出来的.
Reeza gave you the answer in her comment. Here it is typed out.
data long;
set wide;
array a[*] Var1_0 Var1_3 Var1_6 Var2_0 Var2_3 Var2_6;
do i=1 to dim(a);
outcome = a[i];
var = vname(a[i]);
time = input(scan(var,2,'_'),best.);
/*Other stuff you want to do*/
output;
end;
run;
VNAME(array[sub])
为您提供由 array[sub] 引用的变量的变量名称.
VNAME(array[sub])
gives you the variable name of the variable referenced by array[sub].
scan(str,i,delim)
使用指定的分隔符为您提供 str
中的第 i 个单词.
scan(str,i,delim)
gives you the ith word in str
using the specified delimiter.
这篇关于使用 SAS 中的数组将宽转换为长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!