Solved
Contributor
Posts: 40

# How to use array for increment of lagX

Dear Expert,

I have an array but it is not working. I have this WARNING: Apparent symbolic reference I not resolved. How do I write the following program

```data XXX;

array x {3) x1 - x3;

x1 = act;
x2 = lag1(act);
x3 = lag2(act);
x4 = lag3(act);

end;

run;```

into the program below

```data XXX;

array x {3) x1 - x3;

do i = 1 to dim(x);
x{i} = lag&i.(act);
end;

run;```

Thank you so much

Accepted Solutions
Solution
‎10-17-2016 07:25 PM
Super User
Posts: 6,200

## Re: How to use array for increment of lagX

I can't sketch out the whole thing now, but you can extend the suggestion already made by using arrays.  For example:

data want;

set have;

array x {16};

retain x1-x16;

do i=16 to 2 by -1;

x{i} = x{i-1};

end;

x1 = act - ebb;

run;

Similar computations need to be added for your other variables, but this is a workable approach.

Yes, you can apply LAG to computations not just to variable names.

All Replies
Frequent Contributor
Posts: 102

## Re: How to use array for increment of lagX

Do you want 3 x values in each obs to reflect the last 3 values of act?

Data;    set;

retain  x1 x2 x3 0;

x3=x2;

x2=x1;

x1=act;

Jim

Contributor
Posts: 40

## Re: How to use array for increment of lagX

Hi Jim and astounding,

Actually the number of lags I needed is 20. Also there is computational required for numerator and denominator

```r1 = x1/a1;
...
r16 = x16 /a16;```

where

```x1 = act - ebb;
x2 = lag(act-ebb);
...
x16 = lag15(act -ebb);

a1 = alpha - beta;
...
a16 = lag15(alpha - beta);```

alpha, beta, act, ebb are numbers.

I need it to write it in a data step because the modification I need to do is within a big data step.

another question:

`lag(act - ebb) = lag(act) - lag(ebb) ?`

I want to use the 2nd observations.

Solution
‎10-17-2016 07:25 PM
Super User
Posts: 6,200

## Re: How to use array for increment of lagX

I can't sketch out the whole thing now, but you can extend the suggestion already made by using arrays.  For example:

data want;

set have;

array x {16};

retain x1-x16;

do i=16 to 2 by -1;

x{i} = x{i-1};

end;

x1 = act - ebb;

run;

Similar computations need to be added for your other variables, but this is a workable approach.

Yes, you can apply LAG to computations not just to variable names.

Contributor
Posts: 40

## Re: How to use array for increment of lagX

```data test1;
set test;
array x {16} x1-x16;
retain x1-x16;
do i = 16 to 2 by -1;
x{i} = {i-1};
end;
x1 = atq - ibq;
run;```

I encountered this error

data test1; set test; array x {16} x1-x16;retain x1-x16; do i = 16 to 2 by
8 ! -1; x{i} = {i-1}; end; x1 = atq - ibq; run;
-
22
ERROR 22-322: Syntax error, expecting one of the following: a name, INPUT,
PUT.

ERROR: Undeclared array referenced: NAME.
ERROR: Variable NAME has not been declared as an array.
NOTE: The SAS System stopped processing this step because of errors.

Super User
Posts: 12,358

## Re: How to use array for increment of lagX

Your error is referencing a variable that doesn't occur in your code. It is best to copy the code and error at one time from the log and then post using the "run" icon.

Super User
Posts: 6,200

## Re: How to use array for increment of lagX

The actual code uses this formula:

x{i} = {i-1};

x{i} = x{i-1};

Super User
Posts: 12,358

## Re: How to use array for increment of lagX

x{i} = lag&i.(act);

&i represents a MACRO variable that you have not assigned.

To reference in an array you need to have a variable.

If you need to create a bunch of lagged variables you might try this brief macro:

``````%macro LagStem (basevar=,stem=, NumLags=);
%do i = 1 %to &numLags;
stem&i = lag&i(&basevar);
%end;
%mend;``````

And use in your data step

``````data XXX;
%LagStem(basevar=Act,stem=x,NumLags=15);
array x {15) x1 - x15;
run;``````

It will be a very good Idea to call the macro immediately after any SET statement (since lag really doesn't mean much without a Set to read) and before use.

Note that it is up to you make sure to change the basevar and stem values so as not to overwrite your existing variables.

And Lag3 (x-y) isn't going to work. You will need to pull the lagged values in parallel and then calculate them in your data step.

Super User
Posts: 10,404

## Re: How to use array for increment of lagX

It is easy for IML.

``````proc iml;
use sashelp.class;
close;

lag=lag(weight,(0:4));
print lag;
quit;``````

OUTPUT:

lag
112.5 . . . .
84 112.5 . . .
98 84 112.5 . .
102.5 98 84 112.5 .
102.5 102.5 98 84 112.5
83 102.5 102.5 98 84
84.5 83 102.5 102.5 98
112.5 84.5 83 102.5 102.5
84 112.5 84.5 83 102.5
99.5 84 112.5 84.5 83
50.5 99.5 84 112.5 84.5
90 50.5 99.5 84 112.5
77 90 50.5 99.5 84
112 77 90 50.5 99.5
150 112 77 90 50.5
128 150 112 77 90
133 128 150 112 77
85 133 128 150 112
112 85 133 128 150
☑ This topic is solved.