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?

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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