0
votes

I have a SAS dataset, sorted, which has two columns: PERIOD and MYMETRIC

For each row, I want to compute the growth rate of the 4 periods preceding, by using a linear regression. So the formula is basically

GROWTH RATE = Cov([MYMETRIC_lag_4,MYMETRIC_lag_3, MYMETRIC_lag_2, MYMETRIC_lag_1],[1,2,3,4])/Var([1,2,3,4])

I can do this in SAS through a proc expand to compute the lags, then a data step to compute the growth rate. I was wondering if there was a shorter way to do this? Especially if suddenly, I choose to include 8 points and not 4, I want to minimize the rework.

1

1 Answers

1
votes

You can use a data step entirely. This assumes you're asking for the four previous rows. I'm not sure what [1,2,3,4] means, though, so you'll have to fill in exactly what that means in the growth rate.

%let numlags=4;
data want;
  set have;
  array lags[&numlags] _temporary_; *temporary arrays are retained!;

  growth_rate = cov(of lags[*])/var(of lags[*]); *if you want cov of the 4 lags divided by var of the 4 lags;

  *move things about;
  do _t = 1 to dim(lags)-1;
    lags[_t]=lags[_t+1];
  end;
  lags[dim(lags)] = MYMETRIC;
run;