Computing third variable using id and another variable

Accepted Solution Solved
Reply
Super Contributor
Posts: 292
Accepted Solution

Computing third variable using id and another variable

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)


Accepted Solutions
Solution
‎11-10-2014 01:27 PM
Trusted Advisor
Posts: 1,203

Re: Computing third variable using id and another variable

data want;

set A;

by id;

B=a-lag(a);

if first.id then B=0;

run;

View solution in original post


All Replies
Solution
‎11-10-2014 01:27 PM
Trusted Advisor
Posts: 1,203

Re: Computing third variable using id and another variable

data want;

set A;

by id;

B=a-lag(a);

if first.id then B=0;

run;

Respected Advisor
Posts: 4,993

Re: Computing third variable using id and another variable

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);


☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 185 views
  • 1 like
  • 3 in conversation