Hi guys, I am a SAS starter.I just want to build new variables with the do loop, here is my code:
data pnm_sur; set part3.pnm_sur;
fy1date=cv3date+365.25;
array fydate(13) fy1date--fy13date;
do i=1 to 13;
fydate(i+1)=fydate(i)+365.25;
end;
run;
Errors happens " ERROR: fy13date does not follow fy1date on the list of previously defined variables.
ERROR: Too few variables defined for the dimension(s) specified for the array fydate."
I don't know how to figure it out and I want your help. Thanks a lot!
Your variables fy1date--fy13date are not consecutive, with fy13date coming somewhere after fy1date. Or maybe there aren't 13 variables in this variable list. You need to look at the data set part3.pnm_sur with your own eyes and see what is in there in terms of these variables. You probably will have to specify the array differently, depending on what you see in part3.pnm_sur.
Your variables fy1date--fy13date are not consecutive, with fy13date coming somewhere after fy1date. Or maybe there aren't 13 variables in this variable list. You need to look at the data set part3.pnm_sur with your own eyes and see what is in there in terms of these variables. You probably will have to specify the array differently, depending on what you see in part3.pnm_sur.
Hi PaigeMiller,
Thanks a lot for your quick reply!
I see my problem. I thought "array" may develop new variables, actually it cannot.
I develop fy1date to fy13date in the former step and it works!
Thank you!
If you want to create the variables in the array in data set pnm_sur, you need
array fydate(13) fydate1-fydate13;
Note the single dash and the fact that the number of the variable is at the end. This is a valid list of 13 variables named consecutively, and in the ARRAY statement creates these 13 variables if they don't already exist.
New variables will be created if they are first mentioned in an ARRAY statement.
Your problem is that position based variable list can only work with variables that already exist. By definition. A variable cannot have a position if it does not exist yet.
Another issue with your code.
do i=1 to 13; fydate(i+1)=fydate(i)+365.25; end;
When i=13 then you reference a 14th item in the array: fydate(i+1) is fydate(14). You define the array explicitly with 13 elements so the attempt to use a 14th element will generate an out of bounds error.
Another concern when I see variables with DATE names and then fractional values is that perhaps you want to reconsider the initial values and what you mean by "+365.25".
If you have an actual SAS date value the function INTNX will increment values by given intervals and account for things like leap days without having to deal with fractional days. If you have date value then
newdatevar = intnx('year',datevar,1,'S') will return the same day of the next year for example.
And proper date values allow use of SAS date formats to create analysis groups by changing formats without having to create additional variables.
https://communities.sas.com/t5/SAS-Communities-Library/Working-with-Dates-and-Times-in-SAS-Tutorial/... has a PDF with much information about dates.
I changed my code:
data pnm_sur;set part3.pnm_sur;
array fydate (14) fydate1-fydate14;
fydate1= intnx('year',cv3date,1,'S');
do i=1 to 13;
fydate(i+1)=intnx('year',fydate(i),1,'S');
end;
do i=1 to 14;
format fydate(i) MMDDYY10.;
end;
run;
But there still some errors with the format: ERROR 85-322: Expecting a format name. It did not consider fydate(i) as a format name?
This statement is not valid syntax:
format fydate(i) MMDDYY10.;
The format statement needs actual variable names.
format fydate1-fydate14 mmddyy10. ;
You seem to have over complicated things.
do i=1 to dim(fydate);
fydate(i)=intnx('year',cv3date,i,'S');
end;
It works when the code change to
data pnm_sur;set part3.pnm_sur;
array fydate (14) fydate1-fydate14;
fydate1= intnx('year',cv3date,1,'S');
do i=1 to 13;
fydate(i+1)=intnx('year',fydate(i),1,'S');
end;
format fydate1--fydate14 MMDDYY10.;
run;
So do loop cannot be used in format? or the reason is something else
Doesn't make any sense to put a FORMAT statement inside a DO loop. It is not executed, it just sets the metadata of the variable(s) listed.
@Yaqun wrote:
So do loop cannot be used in format? or the reason is something else
Unfortunately, I don't understand this. What problem are you having? Please be specific.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.