Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- Home
- /
- Analytics
- /
- Forecasting
- /
- How to generate a forecast path using data step?

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

🔒 This topic is **solved** and **locked**.
Need further help from the community? Please
sign in and ask a **new** question.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Posted 03-04-2013 02:27 PM
(933 views)

Hi, folks:

I have a simple time series model but not estimated by arima, but I want to generate a forecast path based on historical data and estimated parameters.

For example, the model is given by x=beta1*lag(x)+beta2*lag2(x); obviously the forecast for a given forecast period, say, t+k, should be xhat(t+k)=beta1*xhat(t+k-1)+beta2*xhat(t+k-2), where xhat is the forecast value.

I wonder if I can use the data step to archieve this goal. The key part is to update the value of xhat iteratively.

I have a data set with the structure as the following:

xxxx;

T X Beta1 Beta2

_________________________

1 x (1) beta1 beta2

2 x (2) beta1 beta2

3 . beta1 beta2

4 . beta1 beta2

5 . beta1 beta2

Basically, if there is historical data, the value of x is the historical value, if not, it is missing and the value of Beta1 and Beta2 are hold at the estimated value for each observation. The task is to generate a series of Xhat. Xhat(T)=beta1*Xhat(t-1)+beta2*Xhat(t-2);, for T=3, Xhat(2)=x(2) and Xhat(1)=x(1). For T>3, we just use lag values of Xhat.

Here is my code;

data yyy;

set xxxx;

xhat=X;

l1_xhat0=lag(xhat);

l2_xhat0=lag(l1_xhat);

xhat0=beta1*l1_xhat0+beta2*l2_xhat0;

xhat=xhat0;

run;

However, in the dataset, yyy, the value of xhat is still missing.

Can anyone give me some hints or advice on what the correct way is to do it?

Thank you very much in advance.

Regards,

Jing

1 ACCEPTED SOLUTION

Accepted Solutions

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Here's an attempt, possibly a good one at that.

data yyy;

L2 = L1;

L1 = Xhat;

retain Xhat L1 L2;

set xxx;

if _n_ < 3 then Xhat = X;

else Xhat = beta1 * L1 + beta2 * L2;

run;

Good luck.

4 REPLIES 4

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Here's an attempt, possibly a good one at that.

data yyy;

L2 = L1;

L1 = Xhat;

retain Xhat L1 L2;

set xxx;

if _n_ < 3 then Xhat = X;

else Xhat = beta1 * L1 + beta2 * L2;

run;

Good luck.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Hi, Astounding:

Thank you very much. It works very well. If there are different groups of data and different parameter values, and I want to do the same thing for each group, can I simply write the code as:

data yyy;

L2 = L1;

L1 = Xhat;

retain Xhat L1 L2;

set xxx;

** by group;**

if _n_ < 3 then Xhat = X;

else Xhat = beta1 * L1 + beta2 * L2;

run;

I know I am being lazy here, but your input is highly appreciated.

Best,

Jing

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Should be possible with minor changes:

data yyy;

L2 = L1;

L1 = Xhat;

retain Xhat L1 L2;

set xxx;

**by group;**

** if first.group then group_counter=1;**

** else group_counter + 1;**

** ** if **group_counter** < 3 then Xhat = X;

else Xhat = beta1 * L1 + beta2 * L2;

run;

If you're working with large amounts of data, you can speed things up a tiny bit by changing the order of the last two statements:

if group_counter > 2 then Xhat = beta1 * L1 + beta2 * L2;

else Xhat = X;

Good luck.

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

**If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. **

Multiple Linear Regression in SAS

Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.

Find more tutorials on the SAS Users YouTube channel.