BookmarkSubscribeRSS Feed
Danduke22
Calcite | Level 5

I have some  reference data that I've imported from a csv file.

Then using a data step 1 - I've then stored them in arrays.

I've then create a new data step 2 based around some transactions using macros but when I try to reference the arrays created in data step 1 it is not recognising them.

 

What is the best solution to overcome this scenario

 

Your help would be greatly appreciated!

4 REPLIES 4
PaigeMiller
Diamond | Level 26

Data step #2 does not know about arrays created in data step #1. You would have to define the same arrays in data step #2 in order to work with arrays in data step #2.

--
Paige Miller
Tom
Super User Tom
Super User

An ARRAY is just a convenient way to reference a series of variables using an index.  They only exist for the step where they are defined.  Any variables you create using references to elements of an array will exist but the concept of an array will not.

 

If you need the DATA generated in the first data step then include that data as input to the second data step.

 

But I suspect that you just need one data step.

Lets assume your REFERENCE dataset has 40 observations of a variable named VALUE.  And your real data, let's call it HAVE, has a variable named INDEX with values from 1 to 40 and you want to use that to lookup the the right value for REFERENCE.  In that case your single data step might look like this:

data want;
  if _n_=1 then do;
      array ref [40]  _temporary_;
      do i=1 to 40;
         set reference ;
         ref[i] = value;
      end;
  end;
  set have;
  if index in (1:40) then value=ref[index];
  else value=.;
run;
Patrick
Opal | Level 21

You would need to provide a bit more detail of how this reference data looks like and how you want to use it. 

SAS Arrays are just a way to address individual variables within a data step. They don't persist over data step boundaries. 

You can also use hash lookups (hash tables) to lookup values over a key. 

You can use a data step merge or a SQL join to add reference data to a base table.

The one thing that you can store permanently are key/value pairs in the form of a SAS format (proc format cntlin= ). Once the format has been created you can use it in any data step and also in procedures.

ballardw
Super User

Can you provide a small example of HOW you are using the reference data?

 

If the "reference" data is used to look up things, example product number to product name, price  or description then using that data to make a Format may be more flexible approach than actually combining data sets and manipulation.

 


@Danduke22 wrote:

I have some  reference data that I've imported from a csv file.

Then using a data step 1 - I've then stored them in arrays.

I've then create a new data step 2 based around some transactions using macros but when I try to reference the arrays created in data step 1 it is not recognising them.

 

What is the best solution to overcome this scenario

 

Your help would be greatly appreciated!


 

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 708 views
  • 0 likes
  • 5 in conversation