Hello
I am trying to create new variables using array.
When I create X12 variables: minus1,minus2.....minus12 then it works 100%.
When I create X12 variables: minus0,minus2.....minus12 then I get an error.
MAy anyone help to understand why is the error and how to solve it please?
data ttt1;
INPUT ID month_YYMM;
cards;
111 1407
222 1708
333 1901
444 2206
555 1909
;
run;
Data ttt2(drop=i);
SET ttt1;
month_date=input( put(month_YYMM,4.),yymmn4.);
format month_date date9.;
array AAA(12) minus1 minus2 minus3 minus4 minus5 minus6 minus7 minus8 minus9 minus10 minus11 minus12;
do i=1 to 12;
AAA(i)=PUT(INTNX('month',month_date,-i),YYMMN4.);
end;
RUN;
Data ttt3(drop=i);
SET ttt1;
month_date=input( put(month_YYMM,4.),yymmn4.);
format month_date date9.;
array AAA(12) minus0 minus1 minus2 minus3 minus4 minus5 minus6 minus7 minus8 minus9 minus10 minus11;
do i=0 to 11;
AAA(i)=PUT(INTNX('month',month_date,-i),YYMMN4.);
end;
RUN;
/*NOTE: Character values have been converted to numeric values at the places given by: (Line):(Column).*/
/* 33:1 */
/*ERROR: Array subscript out of range at line 33 column 1.*/
/*ID=111 month_YYMM=1407 month_date=01JUL2014 minus0=. minus1=. minus2=. minus3=. minus4=. minus5=. minus6=. minus7=. minus8=.*/
/*minus9=. minus10=. minus11=. i=0 _ERROR_=1 _N_=1*/
/*NOTE: The SAS System stopped processing this step because of errors.*/
/*NOTE: There were 1 observations read from the data set WORK.TTT1.*/
/*WARNING: The data set WORK.TTT3 may be incomplete. When this step was stopped there were 0 observations and 15 variables.*/
/*NOTE: DATA statement used (Total process time):*/
As the log indicates, the problem here is the array indexes.
So change
array AAA(12)
to
array AAA(0 : 11)
in the last step, and you're all good 🙂
As the log indicates, the problem here is the array indexes.
So change
array AAA(12)
to
array AAA(0 : 11)
in the last step, and you're all good 🙂
Thanks,
May you please explain why array AAA(12) is not working with do i=0 to 11 ?
Sure thing. It is because
array AAA(12)
is equivalent to
array AAA(1 : 12)
which means that you create an array with indices 1 to 12. When you loop from i = 0 to 11, you try to retrieve AAA[0] on the first iteration. Which does not exist. Therefore you get an error in the log.
To bring things to a point from the post:
If the variables don't exist consider:
Array minus(12);
That will create variables minus1 to minus12.
Or at least enumerated lists:
Array AAA (*) minus1- minus12;
Note your code already has a flaw related to variable types. The array as stated is numeric. Then you attempt to place character values into it. Not a good idea. If you want character values create the array as character.
array AAA(12) minus0 minus1 minus2 minus3 minus4 minus5 minus6 minus7 minus8 minus9 minus10 minus11;
do i=0 to 11;
AAA(i)=PUT(INTNX('month',month_date,-i),YYMMN4.);
end;
If you use use:
Array AAA(*) minus0-minus11;
You can sequentially access them just fine using
Do i=1 to dim(AAA);
I strongly suggest learning to use the DIM() function that returns the number of elements defined for an array. That way when you modify the program to use minus1 to minus25 the only change needed is likely to be the array definition itself. The dim function takes care of handling all 25 elements. Otherwise you may have to search and find occurrences of specific numbers.
AND just why are you persisting in creating character values that are partial dates to begin with. Create the values as dates and assign the format.
If you have another variable that will be used to index the array and you need 0 then define the array to use the definition as @PeterClemmensen provided.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.