Calcite | Level 5

## Calculate the difference of one variable between current year and last year.

Hi Experts,

Now I have the annual data for one variable ("total_pps") for each company(labeled as "GVKEY"), and I'd like to calculate the difference between current year's "pps" and the pps for the last year for each company and make it as current year's difference. For example, for gvkey=001045, the difference between pps in 1993 and pps in 1992 should be the "diff_pps" for 1993 which will become a new variable. How to write the codes?

Thanks a lot!!! And I attach a screencut of my data.

1 ACCEPTED SOLUTION

Accepted Solutions
Tourmaline | Level 20

## Re: Calculate the difference of one variable between current year and last year.

``````
data have;
input Year      GVKEY   \$          total_pps       ;
cards;
1992      001045            50                  *
1993       001045           56                  6
1994       001045           99                  43
1995       001045           69                  -30
1996       001045           78                  9
1997       001045            88                 10
;

data want;
set have;
by gvkey year;
dif=dif(total_pps);
if first.gvkey then call missing(dif);
run;
``````
5 REPLIES 5
Tourmaline | Level 20

## Re: Calculate the difference of one variable between current year and last year.

Can you paste the date as text rather than pics plz . Also post a sample of your expected output plz

Calcite | Level 5

## Re: Calculate the difference of one variable between current year and last year.

Sure. The attached is my expected output sample. Thank you!

Tourmaline | Level 20

## Re: Calculate the difference of one variable between current year and last year.

``````
data have;
input Year      GVKEY   \$          total_pps       ;
cards;
1992      001045            50                  *
1993       001045           56                  6
1994       001045           99                  43
1995       001045           69                  -30
1996       001045           78                  9
1997       001045            88                 10
;

data want;
set have;
by gvkey year;
dif=dif(total_pps);
if first.gvkey then call missing(dif);
run;
``````
Calcite | Level 5

## Re: Calculate the difference of one variable between current year and last year.

Thanks a lot!! It works well!

Quartz | Level 8