我有一个包含SAS日常数据的数据集。我想通过id与上个月的价值差异将其转换为月度形式。例如:
thedate, id, val
2012-01-01, 1, 10
2012-01-01, 2, 14
2012-01-02, 1, 11
2012-01-02, 2, 12
...
2012-02-01, 1, 20
2012-02-01, 2, 15
我想输出:
thedate, id, val
2012-02-01, 1, 10
2012-02-01, 2, 1
答案 0 :(得分:2)
这是一种方法。如果您许可SAS-ETS,可能有更好的方法来使用PROC EXPAND。
*Setting up the dataset initially;
data have;
informat thedate YYMMDD10.;
input thedate id val;
datalines;
2012-01-01 1 10
2012-01-01 2 14
2012-01-02 1 11
2012-01-02 2 12
2012-02-01 1 20
2012-02-01 2 15
;;;;
run;
*Sorting by ID and DATE so it is in the right order;
proc sort data=have;
by id thedate;
run;
data want;
set have;
retain lastval; *This is retained from record to record, so the value carries down;
by id thedate;
if (first.id) or (last.id) or (day(thedate)=1); *The only records of interest - the first record, the last record, and any record that is the first of a month.;
* To do END: if (first.id) or (last.id) or (thedate=intnx('MONTH',thedate,0,'E'));
if first.id then call missing(lastval); *Each time ID changes, reset lastval to missing;
if missing(lastval) then output; *This will be true for the first record of each ID only - put that record out without changes;
else do;
val = val-lastval; *set val to the new value (current value minus retained value);
output; *put the record out;
end;
lastval=sum(val,lastval); *this value is for the next record;
run;
答案 1 :(得分:0)
你可以使用PROC SQL实现这一点,并使用intnx函数将上个月的日期提前一个月......
proc sql ; create table lag as select b.thedate, b.id, (b.val - a.val) as val from mydata b left join mydata a on b.date = intnx('month',a.date,1,'s') and b.id = a.id order by b.date, b.id ; quit ;
这可能需要调整以处理上个月不存在的情况或与上个月具有不同天数的月份。