BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
CheerfulChu
Obsidian | Level 7

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

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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.

View solution in original post

8 REPLIES 8
Jim_G
Pyrite | Level 9

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

CheerfulChu
Obsidian | Level 7

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.

 

Astounding
PROC Star

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.

CheerfulChu
Obsidian | Level 7

 

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.

 

ballardw
Super User

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.

Astounding
PROC Star

The actual code uses this formula:

 

x{i} = {i-1};

 

It should read:

 

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

ballardw
Super User

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.

Ksharp
Super User

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 8 replies
  • 2069 views
  • 2 likes
  • 5 in conversation