Help using Base SAS procedures

Lag function inside a Do Loop

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 19
Accepted Solution

Lag function inside a Do Loop

Hi,

I am using the lag function inside of a do loop, however it seems to return the current value of the variable, not the lagged value. I did some testing, if I do the calculation outside of the loop, such as test = lag(var[1]); it works fine. however that same code inside the loop does not return a lagged value, but the current one. I have used very similar statements in other parts of my code which work fine, not sure what is different in this case.

desired result for st08, obs 6 = (-1) + (0)*(0.8) +  (0)*(0.8)*(0.8) + (0)*(0.8)*(0.8)*(0.8) = -1

actual result = (-1) + (-1)*(0.8) +  (-1)*(0.8)*(0.8) + (-1)*(0.8)*(0.8)*(0.8) = -2.952

data star;

     input star;

     datalines;

     0

     0

     0

     0

     0

     -1

run;

data star;

     set star;

array var[1] star;
array result
  • st01-st09;
  • do i=1 to 9;

    result = var[1] + lag(var[1])*(i/10) + lag2(var[1])*(i/10)*(i/10) + lag3(var[1])*(i/10)*(i/10)*(i/10);
    end;

    run;

    Any ideas on what is wrong?


    Accepted Solutions
    Solution
    ‎09-25-2012 01:29 PM
    PROC Star
    Posts: 7,468

    Re: Lag function inside a Do Loop

    Each lag has its own que, thus there is a separate one for lag, lag2, lag3, etc.  In the code you said worked before, you only took the lags once for each variable within each iteration.  Thus, one record's values only appeared in the que once.

    In your current code you are taking the lags of the same variable 9 times within each iteration.  Thus, on the first iteration lag is getting the value from the ninth iteration of the last record, then from the first iteration of the current record, then from the second iteration of the current record, etc.

    View solution in original post


    All Replies
    PROC Star
    Posts: 7,468

    Re: Lag function inside a Do Loop

    Not sure what you are trying to achieve, but I see two potential problems.

    First, lag is just a que, not necessarily the last record.

    Second, is the question of how you want to deal with cases when one or more of the lags are missing.

    Does the following do what you want?:

    data star;

         input star;

         datalines;

         0

         0

         0

         0

         0

         -1

    run;

    data star (drop=lag: i);

      set star;

      array var[1] star;

      array result

  • st01-st09;
  •   lag_1=lag(var[1]);

      lag_2=lag2(var[1]);

      lag_3=lag3(var[1]);

      do i=1 to 9;

        result = sum(var[1],lag_1*(i/10),lag_2*(i/10)*(i/10),lag_3*(i/10)*(i/10)*(i/10));

      end;

    run;

    Occasional Contributor
    Posts: 19

    Re: Lag function inside a Do Loop

    Thanks Arthur, your code works just fine, I am just unclear as to why mine doesn't. I have used the lag function in other pieces of code, such as

    do i=1 to dim(var4);

      serial1 = lag(var4);

      serial2 = lag2(var4);

      serial3 = lag3(var4);

    end;

    I have had no problems with the above, so I am not sure why the lag function must be performed outside the loop in my current code.

    Solution
    ‎09-25-2012 01:29 PM
    PROC Star
    Posts: 7,468

    Re: Lag function inside a Do Loop

    Each lag has its own que, thus there is a separate one for lag, lag2, lag3, etc.  In the code you said worked before, you only took the lags once for each variable within each iteration.  Thus, one record's values only appeared in the que once.

    In your current code you are taking the lags of the same variable 9 times within each iteration.  Thus, on the first iteration lag is getting the value from the ninth iteration of the last record, then from the first iteration of the current record, then from the second iteration of the current record, etc.

    PROC Star
    Posts: 1,322

    Re: Lag function inside a Do Loop

    Agree with Art's summary.  I rewrote your step with some put statements to show the values on each iteration of the do-loop.

    14   data star;
    15     set star;
    16
    17     array var[1] star;
    18     array result
  • st01-st09; 19 20     do i=1 to 9; 21       lag1=lag(var[1]); 22       lag2=lag2(var[1]); 23       lag3=lag3(var[1]); 24       put (_n_ star i lag1 lag2 lag3)(=); 25    end; 26 27   run; _N_=1 star=0 i=1 lag1=. lag2=. lag3=. _N_=1 star=0 i=2 lag1=0 lag2=. lag3=. _N_=1 star=0 i=3 lag1=0 lag2=0 lag3=. _N_=1 star=0 i=4 lag1=0 lag2=0 lag3=0 _N_=1 star=0 i=5 lag1=0 lag2=0 lag3=0 _N_=1 star=0 i=6 lag1=0 lag2=0 lag3=0 _N_=1 star=0 i=7 lag1=0 lag2=0 lag3=0 _N_=1 star=0 i=8 lag1=0 lag2=0 lag3=0 _N_=1 star=0 i=9 lag1=0 lag2=0 lag3=0 _N_=2 star=0 i=1 lag1=0 lag2=0 lag3=0 _N_=2 star=0 i=2 lag1=0 lag2=0 lag3=0 _N_=2 star=0 i=3 lag1=0 lag2=0 lag3=0 _N_=2 star=0 i=4 lag1=0 lag2=0 lag3=0 _N_=2 star=0 i=5 lag1=0 lag2=0 lag3=0 _N_=2 star=0 i=6 lag1=0 lag2=0 lag3=0 _N_=2 star=0 i=7 lag1=0 lag2=0 lag3=0 _N_=2 star=0 i=8 lag1=0 lag2=0 lag3=0 _N_=2 star=0 i=9 lag1=0 lag2=0 lag3=0 _N_=3 star=0 i=1 lag1=0 lag2=0 lag3=0 _N_=3 star=0 i=2 lag1=0 lag2=0 lag3=0 _N_=3 star=0 i=3 lag1=0 lag2=0 lag3=0 _N_=3 star=0 i=4 lag1=0 lag2=0 lag3=0 _N_=3 star=0 i=5 lag1=0 lag2=0 lag3=0 _N_=3 star=0 i=6 lag1=0 lag2=0 lag3=0 _N_=3 star=0 i=7 lag1=0 lag2=0 lag3=0 _N_=3 star=0 i=8 lag1=0 lag2=0 lag3=0 _N_=3 star=0 i=9 lag1=0 lag2=0 lag3=0 _N_=4 star=0 i=1 lag1=0 lag2=0 lag3=0 _N_=4 star=0 i=2 lag1=0 lag2=0 lag3=0 _N_=4 star=0 i=3 lag1=0 lag2=0 lag3=0 _N_=4 star=0 i=4 lag1=0 lag2=0 lag3=0 _N_=4 star=0 i=5 lag1=0 lag2=0 lag3=0 _N_=4 star=0 i=6 lag1=0 lag2=0 lag3=0 _N_=4 star=0 i=7 lag1=0 lag2=0 lag3=0 _N_=4 star=0 i=8 lag1=0 lag2=0 lag3=0 _N_=4 star=0 i=9 lag1=0 lag2=0 lag3=0 _N_=5 star=0 i=1 lag1=0 lag2=0 lag3=0 _N_=5 star=0 i=2 lag1=0 lag2=0 lag3=0 _N_=5 star=0 i=3 lag1=0 lag2=0 lag3=0 _N_=5 star=0 i=4 lag1=0 lag2=0 lag3=0 _N_=5 star=0 i=5 lag1=0 lag2=0 lag3=0 _N_=5 star=0 i=6 lag1=0 lag2=0 lag3=0 _N_=5 star=0 i=7 lag1=0 lag2=0 lag3=0 _N_=5 star=0 i=8 lag1=0 lag2=0 lag3=0 _N_=5 star=0 i=9 lag1=0 lag2=0 lag3=0 _N_=6 star=-1 i=1 lag1=0 lag2=0 lag3=0 _N_=6 star=-1 i=2 lag1=-1 lag2=0 lag3=0 _N_=6 star=-1 i=3 lag1=-1 lag2=-1 lag3=0 _N_=6 star=-1 i=4 lag1=-1 lag2=-1 lag3=-1 _N_=6 star=-1 i=5 lag1=-1 lag2=-1 lag3=-1 _N_=6 star=-1 i=6 lag1=-1 lag2=-1 lag3=-1 _N_=6 star=-1 i=7 lag1=-1 lag2=-1 lag3=-1 _N_=6 star=-1 i=8 lag1=-1 lag2=-1 lag3=-1 _N_=6 star=-1 i=9 lag1=-1 lag2=-1 lag3=-1 NOTE: There were 6 observations read from the data set WORK.STAR. NOTE: The data set WORK.STAR has 6 observations and 14 variables
  • HTH

    --Q.

    🔒 This topic is solved and locked.

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

    Discussion stats
    • 4 replies
    • 1543 views
    • 4 likes
    • 3 in conversation