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

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 4 replies
  • 387 views
  • 0 likes
  • 5 in conversation