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
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.
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
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.
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.
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.
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.
The actual code uses this formula:
x{i} = {i-1};
It should read:
x{i} = x{i-1};
Your error comes from:
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.
It is easy for IML.
proc iml;
use sashelp.class;
read all var {weight};
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 |
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.