# help with proc iml

#### bran

##### New Member
Hi

sorry if you have see this on the stats page as got the pages mixed up.
I am having some real trouble with proc iml. I am trying to do a matrix regression using IML.

I can solve the equations for each month (sas code below), but i really need the results as a regression.

basic equation id
- - --- --- -- -
|x | |a1 b1 c1 d1 e1 | | f |
|y | = |a2 b2 c2 d2 e2 | | g |
|z | |a3 b3 c3 d3 e3 | | h |
- - --- --- | | i |
| j |

I can solve this as an equation, but the figures change by month. So I would like to generate a regression formula for x, y and z

- - -- ---
|x |= |a1f + b1g + c1h + d1i + e1j + intercept |
|y |= |a2f + b2g + c2h + d2i + e2j + intercept |
|z |= |a3f + b3g + c3h + d3i + e3j + intercept |

My known figures are x,y,z and f,g,h,i,j

so far my iml code is looking quite poor ( printed below). Am I approaching this from the wrong direction? Currently all I need to calculate will be the parameter estimates.

Thanks
Alun

* sample of date ;

data work.lp;
input date $centre$ cost ;
cards ;
JAN06 directory 27733.77
JAN06 press 0.00
JAN06 tv 197266.91
JAN06 inserts 15557.68
JAN06 mailings 2370.00
JAN06 outdoor 0.00
JAN06 mags 23075.68
FEB06 directory 25503.14
FEB06 press 0.00
FEB06 tv 214140.26
FEB06 inserts 13133.49
FEB06 mailings 2370.00
FEB06 outdoor 0.00
FEB06 mags 22236.58
MAR06 directory 25503.13
MAR06 press 19723.50
MAR06 tv 241377.96
MAR06 inserts 8552.49
MAR06 mailings 2370.00
MAR06 outdoor 0.00
MAR06 mags 22933.64
APR06 directory 18692.40
APR06 press 1552.96
APR06 tv 260345.25
APR06 inserts 9170.39
APR06 mailings 5113.94
APR06 outdoor 2743.94
APR06 mags 13502.20
MAY06 directory 18692.40
MAY06 press 0.00
MAY06 tv 257231.00
MAY06 inserts 14045.21
MAY06 mailings 2370.00
MAY06 outdoor 0.00
MAY06 mags 11453.18
JUN06 directory 18692.40
JUN06 press 0.00
JUN06 tv 282514.74
JUN06 inserts 8009.30
JUN06 mailings 2430.31
JUN06 outdoor 0.00
JUN06 mags 11900.03
JUL06 directory 25618.58
JUL06 press 0.00
JUL06 tv 216692.76
JUL06 inserts 0.00
JUL06 mailings 2310.00
JUL06 outdoor 0.00
JUL06 mags 12496.61
AUG06 directory 25618.58
AUG06 press 0.00
AUG06 tv 241297.23
AUG06 inserts 24166.67
AUG06 mailings 2557.24
AUG06 outdoor 0.00
AUG06 mags 13930.52
SEP06 directory 25618.58
SEP06 press 6062.78
SEP06 tv 240243.57
SEP06 inserts 21135.71
SEP06 mailings 2257.00
SEP06 outdoor 0.00
SEP06 mags 14890.14
OCT06 directory 25694.76
OCT06 press 6793.90
OCT06 tv 258143.73
OCT06 inserts 44243.71
OCT06 mailings 2067.00
OCT06 outdoor 0.00
OCT06 mags 20189.03
NOV06 directory 25694.76
NOV06 press 0.00
NOV06 tv 178671.48
NOV06 inserts 20629.03
NOV06 mailings 1556.00
NOV06 outdoor 0.00
NOV06 mags 13897.51
DEC06 directory 25694.76
DEC06 press 0.00
DEC06 tv 107404.48
DEC06 inserts 0.00
DEC06 mailings 1373.00
DEC06 outdoor 0.00
DEC06 mags 10660.41
;
run;

data work.sales;
input date $group$ sales ;
cards ;
JAN06 DIRsale 13694
JAN06 aggsale 105
JAN06 indirsale 5035
FEB06 dirsale 11887
FEB06 aggsale 184
FEB06 indirsale 4921
MAR06 dirsale 13606
MAR06 aggsale 225
MAR06 indirsale 4947
APR06 dirsale 12836
APR06 aggsale 307
APR06 indirsale 4733
MAY06 dirsale 13565
MAY06 aggsale 321
MAY06 indirsale 4604
JUN06 dirsale 13002
JUN06 aggsale 281
JUN06 indirsale 4448
JUL06 dirsale 12404
JUL06 aggsale 392
JUL06 indirsale 4622
AUG06 dirsale 12872
AUG06 aggsale 441
AUG06 indirsale 4217
SEP06 dirsale 12551
SEP06 aggsale 459
SEP06 indirsale 4005
OCT06 dirsale 12821
OCT06 aggsale 606
OCT06 indirsale 3756
NOV06 dirsale 10264
NOV06 aggsale 354
NOV06 indirsale 3030
DEC06 dirsale 7747
DEC06 aggsale 239
DEC06 indirsale 2413
;
run;

%macro matrix(var1);

proc iml;
* Read data into IML ;
use work.sales;
read all var {sales} into a where (dat="&var1");
use work.cost;
read all var {cost} into da where (dat="&var1");
d = da`; *transform the data;
di = ((d*da)**-1)*d;
print a;
print d;
print da;
print di;
res=a* di;
sales={direct, direct, direct,direct, direct, direct,direct, direct, direct,
indirect, indirect, indirect,indirect, indirect, indirect,indirect, indirect, indirect,
agg, agg, agg,agg, agg, agg,agg, agg, agg};
date = {&var1, &var1, &var1,&var1, &var1, &var1,&var1, &var1, &var1,
&var1, &var1, &var1,&var1, &var1, &var1,&var1, &var1, &var1,
&var1, &var1, &var1,&var1, &var1, &var1,&var1, &var1, &var1};
print res [rowname=sales];;

create work.&var1 var {date sales res da} ; /****** this and next line ***/
append ; /** LETS UYOU STORE THE RESULT INTO A FILE CALLED WORK.RES **/

quit;

%mend;

%matrix(JAN06);
%matrix(FEB06);
%matrix(MAR06);
%matrix(APR06);
%matrix(MAY06);
%matrix(JUN06);
%matrix(JUL06);
%matrix(AUG06);
%matrix(SEP06);
%matrix(OCT06);
%matrix(NOV06);
%matrix(DEC06);

data work.all;
set work.JAN06 work.FEB06 work.MAR06 work.APR06 work.MAY06
work.JUN06 work.JUL06 work.AUG06 work.SEP06 work.OCT06
work.NOV06 work.DEC06 ;
run;