Contributor
Posts: 43

# subtract the observations with in variable

Hi i need some help to find how to subtract with in the variable. here is the sample data

name x1 x2 x3
wk1 15 35 18
wk2 36 48 29
wk3 47 56 68
wk4 47 58 91
wk5 56 79 125
wk6 84 96 175
.
.
.
.

i would like to get the out like in the a1 ( wk2.x1-wk1.x1...) a2 ( wk2.x2-wk1.x2) a3 ( wk2.x3-wk1.x3)

name x1 x2 x3 a1 a2 a3
wk1 15 35 18 15 35 18
wk2 36 48 29 21(36-15) 13 (48-35) 11(29-18)
wk3 47 56 68 16 (47-36) 8 (56-48) 39 (68-29)
wk4 47 58 91 0 (47-47) 2 (58-56) 23 (91-68)
wk5 56 79 125
wk6 84 96 175
.
.
.
.

sams
Super Contributor
Posts: 3,176

## Re: subtract the observations with in variable

Investigate using the DATA step and the LAG function, along with a sorted file and the BY statement so you can test IF FIRST.NAME THEN DO; END; in your DATA step and doing the subtraction correctly on all but the first observation in a BY group.

Scott Barry
SBBWorks, Inc.
Contributor
Posts: 43

## Re: subtract the observations with in variable

Hi SBB,
Thanks for your help. I found the answer. I just used the dif function.

data cctest1;
set cctest;
tix06=dif(tix_sold_in_06);
tix07=dif(tix_sold_in_07);
tix08=dif(tix_sold_in_08);
if tix06 eq . then tix06=tix_sold_in_06;
if tix07 eq . then tix07=tix_sold_in_07;
if tix08 eq . then tix08=tix_sold_in_08;
run;