user14419016
user14419016

Reputation: 33

SAS expand lag1() to some columns of a dataset

I would like to apply the function lag1() to some columns of a dataset.

I have a dataset with n columns:

Date         Col1   Col2   Col3   ...  Coln
01/01/2000    10     5      5     ...   3
01/02/2000     7    10      5     ...   1
01/03/2000     9     2      8     ...   1
01/04/2000     3     5      4     ...   8

I would like to get:

Date         Col1   Col2   Col3   ...  Coln
01/01/2000     .     .      .     ...   .
01/02/2000    -3     5      0     ...  -2
01/03/2000     2    -8      3     ...   0
01/04/2000    -6     3     -4     ...   7

I did it one by one using this command:

data test_tab;
   set mydata;
   test=Col2-lag1(col2);
run;

but I would like to get it for all columns but the first, given that there are some hundreds of columns. What do you suggest? Thank you!

Upvotes: 1

Views: 97

Answers (2)

Richard
Richard

Reputation: 27508

Specify a variable based array for the variables you want to 'differate' and loop over the elements computing the lagged difference with the DIF function.

Example:

data want;
  set have;
  array x x2-x10 x20-x89 x91-x100;
  do _i_ = 1 to dim(x);
    x(_i_) = dif(x(_i_));
  end;
run;

Upvotes: 2

Stu Sztukowski
Stu Sztukowski

Reputation: 12909

Assuming your data is as nicely formatted as you posted, you can do this with some variable selection shortcuts and arrays. The shortcut col2--coln will select all variables between col2 and coln inclusive.

data test_ab;
    set mydata;
    array var[*] col2--coln;

    do i = 1 to dim(var);
        var[i] = var[i] - lag(var[i]);
    end;

    drop i;
run;

Upvotes: 1

Related Questions