Programming the statistical procedures from SAS

One-sided (rolling) HP filter

Posts: 60

One-sided (rolling) HP filter

Dear all,

I have a following problem.

I would like to calculate a trend for some ratio, using one-sided Hodrick-Prescott  filter.  This means, I want to use rolling window and calculate the trend using only the data available up to the given period.  For example

data have;

format period ddmmyy10.;

input period ddmmyy10. ratio;


31/03/2000        7.7035308

30/06/2000        7.704633

30/09/2000        7.7113104

31/12/2000        7.7438095

31/03/2001        7.7623468



In the output dataset I would like to have another variable with the trend for the ratio, where for the first period the trend is equal to the first value of the ratio (7.7035308), for the second period it is calculated on the basis of the first and the second value of the ratio (7.7035308 and 7.704633 respectively), for the third period – on the basis of the  first, second and third value etc.

In the next step, I would like to do the same, but separately for each country A and B, using a panel dataset. For example

data have1;

format period ddmmyy10.;

input period :ddmmyy10. country :$1. ratio ;


31/03/2000        A            7.7035308

30/06/2000        A            7.704633

30/09/2000        A            7.7113104

31/12/2000        A            7.7438095

31/03/2000        B            7.8334211

30/06/2000        B            7.8389886

30/09/2000        B            7.8344153

31/12/2000        B            7.8422493

31/03/2001        B            7.8409916

30/06/2001        B            7.8572007



Again, I want to add another variable to the dataset with rolling HP trend, but calculated separately for country A and B.

Any ideas how to do this? I've looked at the proc timeseries but haven't found any option to include rolling window or set number of observations to be used to calculate the trend.



SAS Employee
Posts: 64

Re: One-sided (rolling) HP filter

Posted in reply to chris2377

Hello Chris,

You can use the following program (based on PROC UCM in SAS/ETS):

proc ucm data=have;

   id period interval=qtr;

   model ratio;

   irregular ;

   level var=0 noest print=filter;

   slope var=0.000625 noest;

   estimate PROFILE;

   forecast ;


For more information see the UCM procedure documentation, particularly the example number 5(Trend Removal Using the Hodrick-Prescott Filter):

SAS/ETS(R) 13.2 User's Guide

The FORECAST variable in the output data set FOR contains the one-step-ahead forecasts of RATIO that

you are looking for.  Forecasts for first two quarters are missing by design because the first two observations

are needed for "initialization" (you can set them to some convenient values if you choose).

From third quarter on, the forecasts are one-sided Hodrick-Prescott filter based.  For your second example,

you can use the same program with COUNTRY as a BY variable (you will need to sort your data by


Hope this helps.


Posts: 60

Re: One-sided (rolling) HP filter

Thank you. I'll look more closely at the proc ucm.

Best regards,


Ask a Question
Discussion stats
  • 2 replies
  • 2 in conversation