Automatic lag1-lag100 values

Accepted Solution Solved
Reply
Super Contributor
Posts: 371
Accepted Solution

Automatic lag1-lag100 values

Hi Everyone,

I would like to create 100 lag variables for the Value Variable.

I guess there should be better way than lag1=lag1(value); lag2=lag2(value);....;

Could you please help me?

Thank you,

HHC

data have;

input value;

datalines;

1

2

3

4

5

6

7

8

9

10

11

12

13

14

;

run;


data want; set have;

  array lag{0:9} lag1-lag10;

  do i=1 to 10;

  lag=lag&i(value);

  end;

run;


Accepted Solutions
Solution
‎07-29-2015 03:31 PM
Super User
Posts: 10,476

Re: Automatic lag1-lag100 values

In a data step not really any short cut but to have lag1 lag2 etc. But a macro can help write repetitive lines of code.

%macro lagit(n=, invar=,outvar=);
/* n is the number of lags to generate, INVAR is a single variable
to base the lags and OUTVAR is the stem of an output variable
that will hold the lagged values
Example call: %lagit(n=10, invar=x, outvar=lagx)
*/
%do i= 1 %to &n;
   &&outvar&i = lag&i(&invar);
%end;
%mend;

data want;
   set have;
   %lagit(n=100, invar=value,outvar=lagv)
run;

should create 100 variables, lagv1 to lagv100

View solution in original post


All Replies
SAS Super FREQ
Posts: 3,475

Re: Automatic lag1-lag100 values

I you have access to SAS/IML, you can supply an argument to the LAG function, as follows:

proc iml;

use have; read all var "Value"; close have;

L = lag(value, 1:100);

Solution
‎07-29-2015 03:31 PM
Super User
Posts: 10,476

Re: Automatic lag1-lag100 values

In a data step not really any short cut but to have lag1 lag2 etc. But a macro can help write repetitive lines of code.

%macro lagit(n=, invar=,outvar=);
/* n is the number of lags to generate, INVAR is a single variable
to base the lags and OUTVAR is the stem of an output variable
that will hold the lagged values
Example call: %lagit(n=10, invar=x, outvar=lagx)
*/
%do i= 1 %to &n;
   &&outvar&i = lag&i(&invar);
%end;
%mend;

data want;
   set have;
   %lagit(n=100, invar=value,outvar=lagv)
run;

should create 100 variables, lagv1 to lagv100

Super User
Posts: 5,077

Re: Automatic lag1-lag100 values

I think you have the right idea.  Just a couple of considerations ...

I think you meant &outvar&i rather than &&outvar&i

You might want a starting as well as an ending number, something like:

%do i=&n_start to &n_end;

Sometimes you don't want to create every single lag.

Super User
Posts: 10,476

Re: Automatic lag1-lag100 values

You're right on both counts.

Super User
Super User
Posts: 7,392

Re: Automatic lag1-lag100 values

Hi,

data _null_;

     call execute('data want; set have; array lag{100} 8.;');

     do i=1 to 100;

          call execute(cats(' lag{',strip(put(i,best.)),'}=lag',strip(put(i,best.)),'(value);'));

     end;

     call execute('run;');

run;

Super Contributor
Posts: 371

Re: Automatic lag1-lag100 values

data have;

input value;

datalines;

1

2

3

4

5

6

7

;

run;

%macro lagit(n_start=,n_end=);

%do i= &n_start %to &n_end;

   lagvalue&i = lag&i(value);

%end;

%mend;

run;

data want;

   set have;

   %lagit(n_start=1, n_end=100)

run;

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 332 views
  • 6 likes
  • 5 in conversation