DATA Step, Macro, Functions and more

SAS Array Elements - use in any subsequent DATA step

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 9
Accepted Solution

SAS Array Elements - use in any subsequent DATA step

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


Accepted Solutions
Solution
‎07-15-2015 07:07 AM
PROC Star
Posts: 1,759

Re: SAS Array Elements - use in any subsequent DATA step

Posted in reply to IsoTropic

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(Smiley Wink

               array termdates [2]  4 _temporary_ (&dates)%str(Smiley Wink;

data TEST;

  &arrays.;

  put   termdates[1];

run;

View solution in original post


All Replies
Super User
Super User
Posts: 7,039

Re: SAS Array Elements - use in any subsequent DATA step

Posted in reply to IsoTropic

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.

Solution
‎07-15-2015 07:07 AM
PROC Star
Posts: 1,759

Re: SAS Array Elements - use in any subsequent DATA step

Posted in reply to IsoTropic

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(Smiley Wink

               array termdates [2]  4 _temporary_ (&dates)%str(Smiley Wink;

data TEST;

  &arrays.;

  put   termdates[1];

run;

Occasional Contributor
Posts: 9

Re: SAS Array Elements - use in any subsequent DATA step

Thanks.

Chris your example is excellent and to the point.

I tried your sample code and it does make the termdates available in any subsequent DATA steps as long as &array. is declared for each data step that needs to use the array references.

By using the &arrays. this must instantiate the macro variables for that DATA step.

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 267 views
  • 0 likes
  • 3 in conversation