Combining Lags and Arrays
[and a little macro]
Lynn Lethbridge
SHRUG OCT 28, 2011
Lag Function
A tool to manipulate data across
observations
Can lag back as far as you like
(default is a lag of 1)
Can be frustrating!
Don’t put a lag in a conditional
statement; create a variable with the
lag value first and use the variable in
the conditional statement
Example 1a
data work.temp;
input x sex $;
lx=lag(x);
if sex='M' then z=lx;
drop lx;
cards;
1M
1M
3F
5M
5F
8F
8F
8F
9F
10 M
;
run;
Obs x sex z
1 1 M .
2 1 M 1
3 3 F .
4 5 M 3
5 5 F .
6 8 F .
7 8 F .
8 8 F .
9 9 F .
10 10 M 9
Example 1b
data work.temp;
input x sex $;
if sex='M' then z=lag(x);
cards;
1M
1M
3F
5M
5F
8F
8F
8F
9F
10 M
;
run;
Obs
1
2
3
4
5
6
7
8
9
10
x
1
1
3
5
5
8
8
8
9
10
sex
M
M
F
M
F
F
F
F
F
M
z
.
1
.
1
.
.
.
.
.
5
Arrays
Allows you to assign a single name
to multiple variables
Can use an array to manipulate
data across variables without
having to write them out multiple
times
Can create new variables in arrays
data work.temp;
input id sex $ a b c;
array oldvars {*} a b c;
array newvars {*} var1-var3;
do i=1 to dim(oldvars);
if sex='f' then newvars{i}=oldvars{i};
end;
cards;
1 m 1
1 m 2
3 m 6
3 f 7
4 m 8
4 f 1
5 f 4
6 m 3
;
run;
drop i;
3
2
2
1
4
4
9
8
4
4
6
5
7
8
5
9
Obs
1
2
3
4
5
6
7
8
id
sex
1
1
3
3
4
4
5
6
m
m
m
f
m
f
f
m
a
1
2
6
7
8
1
4
3
b
3
2
2
1
4
4
9
8
c
4
4
6
5
7
8
5
9
var1
.
.
.
7
.
1
4
.
var2
var3
.
.
.
.
.
.
1
.
4
9
.
.
.
5
8
5
Lags and Arrays
Lags allow you to work across
observations
Arrays allow you to work across
variables
Combining the two lets you work
efficiently with your data as a
matrix
Combined Example
Suppose you have multiple
observations per ID
Ordered first by ID and then by
date
You want to know if a code appears
twice in a row for any individual ID
across multiple variables
Data
data work.temp;
input id date a $ b $ c $;
cards;
1 12951 a1 b2 g5
1 12999 a4 a1 v4
1 13777 s3 f5
g5
3 11111 a1 a1 g5
3 12222 h6 i8
t5
3 13333 b2 f4
f4
3 14444 d4 f4
s3
4 10000 c1 g4 d4
4 11111 f4 d4
s3
5
9900 d1 s2
w2
5 10000 a1 s2
d1
5 13456 a1 a2
a1
5 14566 f4 f3
f4
5 15999 d1 a1
a1
;
run;
Lags and Arrays
data work.temp2;
set work.temp;
lagid=lag(id);
array vars {*} a b c;
array newvars {*} $ na nb nc;
do i=1 to 3;
lagvars=lag(vars{i});
if id=lagid and lagvars=vars{i}
then newvars{i}=vars{i};
end;
run;
drop lagid i lagvars;
Printed Output
Obs
id
1
2
3
4
5
6
7
8
9
10
11
12
13
14
1
1
1
3
3
3
3
4
4
5
5
5
5
5
date
a
17JUN95
a1
04AUG95 a4
20SEP97
s3
03JUN90
a1
18JUN93
h6
03JUL96
b2
19JUL99
d4
19MAY87 c1
03JUN90
f4
08FEB87 d1
19MAY87 a1
03NOV96 a1
18NOV99 f4
21OCT03 d1
b
c
b2
a1
f5
a1
i8
f4
f4
g4
d4
s2
s2
a2
f3
a1
g5
v4
g5
g5
t5
f4
s3
d4
s3
w2
d1
a1
f4
a1
na
nb
f4
a1
s2
nc
Higher Order Lag
data work.temp;
input id a ;
la3=lag3(a);
cards;
1 1
1 2
3 3
3 4
4 5
4 6
5 7
6 8
;
run;
Obs
1
2
3
4
5
6
7
8
a la3
1
.
2
.
3
.
4
1
5
2
6
3
7
4
8
5
Higher Order Lag with a Macro
%let num=3;
data work.temp;
input id a ;
la&num=lag&num(a);
cards;
1 1
1 2
3 3
3 4
4 5
4 6
5 7
6 8
;
run;
Obs
1
2
3
4
5
6
7
8
a la3
1
.
2
.
3
.
4
1
5
2
6
3
7
4
8
5
Thank you for your attention!
© Copyright 2026 Paperzz