BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Yoko
Obsidian | Level 7

Hello, 

 

I try to write a program to find out age at July 1 of each year specified.  

I am having a problem to find out what is wroing with my program.

 

==============================================

data new_data;
set old_data;
array age(5) age06-age15;
%let sYr=2006;
%let eYr=2015;
do year =&sYr to &eYr;
%let sDate="1Jul&year"d;

 

age(year) = floor((intck('month',dob,&sDate)-(1<day(dob)))/12);
end;
run;

===================================================

 

I got warnign and error messages:

WARNING: Apparent symbolic reference YEAR not resolved.
ERROR: Invalid date/time/datetime constant "1Jul&year"d.

 

I can tell something is wroing especially with 'sDate'.  

 

Could anybody tell me why I got these messages and how I can fix this please? 

 

Thank you, 

 

YI

 

 

 

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

@Yoko wrote:
Suggested program==========================================

data new_data;
set old_data;
array age(10) age06-age15;
do year =2006 to 2015;
age(year) = floor((intck('month',dob,mdy(7, 1, year))-(1
OUTPUT;
end;
run;

error messages ===============================================
ERROR: Array subscript out of range at line 137 column 2.


When define an array as you did the array index expects values of 1 to 10; age(1) for example. If you want to use actual values of year such as 2006 then you need to define the array boundary limits:

data junk;
   array a (2006:2015) age06-age15;
   do year=2006 to 2015;
      a(year) = <calculation>;
   end;
run;

 

 

View solution in original post

10 REPLIES 10
Reeza
Super User

DO NOT USE A MACRO.

 

There's no need for a macro here, use data step variables and get that working first, and then you can add a macro loop after, if required.

 

Since you're trying to add records, you need an OUTPUT statement inside your do loop to add the new rows.

 

data new_data;
set old_data;
array age(5) age06-age15;


do year =2006 to 2015;

 
age(year) = floor((intck('month',dob,mdy(7, 1, year))-(1<day(dob)))/12);

OUTPUT;

end;
run;
Yoko
Obsidian | Level 7
Hello,



Thank you for your reply.

I tried your suggestion.

But, I still have this error message:



ERROR: Array subscript out of range at line 137 column 2.



Sorry, but I'm new to SAS and not sure what this means.

Would you have any other suggestions?



Thank you,



Yoko




HB
Barite | Level 11 HB
Barite | Level 11

I think the array out of subscript error is becasue you are putting ten years (2006 to 2015) into a 5 position array.  Try array age(10).

 

 

Yoko
Obsidian | Level 7

Yes, that was my error.  I have 10 in that place now. 

 

Thakn you!

 

Yoko

novinosrin
Tourmaline | Level 20

Your code suggests understanding of the entire macro compliation and execution is wrong particularly when dealing with timing. Have you tried without macro in the first place?

Yoko
Obsidian | Level 7
Hello,



Just before your message, I received a message from somebody.

She suggests that I do not use macro too.



I tried her suggestion, but I still got an error message (different one).



Suggested program==========================================



data new_data;
set old_data;
array age(10) age06-age15;


do year =2006 to 2015;


age(year) = floor((intck('month',dob,mdy(7, 1, year))-(1
OUTPUT;

end;
run;





error messages ===============================================

ERROR: Array subscript out of range at line 137 column 2.





I already asked her, but if you have any other suggestions, I appreciate it.




Thank you,



Yoko


ballardw
Super User

@Yoko wrote:
Suggested program==========================================

data new_data;
set old_data;
array age(10) age06-age15;
do year =2006 to 2015;
age(year) = floor((intck('month',dob,mdy(7, 1, year))-(1
OUTPUT;
end;
run;

error messages ===============================================
ERROR: Array subscript out of range at line 137 column 2.


When define an array as you did the array index expects values of 1 to 10; age(1) for example. If you want to use actual values of year such as 2006 then you need to define the array boundary limits:

data junk;
   array a (2006:2015) age06-age15;
   do year=2006 to 2015;
      a(year) = <calculation>;
   end;
run;

 

 

HB
Barite | Level 11 HB
Barite | Level 11

To recap all of the above (because it is a good learning experience for me)  this code:

 

data birthdates;
   input dob date9.;
datalines;
01jan1962
26oct1973
17aug2000
05may1999
;
run;

data age_at_years;
	set birthdates;
	array age(2006:2015) age06-age15;
		do year = 2006 to 2015;
			age(year) = floor((intck('month',dob,mdy(7, 1, year))-(1<day(dob)))/12);
		end;
	drop year;
run;

produces a table that looks like:

 

                     
                     
dob age06 age07 age08 age09 age10 age11 age12 age13 age14 age15
731 44 45 46 47 48 49 50 51 52 53
5047 32 33 34 35 36 37 38 39 40 41
14839 5 6 7 8 9 10 11 12 13 14
14369 7 8 9 10 11 12 13 14 15 16

 

Cool.

 

Edit:

 

1.  Standard admonition about storing calculated values.

 

2. @ballardw I think I learn from your every post. +1

Yoko
Obsidian | Level 7
Thank you!

I appreciate that you added 'drop year;' .



Yoko


Yoko
Obsidian | Level 7
Thank you, it worked!

Yoko


SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 10 replies
  • 1490 views
  • 1 like
  • 5 in conversation