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

I am using SAS 9.4 and I cannot figure out why the matrix has not been set to a value. My have uploaded my data sample and my coding is like this :

 

proc import out=pepper
datafile='C:\Users\Cnord\Desktop\UMT\Sem 5\PITA\Proposal\Jan05toMay15.xls'
dbms=excel replace;
sheet='Sheet1';
getnames=yes;
run;

 

proc print data=pepper;
run;

 

Proc iml;
use pepper;
read all into y;
pdh=j(1,4,999);
print pdh;
APEh=j(4,1,0);
print APEh;
u0=sum(y)/nrow(y);
n=nrow(y);
sum=sum(y);
print sum;
print n;
print u0;
do p=1 to 8;
            do d=1 to p;
                      x0=j(nrow(y),p,0);
                      do i= p+1 to nrow(y);
                                  x0[i,]=y[i-1:i-p]`;
                      end;
                      do h=0.1 to 1 by 0.1;
                                   do j=1 to 4;
                                               t=nrow(y)-(7*j);
                                               n=t-p;
                                               x=j(t,p,0);
                                               u=j(t,1,0);
                                               do i= p+1 to t;
                                                            x[i,]=y[i-1:i-p]`;
                                                            u[i]=y[i-d];
                                               end;
                                               x1=x[p+1:t,];
                                               u1=u[p+1:t,];
                                               w=j(n,n,0);
                                               w1=j(n,1,0);
                                               x2=j(n,p,1);
                                               do i=1 to t;
                                                          ind=(ul[i]-u0)/h;
                                                          if (ind<=1) then do;
                                                          w[i,i]=(0.75/h)*(1-((u1[i]-u0)*(u1[i]-u0))/(h*h));
                                                          w1[i,]=w[i,i];
                                                          x2[i,]=x1[i,]*(u1[i]-u0);
                                                          end;
                                                          else do;
                                                          w[i,i]=0;
                                                          w1[i,]=w[i,i];
                                                          x2[i,]=x1[i,]*(u1[i]-u0);
                                                          end;
                                               end;
                                               Xcurl=j(n,2*p,0);
                                               xcurl=xl||x2;
                                               y1=y[p+1:t,];
                                               beta=inv(xcurl`*w*xcurl)*(xcurl`*w*y1);
                                               beta1=beta[1: p,];
                                               yhat=xcurl*beta;
                                               error=y1-yhat;
                                               apeerror=j(7,1,0);
                                               apeerror=error[n-7*j+1:n-7*j+7,];
                                               APEh[j]=(1/7)*apeerror`*apeerror;
                                    end;
                                    APE=sum(APEh)/4;
                                    pdh=pdh//(p||d||h||APE);
                        end;
             end;
end;
pdh=pdh[2:nrow(pdh),];
minAPE=min(pdh[,4]);
do i=1 to nrow(pdh);
            if (pdh[i,4]=minAPE) then rw=i;
end;
result=pdh[rw,];
print beta1;

1 ACCEPTED SOLUTION

Accepted Solutions
IanWakeling
Barite | Level 11

I think the line

  ind=(ul[i]-u0)/h;

has an unintended lower case letter L instead of the digit 1.   However if I fix that I get a subscript error on the same line.   I am not really sure what you are trying to do, but have you calculated t correctly?

 

View solution in original post

2 REPLIES 2
PeterClemmensen
Tourmaline | Level 20

Firstly, you have mistaked ul with u1 (See the revised code) in the beginnig of your do i=1 to t loop 🙂

 

data pepper;
input y @@;
datalines;
4.354 4.316 4.218 4.228 4.265 4.268 4.143 3.961 4.021
4.268 4.622 4.658 4.698 4.618 4.555 4.452 4.512 5.030
6.025 6.736 8.009 8.518 8.077 7.740 7.725 8.321 8.660
9.185 10.574 10.101 9.878 9.705 9.433 9.094 9.048 8.522
8.468 9.211 9.911 9.400 9.716 9.414 8.718 8.162 7.900
7.487 7.188 6.922 6.509 6.686 6.763 6.828 7.131 7.510
8.469 8.522 8.891 9.295 9.432 9.350 9.371 9.360 9.371
9.403 9.609 10.338 10.581 10.678 10.351 10.042 10.350
11.602 12.314 12.297 12.214 12.938 13.471 13.508 13.557
13.702 14.891 16.813 15.935 15.521 15.878 16.283 17.313
17.326 17.118 16.989 15.655 14.719 16.210 16.252 16.150
16.257 16.221 16.310 16.840 16.862 15.655 15.495 16.428
15.955 15.924 17.098 17.784 19.028 19.207 18.946 19.097
20.402 20.798 20.916 21.890 24.693 25.215 26.159 26.592
27.282 27.202 26.176 26.750 28.550 25.758
;

Proc iml;
use pepper;
read all into y;

pdh=j(1,4,999);
print pdh;

APEh=j(4,1,0);
print APEh;

u0=sum(y)/nrow(y);
n=nrow(y);
sum=sum(y);
print sum;print n; print u0;

do p=1 to 8;
            do d=1 to p;
                      x0=j(nrow(y),p,0);
                      do i= p+1 to nrow(y);
                                  x0[i,]=y[i-1:i-p]`;
                      end;
                      do h=0.1 to 1 by 0.1;
                                   do j=1 to 4;
                                               t=nrow(y)-(7*j);
                                               n=t-p;
                                               x=j(t,p,0);
                                               u=j(t,1,0);
                                               do i= p+1 to t;
                                                            x[i,]=y[i-1:i-p]`;
                                                            u[i]=y[i-d];
                                               end;
                                               x1=x[p+1:t,];
                                               u1=u[p+1:t,];
                                               w=j(n,n,0);
                                               w1=j(n,1,0);
                                               x2=j(n,p,1);

                                               do i=1 to t;
                                                          ind=(u1[i]-u0)/h; /* Old Line: ind=(ul[i]-u0)/h; */
                                                          if (ind<=1) then do;
                                                          w[i,i]=(0.75/h)*(1-((u1[i]-u0)*(u1[i]-u0))/(h*h));
                                                          w1[i,]=w[i,i];
                                                          x2[i,]=x1[i,]*(u1[i]-u0);
                                                          end;
                                                          else do;
                                                          w[i,i]=0;
                                                          w1[i,]=w[i,i];
                                                          x2[i,]=x1[i,]*(u1[i]-u0);
                                                          end;
                                               end;
                                               Xcurl=j(n,2*p,0);
                                               xcurl=xl||x2;
                                               y1=y[p+1:t,];
                                               beta=inv(xcurl`*w*xcurl)*(xcurl`*w*y1);
                                               beta1=beta[1: p,];
                                               yhat=xcurl*beta;
                                               error=y1-yhat;
                                               apeerror=j(7,1,0);
                                               apeerror=error[n-7*j+1:n-7*j+7,];
                                               APEh[j]=(1/7)*apeerror`*apeerror;
                                    end;
                                    APE=sum(APEh)/4;
                                    pdh=pdh//(p||d||h||APE);
                        end;
             end;
end;

pdh=pdh[2:nrow(pdh),];
minAPE=min(pdh[,4]);

do i=1 to nrow(pdh);
if (pdh[i,4]=minAPE) then rw=i;
end;

result=pdh[rw,];
print beta1;

quit;

Secondly, I think you should have a look at your subscripts. Since I am not sure exactly what you are trying to do, I will not dig too far into it.

 

Hope this gets you started. 

 

EDIT: Also, remember to close your datasets when reading/writing from/to them, so that

 

use pepper;
read all into y;

becomes

 

use pepper;
read all into y;
close pepper;

 

Regards.

IanWakeling
Barite | Level 11

I think the line

  ind=(ul[i]-u0)/h;

has an unintended lower case letter L instead of the digit 1.   However if I fix that I get a subscript error on the same line.   I am not really sure what you are trying to do, but have you calculated t correctly?

 

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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
  • 2518 views
  • 4 likes
  • 3 in conversation