BookmarkSubscribeRSS Feed
Saurabh_Rana
Obsidian | Level 7

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

6 REPLIES 6
andreas_lds
Jade | Level 19

Please post the code you have, so that we can suggest improvements.

Something like

if first.b then Lag_C = .;

should help.

blueskyxyz
Lapis Lazuli | Level 10
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;
andreas_lds
Jade | Level 19

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

Sajid01
Meteorite | Level 14

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.


Tom
Super User Tom
Super User

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;
ballardw
Super User

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

CFC_SAS_Communities_400x225.jpg

Call for content now open!

It's your turn to help shape SAS Innovate 2027. Share your expertise and inspire the SAS community.

Submit your proposal →

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 7510 views
  • 0 likes
  • 6 in conversation