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.
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.
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
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.
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
If you want to index your array from 0 to 52 rather than 1 to 53
array first_array(0:52) week0-week52;
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.
Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.
If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website.
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.