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.
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.