我有以下数据:
wei 01feb2018车wei 02feb2018车wei 02mar2019自行车卡林01feb2018车carlin 05feb2018自行车carlin 07mar2018自行车卡林01mar2019车我想确定新的……
您似乎需要两个不同的“状态”变量,一个用于连续性 的 过度 强> 前一年和一年的连续性 的 内 强> 月。
在SQL中,存在性反身相关的子查询结果可以是满足该行的行的案例测试 的 过度 强> 和 的 内 强> 标准。日期算术用于计算相隔天数和 INTCK 用于计算相隔数月:
INTCK
data have; input customer $ date& date9. item& $; format date date9.; datalines; wei 01feb2018 car wei 02feb2018 car wei 02mar2019 bike carlin 01feb2018 car carlin 05feb2018 bike carlin 07mar2018 bike carlin 01mar2019 car run; proc sql; create table want as select *, case when exists ( select * from have as inner where inner.customer=outer.customer and (outer.date - inner.date) between 1 and 365 ) then 'cont.' else 'new' end as status_year, case when exists ( select * from have as inner where inner.customer=outer.customer and outer.date > inner.date and intck ('month', outer.date, inner.date) = 0 ) then 'cont.' else 'new' end as status_month from have as outer ; quit;
您可以使用 retain :
retain
proc sort data=test out=test2; by name type date; run; data test2 ; set test2; retain retain 'new'; by name type date; if first.type then retain='new'; else retain='con'; run; proc sort data=test2 out=test2; by name date; run;
输出:
+--------+-----------+------+--------+ | name | date | type | retain | +--------+-----------+------+--------+ | carlin | 01FEB2018 | car | new | | carlin | 05FEB2018 | bike | new | | carlin | 01MAR2019 | car | con | | wei | 01FEB2018 | car | new | | wei | 02FEB2018 | car | con | | wei | 02MAR2019 | bike | new | +--------+-----------+------+--------+