## Create Lag.variable1(variable2)

Solved
Super Contributor
Posts: 506

# Create Lag.variable1(variable2)

Hi Everyone,

I want to create lag with step of lag is the value of a variable.

In the data below, New_var=lag of a1 step (for variable a2).

So:

for the record 2, the New_var=.

for the record 3, the New_var=9 (lag2)

for the record 4, the New_var=9 (lag3)

for the record 5, the New_var=3 (lag1)

Could you help me do it?

Thank you,

HHC

data have;

input a0 a1 a2 ;

datalines;

0 2 9

1 2 0

1 2 0

1 3 3

5 1 9

;;

Accepted Solutions
Solution
‎07-25-2014 10:58 PM
Contributor
Posts: 35

## Re: Create Lag.variable1(variable2)

If you know the maximum value for a1, and it is reasonably low, then you can solve this with the lag function and an array.  E.g. assuming the maximum value of a1 is 5, try:

data want;

set have;

array lagged {5};

lagged1 = lag1(a2);

lagged2 = lag2(a2);

lagged3 = lag3(a2);

lagged4 = lag4(a2);

lagged5 = lag5(a2);

New_var = lagged{a1};

drop lagged: ;

run;

Note that the lag offset is NOT an argument to the lag function - as the title of this thread implies.

Alternatively, the following untested code should work even when the maximum value of a1 is unknown:

data want;

set have;

if _n_ gt a1 and a1 ne 0 then do;

_lagobs_ = _n_-a1;

set have(keep=a2 rename=(a2=New_var)) point=_lagobs_;

end;

run;

All Replies
Solution
‎07-25-2014 10:58 PM
Contributor
Posts: 35

## Re: Create Lag.variable1(variable2)

If you know the maximum value for a1, and it is reasonably low, then you can solve this with the lag function and an array.  E.g. assuming the maximum value of a1 is 5, try:

data want;

set have;

array lagged {5};

lagged1 = lag1(a2);

lagged2 = lag2(a2);

lagged3 = lag3(a2);

lagged4 = lag4(a2);

lagged5 = lag5(a2);

New_var = lagged{a1};

drop lagged: ;

run;

Note that the lag offset is NOT an argument to the lag function - as the title of this thread implies.

Alternatively, the following untested code should work even when the maximum value of a1 is unknown:

data want;

set have;

if _n_ gt a1 and a1 ne 0 then do;

_lagobs_ = _n_-a1;

set have(keep=a2 rename=(a2=New_var)) point=_lagobs_;

end;

run;

Posts: 4,736

## Re: Create Lag.variable1(variable2)

Below tested code should do:

data have;

input a0 a1 a2;

datalines;

0 2 9

1 2 0

1 2 0

1 3 3

5 1 9

;

run;

data want;

set have nobs=_nobs;

_get_obs=_n_-a1;

if 0<_get_obs<=_nobs then

do;

set have (keep=a2 rename=(a2=New_Var)) point=_get_obs;

end;

else call missing(New_Var);

run;

🔒 This topic is solved and locked.