Solved
Contributor
Posts: 64

# SAS_StateSpaceModels

[ Edited ]

Hi all,

I hope all of you are doing well. I have a question regarding the State Space Models. As you probably know, by using these models we can compute the error variances of irregular component and level component by using one observable variable. For example, assume that we have one variable; it is price of the asser. State Space Models help us to find the level and irregular components of this variable. I use the following codes in order to compute that:

proc ucm data = work;
model price (price is my observable variable);
irregular plot = smooth;
level checkbreak plot = smooth;
estimate plot = residual;
forecast plot = forecasts lead = 10 alpha = 0.5;
run;

My problem is, I need to find the error variances of irregular and level components for each group. Above mentioned code help me to find these variances by using data of all groups. For simplicity, I explain it by using a simple data. I have the following datasheet:

group         price

A                0.5

A                0.4

A                0.8

A                0.1

B                0.3

B                0.2

B                0.5

I want to get the following datasheet: ( assume that er.variance  of irregular.c for A is 0.1, assume that er.variance  of irr.egularc for B is 0.2, assume that er.variance of level.c for A is 0.3, assume that er.variance of level.c for B is 0.4)

group         price          Error variances of irregular components (irr.c)            Error variances of level components

A                0.5             0.1                                                                                0.3

A                0.4             0.1                                                                                0.3

A                0.8             0.1                                                                                0.3

A                0.1             0.1                                                                                0.3

B                0.3             0.2                                                                                0.4

B                0.2             0.2                                                                                0.4

B                0.5             0.2                                                                                0.4

I hope I can explain my issue. Sorry for any misunderstanding.

Accepted Solutions
Solution
‎08-18-2017 12:31 PM
Frequent Contributor
Posts: 118

## Re: SAS_StateSpaceModels

You can use a BY statement in PROC UCM to get the analysis by group.  You can use the ODS OUTPUT statement to save the parameter estimates in a data set.  Then you can combine the parameter estimates with the original data set.

``````data work;
input group \$ price;
datalines;
A                0.5
A                0.4
A                0.8
A                0.1
B                0.3
B                0.2
B                0.5
;
run;

proc print data=work;
run;

ods trace on;

ods select ParameterEstimates;

ods output ParameterEstimates=myEstimates;

proc ucm data=work;
model price;
by group;
irregular plot=smooth;
level checkbreak plot=smooth;
estimate plot=residual;
run;

proc print data=myEstimates;
run;

proc transpose data=myEstimates(keep=group component estimate)
out=transposedEstimates;
by group;
id component;
run;

proc print data=transposedEstimates;
run;

proc sql;
create table myResults as
select a.*,
b.irregular as IrregularComponent,
b.level as LevelComponent
from work as a,
transposedEstimates as b
where a.group=b.group;
quit;

proc print data=myResults;
run;
``````

All Replies
Solution
‎08-18-2017 12:31 PM
Frequent Contributor
Posts: 118

## Re: SAS_StateSpaceModels

You can use a BY statement in PROC UCM to get the analysis by group.  You can use the ODS OUTPUT statement to save the parameter estimates in a data set.  Then you can combine the parameter estimates with the original data set.

``````data work;
input group \$ price;
datalines;
A                0.5
A                0.4
A                0.8
A                0.1
B                0.3
B                0.2
B                0.5
;
run;

proc print data=work;
run;

ods trace on;

ods select ParameterEstimates;

ods output ParameterEstimates=myEstimates;

proc ucm data=work;
model price;
by group;
irregular plot=smooth;
level checkbreak plot=smooth;
estimate plot=residual;
run;

proc print data=myEstimates;
run;

proc transpose data=myEstimates(keep=group component estimate)
out=transposedEstimates;
by group;
id component;
run;

proc print data=transposedEstimates;
run;

proc sql;
create table myResults as
select a.*,
b.irregular as IrregularComponent,
b.level as LevelComponent
from work as a,
transposedEstimates as b
where a.group=b.group;
quit;

proc print data=myResults;
run;
``````
Contributor
Posts: 64

## Re: SAS_StateSpaceModels

Dear Suzanne,
I have a question about your last code. if I got it correctly, I need to write the group name (a or b) for this code. If I have a lot of group, what Can I do. The other codes are perfectly ok. You are an amazing
Frequent Contributor
Posts: 118

## Re: SAS_StateSpaceModels

The a and b in the PROC SQL are not the group names -- they are shortcuts that PROC SQL uses to know which data set to get the variables from.

Contributor
Posts: 64