BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Khaladdin
Quartz | Level 8

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.

1 ACCEPTED SOLUTION

Accepted Solutions
SuzanneDorinski
Lapis Lazuli | Level 10

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;
	forecast plot=forecasts lead=10 alpha=0.5;
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;

View solution in original post

4 REPLIES 4
SuzanneDorinski
Lapis Lazuli | Level 10

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;
	forecast plot=forecasts lead=10 alpha=0.5;
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;
Khaladdin
Quartz | Level 8
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 🙂
SuzanneDorinski
Lapis Lazuli | Level 10

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.

Khaladdin
Quartz | Level 8
Thank you very much. It works.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 643 views
  • 1 like
  • 2 in conversation