我的数据看起来像这样:
> Data sales;
> Infile datalines missover;
> Input year $ month sales;
> Datalines;
> 2016 1 489
> 2016 2 475
> 2016 3 523
> 2016 4 546
> 2016 5 675
> 2016 6 667
> 2016 7 334
> 2016 8 359
> 2016 9 521
> 2016 10 526
> 2016 11 459
> 2016 12 439
> 2017 1 500
> 2017 2 445
> 2017 3 531
> 2017 4 541
> 2017 5 620
> 2017 6 630
> 2017 7 300
> 2017 8 350
> 2017 9 502
> 2017 10 524
> 2017 11 402
> 2017 12 469
> ;
我希望采用去年的平均增幅,并用这个来预测未来12个月的月份。我不是在寻找一种先进的方法,这对我来说纯粹是编程。
到目前为止,我所拥有的是(太难看了):
data sales2;
set sales;
lag_sales=lag(sales);
diff=sales-lag_sales;
start_month=month(intnx("month",today(),-12));
start_year=year(intnx("year",today(),-1));
if year>=start_year and month>=start_month;
run;
proc sql;
create table sales3 as
select avg(diff) as avg_diff
from sales2 ; quit;
proc sql;
create table sales4 as
select year, month, monotonic() as row_no, sales, avg_diff,
sales+avg_diff*monotonic() as sales_pred
from sales2, sales3
;
quit;
答案 0 :(得分:2)
以下代码回答了您的问题/您要做的事情。 请注意,我使用了:
如果您有大量记录,这将提高您的查询效果
请注意,您还可以根据移动平均线预测销售额,而不仅仅是差异(不包含在代码中)。
基于您的销售数据集的代码:
/*Calculate the difference*/
data diff;
set sales;
row = _n_;
retain prev;
if row=1 then do; prev=0; diff=0; output; prev=sales;end;
else do; diff=sales -prev; output; prev=sales; end;
run;
/*select avg for 2016, for live data feed change to -1*/
proc sql noprint;
select avg(diff) into:avg from diff where input(year,4.)=year(today())-2;
quit;
/*select avg for 2017, for live data feed remove the -1*/
proc sql;
create table sales_new as
select year,month, sales, &avg as avg_diff, Sales+&avg as sales_pred from diff where input(year,4.)=year(today())-1;
quit;
<强>输出:强>