Statistical programming, matrix languages, and more

Inputting and outputting into and from do loop on basis of name vector?

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 14
Accepted Solution

Inputting and outputting into and from do loop on basis of name vector?

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[51Smiley Sadnrow(vol)-10),];

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

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

x=constant||xtemp2||lintrend;

y=lnvol[51Smiley Sadnrow(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;


Accepted Solutions
Solution
‎09-03-2014 09:17 AM
SAS Super FREQ
Posts: 3,390

Re: Inputting and outputting into and from do loop on basis of name vector?

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


All Replies
Solution
‎09-03-2014 09:17 AM
SAS Super FREQ
Posts: 3,390

Re: Inputting and outputting into and from do loop on basis of name vector?

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

Occasional Contributor
Posts: 14

Re: Inputting and outputting into and from do loop on basis of name vector?

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;

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 322 views
  • 0 likes
  • 2 in conversation