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 | ||
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?
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.
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
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;
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.
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.
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
HTH
--Q.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.