I am working to clean-up some previous SAS hacker's copy/paste technique. These SAS routines have several dozen DATA steps.
At the beginning of the routine, I would like to read a set of Term Codes and Dates into an array and then reference that anywhere in the program.
Currently, the ARRAY statement and reference works only within the SAME Data Step.
Such that TermCode[1] = 2013-1, TermCode[2] = 2013-2, TermCode[3] = 2013-3...........etc
and TermDate[1]= 19183, TermDate[2]=19274 ...........etc (SAS DATES).
So later in the program (about 20 data steps), I can say for
TCidx = 2
ChkDate = TermDate[TCidx];
now I just get ERROR: Undeclared array referenced: TermDate.
Any suggestions on creating GLOBAL variable that is an array.
Thanks
As Tom said, arrays are not global. Macro variable can be.
Just call the array declaration as needed:
%let codes = '2003-01' '2003-02';
%let dates = 19183 19274 ;
%let arrays = array termcodes [2] $8 _temporary_ (&codes)%str(;)
array termdates [2] 4 _temporary_ (&dates)%str(;);
data TEST;
&arrays.;
put termdates[1];
run;
The ARRAY statement is a data step statement that just creates an alias that you can use during that data step to make it easier to access a set of variables. It does not make anything permanent that will survive from one data step to another.
If you have rational variable names the ARRAY statements are not hard to write.
data step2;
set step1;
array TermCode TermCode: ;
array TermDate TermDate: ;
...
run;
You could store the list of variables or the ARRAY statement itself into a macro variable to make it easier to replicate in many places.
As Tom said, arrays are not global. Macro variable can be.
Just call the array declaration as needed:
%let codes = '2003-01' '2003-02';
%let dates = 19183 19274 ;
%let arrays = array termcodes [2] $8 _temporary_ (&codes)%str(;)
array termdates [2] 4 _temporary_ (&dates)%str(;);
data TEST;
&arrays.;
put termdates[1];
run;
Thanks.
Chris your example is excellent and to the point.
I tried your sample code and it does make the termdates
By using the &arrays. this must instantiate the macro variables for that DATA step.
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.