我使用以下SAS代码生成逐年计数:
data have;
infile cards truncover expandtabs;
input MC $ ET $ Date :date9. Time :time. PMC $ PXMC $ Site $ Dia MV SF;
format date date8. Time HHMM.;
cards;
US000409 Meas 12Oct2015 17:26 7101 Et1 1 . 2780462.00000 1
US000409 Meas 12Nov2016 17:33 7101 Et1 1 861.26 2780462.00000 1
US000409 Lath 12Oct2015 17:33 7102 Et1 1 861.6 2780462.00000 1
US01036 Meas 12Nov2016 17:26 7101 Et1 2 . 522860.00000 1
US01036 Lath 13Oct2016 17:33 7202 Et1 2 866.68 522860.00000 1
US01036 Meas 13Oct2015 17:33 7101 Et1 2 867.36 522860.00000 1
US02044 Meas 13Nov2016 17:26 7202 Et1 1 . 569298.00000 1
US02044 Lath 13Nov2015 17:33 7202 Et1 1 865.32 569298.00000 1
US02044 Meas 14Nov2016 17:33 7202 Et1 2 865.68 569298.00000 1
US318 Lath 14Nov2016 17:26 7101 Et2 2 . 2630856.00000 1
US318 Meas 14Nov2016 17:33 7202 Et2 3 863.26 2630856.00000 1
US318 Lath 14Nov2016 17:33 7202 Et2 3 863.94 2630856.00000 1
US000409 Meas 15Nov2016 21:56 7202 Et2 3 860.98 2780462.00000 1
US000409 Meas 15Nov2016 21:56 7203 Et2 4 861.5 2780462.00000 1
US01036 Lath 16Nov2016 21:56 7101 Et2 4 866.64 522860.00000 1
US01036 Meas 16Nov2016 21:56 7202 Et2 4 867.34 522860.00000 1
US02044 Lath 17Nov2016 21:56 7203 Et2 1 865.3 569298.00000 1
US02044 Meas 17Nov2016 21:56 7204 Et2 3 865.68 569298.00000 1
US318 Lath 17Nov2016 21:56 7204 Et2 2 863.24 2630856.00000 1
;
run;
PROC SQL;
CREATE TABLE HAVE01 AS
SELECT t1.*, t1.Date FORMAT=YEAR4. LABEL="Year" AS Year
FROM HAVE t1;
QUIT;
PROC SQL;
CREATE TABLE WANT AS
SELECT Year, (COUNT(ET)) AS COUNT_of_ET
FROM HAVE01
GROUP BY Year;
QUIT;
不幸的是,我得到的结果如下:
Year Count_of_ET
2015 2
2015 1
2015 1
2016 1
2016 2
2016 1
2016 4
2016 2
2016 2
2016 3
我期待这样的事情:
Year Count_of_ET
2015 4
2016 15
我不知道我在做错的地方。
你能帮忙吗?
此外,暂时忘记' MV '列看起来像上面的示例数据,我需要计算的是,'日期与MV的差异(类似于DIF函数或LAG函数用于此类计算)。
答案 0 :(得分:2)
问题是您的新Year
字段仍然是日期而不是年份数字 - 您只将其格式化为一年。
如果您使用YEAR()
功能,它将起作用:
PROC SQL;
CREATE TABLE HAVE01 AS
SELECT t1.*,
YEAR(t1.Date) LABEL="Year" AS Year /* < Changes made here */
FROM HAVE t1;
QUIT;
正如您所期望的那样输出。