I want the following output by using the lag function using multiple by variables.
A B C Lag(C)
a d 1 .
a d 2 1
a e 3 .
b e 4 .
b e 5 4
c e 6 .
c f 7 .
c f 8 7
But the output I am getting is this-
A B C Lag(C)
a d 1 .
a d 2 1
a e 3 2
b e 4 3
b e 5 4
c e 6 5
c f 7 6
c f 8 7
Please post the code you have, so that we can suggest improvements.
Something like
if first.b then Lag_C = .;
should help.
data have;
length a $1 b$1;
input a b c;
cards;
a d 1
a d 2
a e 3
b e 4
b e 5
c e 6
c f 7
c f 8
;
proc sort; by a b c;
run;
data want;
set have;
by a b c;
c_=lag(c);
run;
@blueskyxyz wrote:
data have; length a $1 b$1; input a b c; cards; a d 1 a d 2 a e 3 b e 4 b e 5 c e 6 c f 7 c f 8 ; proc sort; by a b c; run; data want; set have; by a b c; c_=lag(c); run;
This will not create the required dataset, but the one @Saurabh_Rana is getting now. Also: why is variable C in the the by-statement in proc sort? This seems to be wrong.
Hello @Saurabh_Rana
The code is doing the way it is written.
You want the Lag(C) values for selected rows,
You need to define a criteria and add a filter for it in your code.
Why are those other values supposed to be missing?
Is it because you want to treat A and B as grouping variables?
data want;
set have;
by a b ;
lag_c=lag(c);
if first.b then lag_c=.;
run;
@Saurabh_Rana wrote:
I want the following output by using the lag function using multiple by variables.
A B C Lag(C)
a d 1 .
a d 2 1
a e 3 .
b e 4 .
b e 5 4
c e 6 .
c f 7 .
c f 8 7
But the output I am getting is this-
A B C Lag(C)
a d 1 .
a d 2 1
a e 3 2
b e 4 3
b e 5 4
c e 6 5
c f 7 6
c f 8 7
So, what is the INPUT?
Which are the BY variables?
What are the assignment rules?
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.