BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
desireatem
Pyrite | Level 9

Data A;

Imput id A;

Cards;

  1. 4

1  7

  1. 11
  2. 1

2    10

3   11

3  13

Run;

I want to create a  variable B such
that

  1. 4         4-4=0(id=1)

1    7           7-4=3

  1. 11       11-7=4
  2. 1         1-1=0(id=2)

2  10          10-1=9

3 11            11-11=0(id=3)

3  13          13-11=2(id=3)

1 ACCEPTED SOLUTION

Accepted Solutions
stat_sas
Ammonite | Level 13

data want;

set A;

by id;

B=a-lag(a);

if first.id then B=0;

run;

View solution in original post

2 REPLIES 2
stat_sas
Ammonite | Level 13

data want;

set A;

by id;

B=a-lag(a);

if first.id then B=0;

run;

Astounding
PROC Star

Not as well known, but equally effective here, is a cousin of the LAG function.  These two statements produce identical results:

B=a-lag(a);

B=dif(a);


SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 2 replies
  • 599 views
  • 1 like
  • 3 in conversation