BookmarkSubscribeRSS Feed
SUDOKU
Fluorite | Level 6

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 

 

 

4 REPLIES 4
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

SUDOKU
Fluorite | Level 6

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

Reeza
Super User

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

 

array first_array(0:52) week0-week52;

ballardw
Super User

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.

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 788 views
  • 2 likes
  • 4 in conversation