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

I'm having problems reading macro variables into an array that starts at index 0.

 

Here's a much simplified example:

 

%let a0=0; %let a1=1; %let a2=2; %let a3=3;
data test (keep= a_0-a_3);
	array a_(0:3) (&a0 &a1 &a2 &a3);
	output test;
run;

 

When I run this code I receive the following error message in the log:

 

WARNING: Not all variables in the list a_0-a_3 were found.
NOTE: The data set WORK.TEST has 1 observations and 3 variables.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds

 

and the values in the test dataset have been offset and are incomplete:

Obs    a_1    a_2    a_3

     1       0        1        2

 

I know SAS can handle arrays with indexes beginning at 0 rather than 1. What am I doing wrong here?

 

Thanks! in advance

 

Robert

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
If you want to rename variables you may need to explicitly list the variable names unfortunately.

array a_(0:3) a_0-a_3 (&a0 &a1 &a2 &a3);

View solution in original post

9 REPLIES 9
slchen
Lapis Lazuli | Level 10

Although SAS array index could start from 0, array variable of elements should be count form 1, result should be a1-a4, but you keep a0-a3, not include a4, keep a0 which does not exist.

Reeza
Super User
If you want to rename variables you may need to explicitly list the variable names unfortunately.

array a_(0:3) a_0-a_3 (&a0 &a1 &a2 &a3);
PGStats
Opal | Level 21

An unfortunate feature of SAS datastep arrays, indeed!

PG
RobF
Quartz | Level 8

Thanks Reeza!

 

I had a feeling there was a simple solution I'd overlooked.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Why do you need an array which starts at 0?  The array strcuture is specifically designed for array[1-n].  What your effectively saying is that you want a zero element which doesn't make sense, even behind the scenes if you force SAS to use zero it will internally create 1-n and have a pointer.  

RobF
Quartz | Level 8

Right, normally I'm happy with the array index starting at 1.

 

In this case I'm refining a program that iteratively calculates regression coefficients p0, p1, p2, ..., p&numvar, and for convenience's sake I'd like the coefficient array to start at 0 (the intercept). One less thing to keep track of in my code if I don't have to subtract one from the array index when I'm printing out the final array values.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Yes, but then you are mixing up the forumla logic with the underlying code program. p0 is still the first element in the array, whatever you call it.  Personally I would normalise the data, i.e. have a row for each result - thats CDISC model structure, and transpose only when needed for outputs.  However if you fixed on arrays, then maybe a two dimensional array would work:

data want;
  array p_results{4,2} (0,0.1234,
                        1,0.34567,
                        2,0.8765,
                        3,0.2309); 
  do i=1 to 4;
    put "P=" p_results{i,1}  "Result=" p_results{i,2};
  end;
run;
RobF
Quartz | Level 8

Thanks RW9, that's an interesting solution.

 

My code (which performs a lasso/ridge regression) reads the analysis dataset into an array & processes the data in a data _null_ data step to save processing time. Getting it to work was a minor miracle, but there are still a few bugs in my array code & I'm open to suggestions.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Well, I am not familiar with the regression, however I would point out that almost all functions work with by grouping, i.e. having some variables as ids and then some as values to be analysed.  E.g:

proc means data=have;

  class group1 group2;

...

run;

 

This type of processing will be the fastest (I assume) as the grouping is done within the compiled function rather than interpreted from your code.  Maybe if you post a sample + some test data could give an example.

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
  • 9 replies
  • 6628 views
  • 6 likes
  • 5 in conversation