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

Hi,

I'm not quit sure how does this work so the header might be misleading or then it's indeed what I'm seeking for.

However, I have this regression procedure which I need to repeat on my sample of 30 firms:

The preregress module formulates dependent and independent variables which are then used in regress module.

I use "read where()" command to input firms from pooled data. Also I have to form every time a vector "resid_firmname = resid". How could I just make a vector out of the firm names and such a loop which would do this procedure firm by firm and also how to formulate such a output vector for every firm?

proc iml;

edit kirjasto.Dax30_hourly09;

/*394 missing values of volume, 264 for ALTG*/

/*BAYG changed to BAYGn on 21.09.2009*/

/*create variables for excess volume regression*/

start preregres (vol, high, low, close) global (x, y);

lnvol=log(vol);

lagvol=lag(lnvol, (1:50));

vola=high/low;

lnvola=log(vola);

lagvola=lag(lnvola, (1:10));

close10=lag(close, -10);

lnclose10=log(close10);

timeorig=T(1:nrow(vol));

tempx=lagvol||lagvola||lnclose10;

xtemp2=tempx[51:(nrow(vol)-10),];

lintrend=T(1:nrow(xtemp2));

constant=j((nrow(xtemp2)),1);

x=constant||xtemp2||lintrend;

y=lnvol[51:(nrow(vol)-10)];

finish;

/*list of firms:

adsg, altg, alvg, basf, bayg, beig, bmwg, cbkg, cong, DAIGn, DB1Gn, dbkg, dpwg, dteg, eong,

fmeg, freg, heig, hnkg, ifxg, lhag, ling, lxsg, mrcg, muvg, rweg, sapg, sdfg, sieg, tkag, vowg

*/

print resid_adsg resid_altg resid_alvg resid_basf resid_bayg resid_beig resid_bmwg resid_cbkg resid_cong resid_daig resid_db1g;

read all var "volume" into volume where(RIC="DB1Gn.DE");

read all var "high" into high where(RIC="DB1Gn.DE");

read all var "close" into close where(RIC="DB1Gn.DE");

read all var "low" into low where(RIC="DB1Gn.DE");

run preregres (volume, high, low, close);

start Regress;                    /* begins module        */

  xpxi = inv(x`*x);               /* inverse of X'X      */

  beta = xpxi * (x`*y);           /* parameter estimate  */

  yhat = x*beta;                  /* predicted values    */

  resid = y-yhat;                 /* residuals           */

  sse = ssq(resid);               /* SSE                 */

  n = nrow(x);                    /* sample size         */

  dfe = nrow(x)-ncol(x);          /* error DF            */

  mse = sse/dfe;                  /* MSE                 */

  cssy = ssq(y-sum(y)/n);         /* corrected total SS  */

  rsquare = (cssy-sse)/cssy;      /* RSQUARE             */

/* print ,"Regression Results", sse dfe mse rsquare; */

  stdb = sqrt(vecdiag(xpxi)*mse); /* std of estimates    */

  t = beta/stdb;                  /* parameter t tests   */

  prob = 1-probf(t#t,1,dfe);      /* p-values            */

  /*print ,"Parameter Estimates",, beta stdb t prob;

  print ,y yhat resid;*/

finish Regress;                   /* ends module          */

run Regress;

create kirjasto.residuals var {resid};

append;

resid_db1g=resid;

close kirjasto.residuals;

1 ACCEPTED SOLUTION

Accepted Solutions
Rick_SAS
SAS Super FREQ

To "loop over firms", follow the ideas in this article: BY-group processing in SAS/IML - The DO Loop .

Outside the loop, use the SETIN and SETOUT statement to define the data sets that you are reading from and to. (For an example, see Sequential access: Reading one observation at a time in SAS/IML software - The DO Loop )

Use the CREATE statement to define the variables for the output data set.

Then loop over the firms. Inside the loop:

1) READ ALL... WHERE(RIC=firm);

2) Compute the regression model for that firm.

3) Write the statistics to the output data set

View solution in original post

2 REPLIES 2
Rick_SAS
SAS Super FREQ

To "loop over firms", follow the ideas in this article: BY-group processing in SAS/IML - The DO Loop .

Outside the loop, use the SETIN and SETOUT statement to define the data sets that you are reading from and to. (For an example, see Sequential access: Reading one observation at a time in SAS/IML software - The DO Loop )

Use the CREATE statement to define the variables for the output data set.

Then loop over the firms. Inside the loop:

1) READ ALL... WHERE(RIC=firm);

2) Compute the regression model for that firm.

3) Write the statistics to the output data set

LauriN
Calcite | Level 5

Oh yeas it was pretty straight forward after all. The outputting remained somewhat unclear for me but I realized another solution to the problem:

I initialized matrix with width = number of firms, length = length of output vector (residuals in this case) and replaced the columns in this vector with real residuals in the do-loop:

proc iml;

use kirjasto.dax30h09v4;

read all var{RIC} into c;    /*reading class variables, firm id's*/

firm=unique(c);

res_matrix=j(2226,ncol(firm),1);

do i = 1 to ncol(firm);

read all where(RIC=(firm));

run preregres (volume, high, low, close);

run Regress;

res_matrix[,i]=resid;

end;

create kirjasto.residuals from res_matrix [colname=firm];

append from res_matrix;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

From The DO Loop
Want more? Visit our blog for more articles like these.
Discussion stats
  • 2 replies
  • 1082 views
  • 0 likes
  • 2 in conversation