BookmarkSubscribeRSS Feed
mariamon0
Fluorite | Level 6

 

I'm trying to calculate age based on the date they came for a visit (date0) and their date of birth (dob0)

aget=(date0)-(dob0)/365.25;

 

this calculated age is not working. Do I need to formate anything?

 

12 REPLIES 12
ed_sas_member
Meteorite | Level 14

Hi @mariamon0 

Could you please provide some sample data and specify variable types (character / numeric ) ?

 

best,

Jagadishkatam
Amethyst | Level 16
This code should work, unless there is no issue with the date0 or dob0 variables. Could you please check if the date variables are numeric, please post the sample data to understand the query and help you better.
Thanks,
Jag
mariamon0
Fluorite | Level 6

The date variables are in date format: 

visit date: 1/25/2016

date of birth: 2/17/1940

 

how do I change the format?

 

PaigeMiller
Diamond | Level 26

@mariamon0 wrote:

The date variables are in date format: 

visit date: 1/25/2016

date of birth: 2/17/1940

 

how do I change the format?

 


Formatting doesn't change the math. Are these numeric, or character (text), according to PROC CONTENTS???

--
Paige Miller
PaigeMiller
Diamond | Level 26

@mariamon0 wrote:

 

I'm trying to calculate age based on the date they came for a visit (date0) and their date of birth (dob0)

aget=(date0)-(dob0)/365.25;

 

this calculated age is not working. Do I need to formate anything?

 


Formatting doesn't change the math. It only changes the appearance of a number. Also, to compute age you can use the INTCK function or the YRDIF function.

 

But really, you should tell us more than just "not working". You should show us the LOG (code plus warning and error messages, do not chop anything out), and a portion of your data. This will provide sufficient information so we can diagnose the problems. Saying "not working" tells us nothing.

--
Paige Miller
mariamon0
Fluorite | Level 6

I'm not getting whole numbers when I do a proc print of aget. The log appears completely fine. Both date of birth and date visit are numeric variables 

 

PaigeMiller
Diamond | Level 26

Yes, your formula doesn't produce whole numbers.

 

If you use the INTCK() function or the YRDIF() function, you get whole numbers.

--
Paige Miller
mariamon0
Fluorite | Level 6

What would the code look like for that?

my date of birth variable is v_dob_0

and my visit date variable is v_date_0

 

 

Jagadishkatam
Amethyst | Level 16

you can try the below code

 

I assume that the variables are character, so converting them to numeric

 

data have;
input date0:mmddyy10. dob0:mmddyy10.;
aget=(date0-dob0)/365.25;
cards;
1/25/2016 2/17/1940
;
Thanks,
Jag
Astounding
PROC Star

@Jagadishkatam has given you the correct formula.  The formula you originally used was incorrect:

aget=(date0)-(dob0)/365.25;

Using your formula, SAS would perform the division first, then the subtraction.

PaigeMiller
Diamond | Level 26

@mariamon0 wrote:

What would the code look like for that?

my date of birth variable is v_dob_0

and my visit date variable is v_date_0


The documentation I linked to shows simple examples.


To correct an earlier statement I made, YRDIF does not return an integer. INTCK does return an integer.

--
Paige Miller
ballardw
Super User

@mariamon0 wrote:

I'm not getting whole numbers when I do a proc print of aget. The log appears completely fine. Both date of birth and date visit are numeric variables 

 


Please explain why with a formula like this you expected integers:

aget=(date0)-(dob0)/365.25;

Between 1 and 10000 days difference between two dates you will find exactly 6 values that would have an integer result. And that when the formula is:

aget=(date0 - dob0)/365.25;

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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.

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
  • 12 replies
  • 1277 views
  • 2 likes
  • 6 in conversation