error G0152 : Variable not initialized

My program gives the following error messages: 

C:\Users\kullanici\Desktop\gaussdeneme\bit(120) : error G0152 : Variable not initialized
Currently active call: campbell [120] C:\Users\kullanici\Desktop\gaussdeneme\bit
Stack trace:
campbell called from C:\Users\kullanici\Desktop\gaussdeneme\bit, line 165
ilt_ssr called from C:\Users\kullanici\Desktop\gaussdeneme\bit, line 25

Below is the part of the program that has the problem:

proc (1) = campbell(x,pmax);
local i,j,p,xl,z,b,dx,u,s,tr,out,t;
   t = rows(x);
   p = pmax;
   do while p >= 1;
      xl = x[p+1:t-1];
      dx = x[p+2:t] - xl;
      z = ones(t-p-1,1)~xl~lagp((x[1:t-1]-x[2:t]),p-1);
      b = inv(z'z)*(z'dx);
      u = dx - z*b;
      s = (u'u)/(t-p-2);
      s = sqrt(diag(s*inv(z'z)));
      tr = abs(b[rows(b),1]/s[rows(s),1]);
      /* j=0; added to avoid initialize problem */

      if tr > 1.96;
         j = p-1;
         goto out;
      endif;
      p = p - 1;
   endo;
   out:    ------------here 120 line 

retp(j);
endp;

 

Anyone help me to solve this problem ?

thanks

1 Answer



0



The cause of the problem
I think the problem is actually on the line of code just below the line that you reference. The problem is that the variable j is only assigned a value if tr > 1.96 during one of the iterations. If that never happens, then your code tries to return j, but j has never been given a value.

How to resolve it
The simplest way to resolve this problem is to give j a value at the beginning of the procedure. For example, something like this:

proc (1) = campbell(x,pmax);
local i,j,p,xl,z,b,dx,u,s,tr,out,t;
   //New line below
   j = 0;
   t = rows(x);
   .
   .
   .
endp;

If you do this, then the procedure will return 0 if tr is never greater than 1.96. While you can do that, we can remove the goto statement and make the program a little simpler which I would recommend.
Simplifying the program and removing the goto
The GAUSS keyword break ends a do or for loop. So we can remove the goto by changing this chunk of code:

      if tr > 1.96;
          j = p-1;
          goto out;
      endif;
      p = p - 1;
   endo;
   out:

to this code:

      //Move the line below above the 'if'
      p = p - 1;
      if tr > 1.96;
         //Replace 'goto' with 'break'
         break; 
      endif;
   endo;  

In the second code snippet, I placed the line:

p = p - 1;

above the if statement. With that line before the if block, when we hit the break statement, p will equal what j would have equaled. So we do not need to use the j variable at all and we can return the value of p every time.

Full version of the simplified procedure
Below is the full code for the version of the procedure that reflects the changes recommended above.

proc (1) = campbell(x,pmax);
    local i,j,p,xl,z,b,dx,u,s,tr,out,t;
    t = rows(x);
    p = pmax;
    do while p >= 1;
        xl = x[p+1:t-1];
        dx = x[p+2:t] - xl;
        z = ones(t-p-1,1)~xl~lagp((x[1:t-1]-x[2:t]),p-1);
        b = inv(z'z)*(z'dx);
        u = dx - z*b;
        s = (u'u)/(t-p-2);
        s = sqrt(diag(s*inv(z'z)));
        tr = abs(b[rows(b),1]/s[rows(s),1]);
        
        //move p = p - 1 up to here
        //since we will do it on every
        //iteration of the 'do' loop
        p = p - 1;
        if tr > 1.96;
            //'break' will exit the 'do loop'
            break;
        endif;
    endo;
    
    //Always return 'p'
    retp(p);
endp;

aptech

1,773

Your Answer

1 Answer

0

The cause of the problem
I think the problem is actually on the line of code just below the line that you reference. The problem is that the variable j is only assigned a value if tr > 1.96 during one of the iterations. If that never happens, then your code tries to return j, but j has never been given a value.

How to resolve it
The simplest way to resolve this problem is to give j a value at the beginning of the procedure. For example, something like this:

proc (1) = campbell(x,pmax);
local i,j,p,xl,z,b,dx,u,s,tr,out,t;
   //New line below
   j = 0;
   t = rows(x);
   .
   .
   .
endp;

If you do this, then the procedure will return 0 if tr is never greater than 1.96. While you can do that, we can remove the goto statement and make the program a little simpler which I would recommend.
Simplifying the program and removing the goto
The GAUSS keyword break ends a do or for loop. So we can remove the goto by changing this chunk of code:

      if tr > 1.96;
          j = p-1;
          goto out;
      endif;
      p = p - 1;
   endo;
   out:

to this code:

      //Move the line below above the 'if'
      p = p - 1;
      if tr > 1.96;
         //Replace 'goto' with 'break'
         break; 
      endif;
   endo;  

In the second code snippet, I placed the line:

p = p - 1;

above the if statement. With that line before the if block, when we hit the break statement, p will equal what j would have equaled. So we do not need to use the j variable at all and we can return the value of p every time.

Full version of the simplified procedure
Below is the full code for the version of the procedure that reflects the changes recommended above.

proc (1) = campbell(x,pmax);
    local i,j,p,xl,z,b,dx,u,s,tr,out,t;
    t = rows(x);
    p = pmax;
    do while p >= 1;
        xl = x[p+1:t-1];
        dx = x[p+2:t] - xl;
        z = ones(t-p-1,1)~xl~lagp((x[1:t-1]-x[2:t]),p-1);
        b = inv(z'z)*(z'dx);
        u = dx - z*b;
        s = (u'u)/(t-p-2);
        s = sqrt(diag(s*inv(z'z)));
        tr = abs(b[rows(b),1]/s[rows(s),1]);
        
        //move p = p - 1 up to here
        //since we will do it on every
        //iteration of the 'do' loop
        p = p - 1;
        if tr > 1.96;
            //'break' will exit the 'do loop'
            break;
        endif;
    endo;
    
    //Always return 'p'
    retp(p);
endp;

You must login to post answers.

Have a Specific Question?

Get a real answer from a real person

Need Support?

Get help from our friendly experts.