## DO LOOP with lag inside

Solved
Super Contributor
Posts: 506

# DO LOOP with lag inside

[ Edited ]

Hi Everyone,

I have following issue and I would like to use the DO LOOP approach to solve it.

Thank you,

HHC

Each record[i], look Forward 5 day.
If any of the 5 day[j] satisfy the following condition:
value[j] - lag.&lagstep(value[i]) >2
then give value good=1

The lag.&lagstep(value[i]) is the lag of current record [i]

So Take Record 8 for example, SAS first check record 9.

value[j] - lag.&lagstep(value[i]) = 9 - lag3 counting from record 8 = 9-5=4 -->good

data have;
input day value lagstep;
datalines;
1 5 2
2 2 1
3 4 2
4 4 5
5 5 1
6 2 5
7 7 0
8 1 2
9 9 3
10 1 5
11 5 9
12 6 2
13 5 3
14 9 2
;
run;

data want;
set have nobs=totalobs;
drop i v;
i+1;
do j=i+1 to min(nobs,i+5);
set have (rename=(value=v))point=j nobs=nobs;

if v - lag.&lagstep(value[i]) >2 then good=1;

end;
run;

Accepted Solutions
Solution
‎10-16-2015 01:25 PM
Valued Guide
Posts: 765

## Re: DO LOOP with lag inside

Hi, another idea with no lag ...

data have;
input day value lagstep @@;
datalines;
1 5 2 2 2 1 3 4 2 4 4 5 5  5 1 6 2 5 7 7 0 8 1 2
9 9 3 10 1 5 11 5 9 12 6 2 13 5 3 14 9 2
;

data want (keep=day value  good d1-d5);
array d(5);
set have (firstobs=6);
do j=5 to 1 by -1;
rec = _n_ + 5 - j;
set have (keep=lagstep) point=rec;
d(j) = value - lagstep;
end;
good = max(of d(*)) gt 2;
run;

data set WANT (left D1 through D5 ... difference of value - lagstep for 5 previous days) ...

Obs    d1    d2    d3    d4    d5    day    value    good

1      1    -3     0     1     0      6      2        0
2      2     6     2     5     6      7      7        1
3      1    -4     0    -4    -1      8      1        0
4      7     9     4     8     4      9      9        1
5     -2    -1     1    -4     0     10      1        0
6      0     2     3     5     0     11      5        1
7     -3     1     3     4     6     12      6        1
8      3    -4     0     2     3     13      5        1
9      6     7     0     4     6     14      9        1

All Replies
Super User
Posts: 6,785

## Re: DO LOOP with lag inside

A couple of items are confusing.  Your code appears to be looking forward through the data, not backward.  And I can't see any purpose for the variable LAGSTEP.  So I'm guessing this might be what you are looking for.

data want;

set have;

if _n_ > 1 then do J=max(1, _n_-5) to _n_ - 1;

set have (keep=value rename=(value=v)) point=J;

if value - v > 2 then good = 1;  /* might have to reverse this, it is a little unclear */

end;

drop J v;

run;

If I've misinterpreted something, I'm sure we can figure it out along the way.  This should be at least close to what you are looking for.

Solution
‎10-16-2015 01:25 PM
Valued Guide
Posts: 765

## Re: DO LOOP with lag inside

Hi, another idea with no lag ...

data have;
input day value lagstep @@;
datalines;
1 5 2 2 2 1 3 4 2 4 4 5 5  5 1 6 2 5 7 7 0 8 1 2
9 9 3 10 1 5 11 5 9 12 6 2 13 5 3 14 9 2
;

data want (keep=day value  good d1-d5);
array d(5);
set have (firstobs=6);
do j=5 to 1 by -1;
rec = _n_ + 5 - j;
set have (keep=lagstep) point=rec;
d(j) = value - lagstep;
end;
good = max(of d(*)) gt 2;
run;

data set WANT (left D1 through D5 ... difference of value - lagstep for 5 previous days) ...

Obs    d1    d2    d3    d4    d5    day    value    good

1      1    -3     0     1     0      6      2        0
2      2     6     2     5     6      7      7        1
3      1    -4     0    -4    -1      8      1        0
4      7     9     4     8     4      9      9        1
5     -2    -1     1    -4     0     10      1        0
6      0     2     3     5     0     11      5        1
7     -3     1     3     4     6     12      6        1
8      3    -4     0     2     3     13      5        1
9      6     7     0     4     6     14      9        1

Super Contributor
Posts: 506

## Re: DO LOOP with lag inside

Thank you, Mike.

It works perfectly.

HHC

🔒 This topic is solved and locked.