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

So right now, sas doesn't have the ability to determine an upper bound array when using an array to create new variables.  That's what i'm hearing.

 

Thanks

ballardw
Super User

@MeganE wrote:

So right now, sas doesn't have the ability to determine an upper bound array when using an array to create new variables.  That's what i'm hearing.

 

Thanks


Not quite because what you are appently attempting to do seems not to be an array at all.

You can create an array of missing values with

 

array x (25);  Which will create variables x1 through x25.

Optionally you can supply initial values BUT you have to explicitly indicate how many variables you are creating so that SAS can set aside storage.

 

If you want to use existing variables then you have to indicate the names in one way or another

Explicit list of ALREADY EXISTING variables

array y   varname thisvar other;

or numbered list of existing variables:

array z var1 - var35;

or a list of EXISTING variables that start with similar characters

array a  ext: mnt: ; would place all variables that start with EXT or MNT into the array

or a list of contiguous variables that are in column order in a data set:

array b  varincolum5 -- anothervarincolum15;  note the two - that is the contiguous column list definer.

you can mix the approaches but all of those reference variables in the same observation (or same row ).

 

But you need to provide something to build an array with a fixed number of elements.

 

Your description, since you haven't provided even a 10 row example of data an what you are trying to actually do, sounds like you want to look at elements across rows. Which is the perview of PROC IML and the matrix language or reshaping the data so all of the values you want are on a single row. An array cannot ever in a data step look across rows.

 

Some languages include structures called a Lists or stacks that may behave as you want but those both require an entirely differentl approach to data storage.

MeganE
Pyrite | Level 9

Thanks, but, again, i do not want to use existing variables.  I am trying to create new variables.

 

I just talked to sas support, and they have confirmed that sas cannot determine the upperbound of an array that is being used to create BRAND NEW VARIABLES during the process.  It has to be figured out beforehand.

 

 

Quentin
Super User

@MeganE wrote:

 

I just talked to sas support, and they have confirmed that sas cannot determine the upperbound of an array that is being used to create BRAND NEW VARIABLES during the process.  It has to be figured out beforehand. 


 

Sounds right.  Rember that the ARRAY statement does its work at COMPILE time.  If you are using an array statement to create new variables (at compile time), you need to tell the array statement how many variables to create.

 

There have been times I've hoped there might be a syntax like:

data want;
  x1=1; x2=2; x3=3;

  array x{*} x: ;

  array y{dim(x)} ; *pseudocode;

run;

I think that is something like what you might have been hoping for?  But that doesn't work because the dim() function works at execution time.  Even if it did work at compile time, The only allowed arguments to specify the number of elements in an array are an integer or *.

 

 

Basically, I think you are forced to pre-compute the number of variables you want in the array in SAS, because there is no way for SAS to compute it.  (There are often ways to do this computation dynamically so that it isn't hardcoded.)

BASUG is hosting free webinars Next up: Mike Sale presenting Data Warehousing with SAS April 10 at noon ET. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
ballardw
Super User

Provide some data and what you are actually wanting to do with that data, as in what the data would look like AFTER what ever process you are attempting.

 

 

Tom
Super User Tom
Super User

@MeganE wrote:

So right now, sas doesn't have the ability to determine an upper bound array when using an array to create new variables.  That's what i'm hearing.

 

Thanks


That is not true at all. SAS can always determine how many variables you have specified for an array.

What you are asking for is how to generate a program that will convert an unknown number of OBERVATIONS into variables.

So yes in that limited situation SAS cannot figure out what would be an ideal upper bound for your array. You will need to tell it what upper bound to use. (or lower bound for that matter).

 

You could calculate an estimate.  Perhaps based on how many observations are in the source data.

proc sql noprint; select count(*) into :upper trimmed from have ; quit;
data want ;
   set have ;
   array start (&upper) ;
   ...
run;

Or perhaps you are doing BY variable processing so you only need enough for the largest group.

proc sql noprint; 
  select max(nobs) into :upper trimmed 
  from (select byvar,count(*) as nobs from have group by byvar)
  ; 
quit;
data want ;
  do _n_=1 by 1 until (last.byvar);
     set have ;
     by byvar;
     array start (&upper) ;
     start(_n_)= myvar ;
  end;
run;

Or you could just pick an upperbound that is larger than you expect to need.  You can even make the code smoothly deal with the sitiation where it is not large enough.  For example by just starting another output observation. 

data want ;
  do _n_=1 to 200 until (last.byvar);
    set have ;
    by byvar;
    array start (200) ;
    start(_n_)= myvar ;
  end;
run;

 

Ksharp
Super User

Then use Hash Table instead of using Array .

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
  • 21 replies
  • 6204 views
  • 0 likes
  • 8 in conversation