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

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

1 ACCEPTED SOLUTION

Accepted Solutions
ChrisNZ
Tourmaline | Level 20

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;

View solution in original post

3 REPLIES 3
Tom
Super User Tom
Super User

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.

ChrisNZ
Tourmaline | Level 20

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;

IsoTropic
Fluorite | Level 6

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.

sas-innovate-2024.png

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.

 

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
  • 3 replies
  • 871 views
  • 0 likes
  • 3 in conversation