SAS - 将当天发生的日常内容聚合在一起

时间:2012-03-04 12:50:15

标签: sas

我有每日数据,而不是完全连续(即,并非所有天都在一周内出现),我需要将其转换为每周总数。问题在于,数据与交易有关,因此在同一天有多个观察。使用以下PROC EXPAND过程会导致错误“ID变量的值,FixtureDate = 04JAN2011,在数据集RAW.VLCC2011中的观察编号2处与先前的观察相同”:

    PROC EXPAND DATA = raw.VLCC2011 OUT = raw.VLCC2011_wkly FROM= Day TO = Week;
    convert FixtureCargoSize/ OBSERVED=TOTAL method=aggregate;
    ID FixtureDate;
    run;

3 个答案:

答案 0 :(得分:1)

以下是proc sql的解决方案。您也可以使用数据步骤执行类似操作。

proc sql;
 create table VLCC2011_wkly as
  select intnx('week', date, 0, 'end') as week, sum(FixtureCargoSize) as FixtureCargoSizeTotal
  from VLCC2011
  group by calculated week;
quit;

intnx函数获取日期并将其移至其他日期。在这种情况下,它需要任何日期并将其移动到一周的最后一天。通过这种方式对所有具有相同周末日期的日期进行求和,可以满足您的需求。

答案 1 :(得分:0)

我没有使用过PROC EXPAND。但是,错误消息告诉您它不喜欢每个ID值有多个观察值。也许您需要预处理输入数据集RAW.VLCC2011,这样每个FIXTUREDATE最多只能有一个观察值。

答案 2 :(得分:0)

以下是我解决它的方法(很长的路):

data raw.VLCC2011_wkly;
set raw.VLCC2011;
IF FixtureDay < 8 then FixtureWeek = 1;
IF FixtureDay > 7 and FixtureDay < 15 then FixtureWeek = 2;
IF FixtureDay > 14 and FixtureDay < 23 then FixtureWeek = 3;
IF FixtureDay > 22 and FixtureDay < 30 then FixtureWeek = 4;
IF FixtureDay > 29 and FixtureDay < 32 then FixtureWeek = 5;
run;

proc sql;
create table raw.VLCC2011_wkly1 as
select FixtureMonth, FixtureDay, FixtureWeek, FixtureCargoSize,                                   sum(FixtureCargoSize) as CargoSizeTotal
from raw.VLCC2011_wkly 
group by FixtureMonth, FixtureWeek
Order by FixtureMonth, FixtureWeek, FixtureDay;
quit;