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?
Hi @mariamon0
Could you please provide some sample data and specify variable types (character / numeric ) ?
best,
The date variables are in date format:
visit date: 1/25/2016
date of birth: 2/17/1940
how do I change the format?
@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???
@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.
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
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
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
;
@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.
@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.
@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 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.