为什么我的 SAS 数组语句没有引用预期的变量?
Why doesn't my SAS array statement reference the expected variables?
我试图在 SAS 中查找出发(出站)和下一次访问(入站)之间的小时数。
我的数据集(SAS 数据集)看起来像这样。变量 In 和 Out 是日期时间变量。
ID In1 In2 Out1 Out2
-----------------------------------------------------------------
1 14AUG12:10:27 16AUG12:08:38 14AUG12:16:25 16AUG12:09:38
2 08AUG12:14:41 09AUG12:07:37 08AUG12:22:14 09AUG12:08:58
3 08AUG12:08:54 09AUG12:23:45 08AUG12:14:04 10AUG12:02:55
4 27AUG12:06:41 27AUG12:10:42 27AUG12:09:45 27AUG12:12:17
5 07AUG12:23:16 09AUG12:22:13 07AUG12:23:16 10AUG12:15:59
我想计算 In2 和 Out1 之间的差异,即我想要以小时为单位的 In2 - Out1(他们出去的时间和他们再次回来的时间之间的差异)。我决定循环执行此操作,因为我有很多记录。我对所有记录都有不同的 In's 和 Out's(我事先不知道)。在这个特定的数据集中,我最多有 In33 和 Out33。
我的密码是
data counts;
set mydata;
array in _numeric_;
array out _numeric_;
do k = 1 to 32;
time = intck('hour', out[k], in[k+1]);
if time >= 0 and time<= 72 then output;
end;
run;
我的问题是输出的小时数的准确性。对于上面显示的这个特定数据,对于 ID 1、2、3、4 和 5,我得到的时间分别为 46、17、39、4 和 52 小时。但时间应该是 40、9、33、1 和 47 小时分别为 ID 1、2、3、4 和 5。我不确定我在这里做错了什么。我会感谢你的帮助。
你的两个数组是相同的,因为你声明它们等于你的 mydata 集中的所有数字变量。你必须按照 data_null_ 的评论来命名它们,或者更明确地说:
array in in1-in33;
array out out1-out33;
我更改了阵列,现在获得了所需的精度。我就是这样做的
data counts;
set mydata;
array Ins {*} In: ;
array Outs {*} Out: ;
do k = 1 to dim(ins) - 1;
time = intck('hour', outs[k], ins[k+1]);
if time >= 0 and time <= 72 then output;
end;
run;
感谢您的建议,很有帮助。
我试图在 SAS 中查找出发(出站)和下一次访问(入站)之间的小时数。
我的数据集(SAS 数据集)看起来像这样。变量 In 和 Out 是日期时间变量。
ID In1 In2 Out1 Out2
-----------------------------------------------------------------
1 14AUG12:10:27 16AUG12:08:38 14AUG12:16:25 16AUG12:09:38
2 08AUG12:14:41 09AUG12:07:37 08AUG12:22:14 09AUG12:08:58
3 08AUG12:08:54 09AUG12:23:45 08AUG12:14:04 10AUG12:02:55
4 27AUG12:06:41 27AUG12:10:42 27AUG12:09:45 27AUG12:12:17
5 07AUG12:23:16 09AUG12:22:13 07AUG12:23:16 10AUG12:15:59
我想计算 In2 和 Out1 之间的差异,即我想要以小时为单位的 In2 - Out1(他们出去的时间和他们再次回来的时间之间的差异)。我决定循环执行此操作,因为我有很多记录。我对所有记录都有不同的 In's 和 Out's(我事先不知道)。在这个特定的数据集中,我最多有 In33 和 Out33。
我的密码是
data counts;
set mydata;
array in _numeric_;
array out _numeric_;
do k = 1 to 32;
time = intck('hour', out[k], in[k+1]);
if time >= 0 and time<= 72 then output;
end;
run;
我的问题是输出的小时数的准确性。对于上面显示的这个特定数据,对于 ID 1、2、3、4 和 5,我得到的时间分别为 46、17、39、4 和 52 小时。但时间应该是 40、9、33、1 和 47 小时分别为 ID 1、2、3、4 和 5。我不确定我在这里做错了什么。我会感谢你的帮助。
你的两个数组是相同的,因为你声明它们等于你的 mydata 集中的所有数字变量。你必须按照 data_null_ 的评论来命名它们,或者更明确地说:
array in in1-in33;
array out out1-out33;
我更改了阵列,现在获得了所需的精度。我就是这样做的
data counts;
set mydata;
array Ins {*} In: ;
array Outs {*} Out: ;
do k = 1 to dim(ins) - 1;
time = intck('hour', outs[k], ins[k+1]);
if time >= 0 and time <= 72 then output;
end;
run;
感谢您的建议,很有帮助。