【发布时间】:2017-06-20 13:10:34
【问题描述】:
我有一个数据集,其中包含 ID、日期时间 + 一堆值字段。
这个想法是记录在一小时内彼此是一个会话。每 24 小时只能有一个会话。 (时间从第一条记录开始计算)
day() 方法不起作用,因为一条记录可能是下午 23:55,而下一条记录可能是第二天凌晨 12:01,这将是同一个会话。
我添加了 rowid 并运行了以下内容:
data testing;
set testing;
by subscriber_no;
prev_dt = lag(record_ts);
prev_row = lag(rowid);
time_from_last = intck("Second",record_ts,prev_dt);
if intck("Second",record_ts,prev_dt) > -60*60 and intck("Second",record_ts,prev_dt) < 0 then
same_session = 'yes';
else same_session = 'no';
if intck("Second",record_ts,prev_dt) > -60*60 and intck("Second",record_ts,prev_dt) < 0 then
rowid = prev_row;
else rowid = rowid;
format prev_dt datetime19.;
output;
run;
输入
ID record_TS rowid
52 17MAY2017:06:24:28 4
52 17MAY2017:07:16:12 5
91 05APR2017:07:04:55 6
91 05APR2017:07:23:37 7
91 05APR2017:08:04:52 8
91 05MAY2017:08:56:23 9
输入文件按ID排序并记录TS。
输出是
ID record_TS rowid prev_dt prev_row time_from_last same_session
52 17MAY2017:06:24:28 4 28APR2017:08:51:25 3 -1632783 no
52 17MAY2017:07:16:12 4 17MAY2017:06:24:28 4 -3104 yes
91 05APR2017:07:04:55 6 17MAY2017:07:16:12 5 3629477 no
91 05APR2017:07:23:37 6 05APR2017:07:04:55 6 -1122 yes
91 05APR2017:08:04:52 7 05APR2017:07:23:37 7 -2475 yes This needs to be 6
91 05MAY2017:08:56:23 9 05APR2017:08:04:52 8 -2595091 no
倒数第二行 - rowid 是 7,而我需要它是 6。
基本上我需要在脚本移动之前更改为当前保存的 rowid 以评估下一个。
谢谢 本
我已经实现了我所需要的
proc sql;
create table testing2 as
select distinct t1.*, min(t2.record_TS) format datetime19. as from_time, max(t2.record_TS) format datetime19. as to_time
from testing t1
join testing t2 on t1.id_val= t2.id_val
and intck("Second",t1.record_ts,t2.record_ts) between -3600 and 3600
group by t1.id_val, t1.record_ts
order by t1.id_val, t1.record_ts
;
quit;
但我仍然想知道是否有办法在评估下一行之前提交对当前行的更改。
【问题讨论】:
-
为什么不创建一个“会话”表,每个会话有一行并定义一个
ExpiresAt日期,即未来 24 小时。然后,每次用户提出请求时,将ExpiresAt更新为从现在起 24 小时后。 -
这就是我想要做的。我无法更改源表的结构或可用数据。
-
您可以将您的 HAVE 数据(即输入数据集)添加到问题中吗? subscriber_no 变量是在输出中作为 ID 打印的吗?我注意到您有一个
BY subscriber_no;语句,但没有逻辑可以防止两个不同的订阅者被分配到同一个会话。通常,即使只是在测试时,让输入和输出数据集相同也是一个坏主意,因为这样很难重复测试和检查结果。
标签: sas enterprise-guide