DATA Step, Macro, Functions and more

reading values into array starting at index 0 instead of 1

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 81
Accepted Solution

reading values into array starting at index 0 instead of 1

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


Accepted Solutions
Solution
‎12-13-2015 03:35 PM
Super User
Posts: 17,750

Re: reading values into array starting at index 0 instead of 1

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


All Replies
Super Contributor
Posts: 275

Re: reading values into array starting at index 0 instead of 1

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.

Solution
‎12-13-2015 03:35 PM
Super User
Posts: 17,750

Re: reading values into array starting at index 0 instead of 1

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);
Respected Advisor
Posts: 4,641

Re: reading values into array starting at index 0 instead of 1

An unfortunate feature of SAS datastep arrays, indeed!

PG
Frequent Contributor
Posts: 81

Re: reading values into array starting at index 0 instead of 1

Thanks Reeza!

 

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

Super User
Super User
Posts: 7,392

Re: reading values into array starting at index 0 instead of 1

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.  

Frequent Contributor
Posts: 81

Re: reading values into array starting at index 0 instead of 1

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.

Super User
Super User
Posts: 7,392

Re: reading values into array starting at index 0 instead of 1

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;
Frequent Contributor
Posts: 81

Re: reading values into array starting at index 0 instead of 1

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.

Super User
Super User
Posts: 7,392

Re: reading values into array starting at index 0 instead of 1

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.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 9 replies
  • 831 views
  • 6 likes
  • 5 in conversation