BookmarkSubscribeRSS Feed
Mubatsiri
Calcite | Level 5

Good Day

I am running the following code:

data ass3;

input income1 income2 house stand double prop;

cards;

  521.502    118.348    735.779     920.53      0     1215.86

   14.116    457.801    413.522     690.15      0      917.67

  308.237    205.341    567.238     903.11      1     1201.16

  449.589    157.470    496.226     659.05      1     1099.73

   47.286    555.871    414.292     769.92      0     1077.25

   12.702    400.744    283.223     539.62      1      973.28

  303.539    360.630    671.121     934.58      0     1206.01

  325.548    369.610    284.473     707.85      0     1071.03

  328.079     17.192    492.552     699.23      0      834.60

  479.735     34.212    767.408    1097.32      0     1102.11

   70.381    319.148    373.140     760.19      0      774.12

  232.232    255.517    238.515     577.39      0      828.64

   56.125    326.705    589.865     930.42      0     1020.10

  510.569     36.773    461.059     920.65      0     1044.39

   15.890    353.851    345.385     655.05      1      932.65

  298.906    126.398    531.592    1093.24      0     1036.68

  280.401    105.089    497.296     727.87      0      867.00

  188.411    419.229    383.097     903.32      0     1114.49

   11.004    462.602    351.969     575.44      0      833.46

  408.952    119.757    650.882     950.26      0     1044.39

  114.999    253.868    439.853     849.32      0      831.60

  200.932    141.234    400.907     571.64      0      773.70

  276.907    350.366    554.191     948.33      1     1273.24

  271.076    109.235    734.862     970.72      1     1124.66

  357.141    324.151    507.147     686.02      0     1146.86

   74.029    403.535    372.881     520.79      0      836.79

  112.752    195.755    550.987    1048.71      0     1023.95

  189.496    273.100    400.458     550.31      0      834.02

  283.516    395.697    445.404     600.35      0     1064.84

  255.701    154.743    535.123    1078.51      0     1075.30

  ;

run;

proc iml;

use ass3;

read all into EKT3;

print EKT3;

JI = EKT3[,1]+EKT3[,2];   /* to create the joint the joint income variable */

rat = EKT3[,3]/EKT3[,4];  /* to create the ratio variable as defined */

n=30;

p= 4; /* number of indepndenr varibles excluding intercept */

k=5;       /* number of variables */

m = 2;    /* number of linear restrictions */

/*part a */

In = J(nrow(EKT3),1,1);

X = In || JI || EKT3[ ,4] || rat || EKT3[ ,5];

Y = EKT3[ ,6];

bhat = inv(X`*X)*X`*y;

print bhat;

bnew = J(5,n,.);

/*part b */

do i = 1 to n;

if i = 1 then do;

Xnew = X[2:n,];

ynew = EKT3[2:n,];

end;

if  i = n then do ;

Xnew = X[1:n-1,];

ynew = EKT3[1:n-1,];

end;

else do;

Xnew = X[1:i-1,] || X[i+1:n,];

ynew = EKT3[1:i+1,] || EKT3[i+1:n,];

end;

bhat2 = ginv(Xnew`*Xnew)*Xnew`*ynew;

bnew[,i]= bhat2;

end;

print bnew;

quit;

I keep getting the error message

ERROR: (execution) Invalid subscript or subscript out of range.

operation : [ at line 1588 column 13

operands  : X, *LIT1024, _TEM1001,

X     30 rows      5 cols    (numeric)

*LIT1024      1 row       1 col     (numeric)

         1

_TEM1001      1 row       1 col     (numeric)

         0

statement : ASSIGN at line 1588 column 5

1594  print bnew;

1595  quit;

         0

I have tried to fix the error but it is somehow still not running. Any advise on how I can get the code to run smoothly?

4 REPLIES 4
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Move your question to the SAS/IML section and you will get more coverage of people who use that technology.

Rick_SAS
SAS Super FREQ

In the following statements

else do;

Xnew = X[1:i-1,] || X[i+1:n,];

...

The ELSE clause is being executed when i=1, which implies that te vector 1:i-1 evaluates to {1 0}, which is causing the "subscript out of range" error.

It looks like your loop is attempting to omit the i_th row. You can use the SETDIF function to generate the vector

rows = setdif(1:n, i);  /* rows except the i_th */

For a detailed explanation, see the article

"Negative indexing" in SAS/IML: Excluding elements from an array

Mubatsiri
Calcite | Level 5

Thanks Rick it worked

Ksharp
Super User

You missed ELSE before " if  i = n " ,

and you want // (vertical operator) not || (horizontal)  right ?

and at the end of code , you need to consider about

bnew[, i]=bhat2;

which bhat2 is not column vector.

Code: Program

data ass3;
   input income1 income2 house stand double prop;
   cards;
  521.502 118.348 735.779 920.53 0 1215.86
  14.116 457.801 413.522 690.15 0 917.67
  308.237 205.341 567.238 903.11 1 1201.16
  449.589 157.470 496.226 659.05 1 1099.73
  47.286 555.871 414.292 769.92 0 1077.25
  12.702 400.744 283.223 539.62 1 973.28
  303.539 360.630 671.121 934.58 0 1206.01
  325.548 369.610 284.473 707.85 0 1071.03
  328.079 17.192 492.552 699.23 0 834.60
  479.735 34.212 767.408 1097.32 0 1102.11
  70.381 319.148 373.140 760.19 0 774.12
  232.232 255.517 238.515 577.39 0 828.64
  56.125 326.705 589.865 930.42 0 1020.10
  510.569 36.773 461.059 920.65 0 1044.39
  15.890 353.851 345.385 655.05 1 932.65
  298.906 126.398 531.592 1093.24 0 1036.68
  280.401 105.089 497.296 727.87 0 867.00
  188.411 419.229 383.097 903.32 0 1114.49
  11.004 462.602 351.969 575.44 0 833.46
  408.952 119.757 650.882 950.26 0 1044.39
  114.999 253.868 439.853 849.32 0 831.60
  200.932 141.234 400.907 571.64 0 773.70
  276.907 350.366 554.191 948.33 1 1273.24
  271.076 109.235 734.862 970.72 1 1124.66
  357.141 324.151 507.147 686.02 0 1146.86
  74.029 403.535 372.881 520.79 0 836.79
  112.752 195.755 550.987 1048.71 0 1023.95
  189.496 273.100 400.458 550.31 0 834.02
  283.516 395.697 445.404 600.35 0 1064.84
  255.701 154.743 535.123 1078.51 0 1075.30
  ;
run;

proc iml ;
   use ass3;
   read all into EKT3;
   print EKT3;
   JI=EKT3[, 1]+EKT3[, 2];

   /* to create the joint the joint income variable */
   rat=EKT3[, 3]/EKT3[, 4];

   /* to create the ratio variable as defined */
   n=30;
   p=4;

   /* number of indepndenr varibles excluding intercept */
   k=5;

   /* number of variables */
   m=2;

   /* number of linear restrictions */
   /*part a */
   In=J(nrow(EKT3), 1, 1);
   X=In || JI || EKT3[ , 4] || rat || EKT3[ , 5];
   Y=EKT3[ , 6];
   bhat=inv(X`*X)*X`*y;
   print bhat;
   bnew=J(5, n, .);

   /*part b */
   do i=1 to n;

   if i=1 then
   do;
   Xnew=X[2:n, ];
   ynew=EKT3[2:n, ];
   end;
   else if i=n then
   do;
   Xnew=X[1:n-1, ];
   ynew=EKT3[1:n-1, ];
   end;
   else
   do;
   Xnew=X[1:i-1, ]//X[i+1:n, ];
   ynew=EKT3[1:i-1, ]//EKT3[i+1:n, ];
   end;
   bhat2=ginv(Xnew`*Xnew)*Xnew`*ynew;
   bnew[, i]=bhat2[1];
   end;
   print bnew;
   quit;

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 4 replies
  • 1403 views
  • 0 likes
  • 4 in conversation