Laurie Blome
Laurie Blome

Reputation: 41

Spss syntax to compute % change from same date last year

I am trying to figure out the spss syntax for computing the following: I have revenue per date, and want to create a new variable containing the percent change in that revenue compared to the same date during the prior year. I have searched on how spss deals with dates, but have not found a function to do this. Thanks for any help!!

Upvotes: 1

Views: 854

Answers (1)

Andy W
Andy W

Reputation: 5089

If you know you have every date in the file you can use SHIFT VALUES and then have a condition if the year is a leap year. Another way I will show though doesn't need every date in the file. Basically what I do here is to make a new data file, calculate the year ahead, and then merge back into the original file.

*Make some fake data.
DATA LIST FREE / Date (ADATE8) Val (F1.0).
BEGIN DATA
5/11/13 1
5/12/13 2
5/11/14 3
5/12/14 4
END DATA.
DATASET NAME Orig.

*Copy the dataset and add a year.
DATASET COPY Lag.
DATASET ACTIVATE Lag.
*Add 1 year to the date.
COMPUTE Date = DATESUM(Date,1,"YEAR").
*Remerge back into original data file.
DATASET ACTIVATE Orig.
MATCH FILES FILE = *
  /TABLE = 'Lag'
  /RENAME (Val = YearLagVal)
  /BY Date.
DATASET CLOSE Lag.

This can be made into a pretty simple macro to avoid the need to repetitively type this.

*Macro to do this.
DEFINE !YearLag (Data = !TOKENS(1)
                /Date = !TOKENS(1)
                /Var = !TOKENS(1) 
                /NewVar = !TOKENS(1) )
DATASET COPY XLagX.
DATASET ACTIVATE XLagX.
MATCH FILES FILE = * /KEEP !Date !Var.
COMPUTE !Date = DATESUM(!Date,1,"YEAR").
DATASET ACTIVATE !Data.
MATCH FILES FILE = *
  /TABLE = 'XLagX'
  /RENAME (!Var = !NewVar)
  /BY !Date.
DATASET CLOSE XLagX.
!ENDDEFINE.

And now the function call is simply:

!YearLag Data = Orig Date = Date Var = Val NewVar = ValLagYear2.

It would take just alittle more work to expand it so it could take a list of old variable names and new variable names (e.g. if you wanted to lag multiple variables at once).

Upvotes: 0

Related Questions