Calcite | Level 5

## 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?

1 ACCEPTED SOLUTION

Accepted Solutions
Opal | Level 21

## 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.

4 REPLIES 4
Opal | Level 21

## 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;

Calcite | Level 5

## 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.

Opal | Level 21

## 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.

Super User

## 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.

The Boston Area SAS Users Group is hosting free webinars!
Next up: Bart Jablonski and I present 53 (+3) ways to do a table lookup on Wednesday Sep 18.
Register now at https://www.basug.org/events.
Discussion stats
• 4 replies
• 3855 views
• 4 likes
• 3 in conversation