Help using Base SAS procedures

Array subscript out of range: processing previous column

Reply
New Contributor
Posts: 4

Array subscript out of range: processing previous column

Hi I am having a problem with processing an array but I just can't seem to get it using a simple loop. Essentially I am trying to get a week weekly count from cumulative value given for week. So for example the weekly value for week #10 I want would be Week #10 - Week#9. Using the Macro processor there is no problem with the loop however with a the do loop in the data step I get an "array subscript out of range" error. 

 

data producer_shipments13; set pcars13."Sheet1$"n;
Array Week {53} week0-week52;
Array PCarUnl {52} PCarUnl1 - PCarUnl52;
 
 
do I=1 to 52;
        lastWeek=I-1;
        PCarUnl{i} = Week{i} - Week{lastWeek};
end;
run;

 

It would appear that the error is occuring at start of the loop and if I change my loop to start at 2, and make my PCarUnl array 1 column larger I get what I need with a couple of null columns to clean up.

data producer_shipments13; set pcars13."Sheet1$"n;
Array Week {53} week0-week52;
Array PCarUnl {53} PCarUnl0 - PCarUnl52;
 
 
do I=2 to 52;
        lastWeek=I-1;
        PCarUnl{i} = Week{i} - Week{lastWeek};
end;
 
run;

 

I would have assumed this is a Program Data Vector thing for row prcessing but not so confident on how things work in arrays. Any array mavens out there who care explain or have a link that I can read through on what is happening.

 

Many Thanks,

 

Kevin 

 

 

Super User
Super User
Posts: 7,970

Re: Array subscript out of range: processing previous column

It looks like your reading an Excel file, are you sure all the variables are present and you counted them correctly?  You will find coding would be far simpler if you normalised the data, i.e. have weeks going down rather than across, this question for instance disappears as you can use aggregates or procedures on the data.

As for your question:

Array Week {53} week0-week52;
Array PCarUnl {52} PCarUnl1 - PCarUnl52;
 
 
do I=1 to 52;
        lastWeek=I-1;
        PCarUnl{i} = Week{i} - Week{lastWeek};

Now you create an array called week, with 53 elements = week{1}-week{53}.  Now in your loop, you start I at 1, and lastweek becomes 0, now week{0} is not within the 1-53 elements so its out of bounds.  Its probably that you want to do i=2-53, then the first iteration would be 2-1 for lastweek.

New Contributor
Posts: 4

Re: Array subscript out of range: processing previous column

Thanks RW9, yes the data are in excel and i could have normalised from excel using a proc transpose in SAS but I'm not in control of the design of the excel sheets. However, I am very confident the variables are correct and I am not missing columns in the array. FWIW the data were keyed into the tables by our regional staff for many years now from manual reports that are created and posted to industry. We have a data warehouse and are looking to move reporting from it but there are questions from executives and industry as to whether the reports will be different. And so it's my job to use both sources and compare.

 

Indeed I did need to extend the loop to 53 to get week 52 included and I think I understand what I need to code to get the information. 

Thanks Again,

 

Kevin

Super User
Posts: 19,822

Re: Array subscript out of range: processing previous column

If you want to index your array from 0 to 52 rather than 1 to 53

 

array first_array(0:52) week0-week52;

Super User
Posts: 11,343

Re: Array subscript out of range: processing previous column

To avoid proliferation of uneeded variables you might try instead of

lastWeek=I-1;
PCarUnl{i} = Week{i} - Week{lastWeek};

 

PCarUnl{i} = Week{i} - Week{i-1};

 

While potentially confusing, you can put functions in the index position. Pretty much anything that resolves to an integer in the subscript range is valid.

 

Ask a Question
Discussion stats
  • 4 replies
  • 142 views
  • 2 likes
  • 4 in conversation