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

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?

    1 ACCEPTED SOLUTION

    Accepted Solutions
    art297
    Opal | Level 21

    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

    4 REPLIES 4
    art297
    Opal | Level 21

    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;

    warnost
    Calcite | Level 5

    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.

    art297
    Opal | Level 21

    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.

    Quentin
    Super User

    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.

    The Boston Area SAS Users Group (BASUG) is hosting our in person SAS Blowout on Oct 18!
    This full-day event in Cambridge, Mass features four presenters from SAS, presenting on a range of SAS 9 programming topics. Pre-registration by Oct 15 is required.
    Full details and registration info at https://www.basug.org/events.

    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!

    What is Bayesian Analysis?

    Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

    Find more tutorials on the SAS Users YouTube channel.

    Click image to register for webinarClick image to register for webinar

    Classroom Training Available!

    Select SAS Training centers are offering in-person courses. View upcoming courses for:

    View all other training opportunities.

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