-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSASDiagnose_HoldOut_RMSE.sas
More file actions
143 lines (120 loc) · 5.91 KB
/
Copy pathSASDiagnose_HoldOut_RMSE.sas
File metadata and controls
143 lines (120 loc) · 5.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/* Copyright © 2026, SAS Institute Inc., Cary, NC, USA. All Rights Reserved. */
/*
Summary:
This script generates ARIMA forecasts for a subset of the M5 competition data using the TSModel procedure in SAS Visual Forecasting.
It uses an out-of-sample region and the RMSE criterion for model selection. It also uses a holdout sample. The same script
can be used to produce both daily and weekly forecasts by adjusting the input data and the ID statement in the TSModel procedure.
Instructions:
1. Replace <tablePrefix> with a unique prefix for the output tables. This prefix will be used to name the output tables generated by the script.
Example: Replace <tablePrefix> with "M5_Weekly" to generate tables like "M5_Weekly.OUTFOR".
2. Replace <myCaslib> with the name of the CAS library where your data and output tables will be stored.
Example: Replace <myCaslib> with "Public" if your CAS library is named "Public".
3. Load your input data into CAS:
- If your data is stored as a ".sashdat" file, use the following script to load it to memory:
proc cas;
table.dropTable / name="<your_table_name>" caslib="<myCaslib>" quiet=true;
table.loadTable /
caslib="<myCaslib>"
path="<your_data_file.sashdat>"
casout={caslib="<myCaslib>", name="<your_table_name>"};
quit;
- Replace "<your_data_file.sashdat>" with the path to the ".sashdat" file and "<myCaslib>" with the name of your CAS library.
- The "casout" parameter specifies the settings for an output table.
4. In the PROC TSMODEL statement, update the "data=" field: replace "<your_table_name>" with the name of the input table in CAS.
Example: If table name is "M5_Weekly_Data", update the line to 'data=mylib."M5_Weekly_Data"n'.
*/
/* Create a CAS session named vf_session. */
/* The session will be used to run CAS actions and procedures. */
cas vf_session;
option validmemname=extend validVarName=any;
proc cas;
session vf_session;
/* Drop tables from the previous runs if they exist. */
table.dropTable / name="<tablePrefix>.OUTFOR" caslib="<myCaslib>" quiet=true;
table.dropTable / name="<tablePrefix>.OUTSTAT" caslib="<myCaslib>" quiet=true;
table.dropTable / name="<tablePrefix>.OUTLOG" caslib="<myCaslib>" quiet=true;
table.dropTable / name="<tablePrefix>.OUTMODELINFO" caslib="<myCaslib>" quiet=true;
table.dropTable / name="<tablePrefix>.OUTSELECT" caslib="<myCaslib>" quiet=true;
table.dropTable / name="<tablePrefix>.RUNTIME" caslib="<myCaslib>" quiet=true;
quit;
/* Define a SAS library and link it to the project CAS library. */
libname mylib cas caslib="<myCaslib>";
proc tsmodel data=mylib."<your_table_name>"n
logcontrol=(error=keep warning=keep)
outobj=(
outFor=mylib."<tablePrefix>.OUTFOR"n
outStat=mylib."<tablePrefix>.OUTSTAT"n
outSelect=mylib."<tablePrefix>.OUTSELECT"n
outModelInfo=mylib."<tablePrefix>.OUTMODELINFO"n)
outscalar=mylib."<tablePrefix>.RUNTIME"n
outlog=mylib."<tablePrefix>.OUTLOG"n
errorstop=NO;
/* Define time series ID variable and the time interval. For Daily data, the interval=DAILY. */
id numeric_date interval=WEEK setmissing=MISSING trimid=LEFT nlformat=yes;
/* Define the variable to be used later in the code. */
var sales / acc=TOTAL setmiss=MISSING;
/* Define the BY group variable. */
by id;
/* Load ATSM (Automatic Time Series Modeling) and UTL (Utility) packages. */
require ATSM UTL;
outscalars diagtime;
submit;
declare object pkgObj(UTL);
/* Get the time at the start of the procedure to calculate the total runtime at the end. */
tStart=pkgObj.GetTime();
declare object dataFrame(tsdf);
declare object diagnose(diagnose);
declare object diagSpec(diagspec);
declare object inselect(selspec);
declare object forecast(foreng);
rc=dataFrame.Initialize();
/* Define the dependent variable to be forecasted. */
rc=dataFrame.AddY(sales);
rc=diagSpec.Open();
rc=diagSpec.SetARIMAX("identify", "BOTH");
rc=diagSpec.SetIDM("intermittent", 1000000);
rc=diagSpec.Close();
rc=diagnose.Initialize(dataFrame);
rc=diagnose.SetSpec(diagSpec);
rc=diagnose.SetOption("holdout", 26);
rc=diagnose.SetOption("holdoutpct", .);
rc=diagnose.SetOption("back", 26);
rc=diagnose.SetOption("criterion", "RMSE");
rc=diagnose.SetOption("minobs.trend", 2);
rc=diagnose.SetOption("minobs.season", 2);
rc=diagnose.Run();
ndiag=diagnose.nmodels();
rc=inselect.Open(ndiag);
rc=inselect.AddFrom(diagnose);
rc=inselect.Close();
rc=forecast.Initialize(dataFrame);
rc=forecast.SetOption("minobs.trend", 2);
rc=forecast.SetOption("minobs.season", 2);
rc=forecast.SetOption("minobs.mean", 2);
rc=forecast.SetOption("horizon", 20575);
rc=forecast.SetOption("lead", 26);
rc=forecast.SetOption("back", 26);
rc=forecast.SetOption("alpha", 0.05);
rc=forecast.SetOption("fcst.bd.lower", 0);
rc=forecast.SetOption("holdout", 26);
rc=forecast.SetOption("holdoutpct", .);
rc=forecast.SetOption("criterion", "RMSE");
rc=forecast.AddFrom(inselect);
rc=forecast.SetOption("seasonTest", "NONE");
rc=forecast.SetOption("intermittent", 1000000);
rc=forecast.Run();
declare object outFor(outFor);
declare object outSelect(outSelect);
declare object outModelInfo(outModelInfo);
declare object outStat(outStat);
rc=outFor.Collect(forecast);
rc=outSelect.Collect(forecast);
rc=outModelInfo.Collect(forecast);
rc=outStat.Collect(forecast);
/* Get the time at the end of the procedure and calculate the total runtime.
Store the runtime in the outscalar table. */
tEnd=pkgObj.GetTime();
diagtime=tEnd - tStart;
endsubmit;
run;
quit;