Seasonality Index and Trend using SAS


<<< Click here to go back


Following code can be used on the data with column names : year, Month, sales to calculate the seasonality index and trend variables.

Download the file to use in this  code  : Sales data in .csv format

/*  import data ... Change the file's location in the code */


Proc Import datafile = "location\sales_data_for_sas.csv"
out = sales dbms =csv replace;
Run;

/*  first the sales is replicated in another variable and a Row id counter 1,2,3,.... is generated ... this counter is very useful */


Data sales;
set sales;
sales_with_outlier = sales;
Counter+1;
Run;

/*  Uni variate outlier detection and treatment */


Proc standard data = sales out  = sales_new mean = 0 std = 1;
var sales;
Run;

Proc expand data = sales_new out = sales_new;
convert sales_with_outlier = lag_sales /transformout = (lag 1);
convert sales_with_outlier = lead_sales /transformout = (lead 1);
Run;

Data sales_new;
retain counter year month sales_with_outlier Sales_treated;
set  sales_new;
If abs(sales) > 2.5 then Sales_treated = sum(lag_sales,lead_sales)/2;
else Sales_treated = sales_with_outlier;
Drop time sales lag_sales lead_sales;
Run;

/*Calculation of Seasonality Index*/


Proc SQL;
create table SI as select month, mean(Sales_treated) as mean_sales
from sales_new
group by month;
quit;
Proc SQL;
create table SI as select *, mean(mean_sales) as divisor, mean_sales / calculated divisor as seas_index
from SI;
quit;
Proc SQL;
create table sales_new as select a.counter, a.year, a.month, 
a.sales_with_outlier,a.Sales_treated, b.seas_index
from sales_new as a join SI as b
on a.month = b.month
order by counter;
quit;

/*Calculation of Trend variable*/

Proc Reg data = sales_new;
model Sales_treated = counter;
output out = sales_new p= Trend;
run;
Quit;



Enjoy reading our other articles and stay tuned with ...



Kindly do provide your feedback in the 'Comments' Section and share as much as possible.

Family of the article:


1. Outlier Detection &Treatment - A fresh perspective
2. Seasonality Index and Trend Variables

3. Outlier Detection &Treatment - Part 2 - Multivariate

No comments:

Post a Comment

Do provide us your feedback, it would help us serve your better.