I tried to calculate age using the
current_age=int(yrdif(b_date, today(), 'ACTUAL')); Since the age is before 1960 for bene_id 02, It is giving me negative value.
Here is my data after calculating age:
bene_id b_date Current_age
01 18May64 54
02 18Jun22 -3
03 08sep51 67
Any help appreciated!
The YEARCUTOFF option only impacts how SAS determine the century when converting text with two digit years into dates.
You are not converting any text into dates. You already have data with the wrong century.
You might add logic to check the date for reasonableness before calculating age.
data B;
set A;
b_date=datepart(birthdate);
date=today();
if b_date > date then do;
put b_date = @ ;
b_date=intnx('year',b_date,-100,'s');
put '-> ' b_date ;
end;
current_age=int(yrdif(b_date, date, 'ACTUAL'));
format b_date date date9.;
run;
Try:
Age_exact = floor((intck('month',DOB,Date)-(day(Date) < day(DOB))) / 12);
plug in: today() for date
I used it as:
age_exact=floor((intck('month', b_date, Date)-(day(Date) < day(b_date)))/12);
and I saw both two new variables were empty.
Ok tested with your sample that you gave us. Here it is:
options yearcutoff=1910;
data have;
input bene_id $ b_date :date9.;
format b_date date9.;
cards;
01 18May64
02 18Jun22
03 08sep51
;
data want;
set have;
date=today();
Age_exact = floor((intck('month',b_date,Date)-(day(Date) < day(b_date))) / 12);
format date date9.;
run;
Thanks, but I'm still getting -4 as a value for age_exact.
Please run the code i gave you. Here is the result. Just run and see:
proc print data=want noobs;run;
SAS Output
The SAS System |
bene_id | b_date | date | Age_exact |
---|---|---|---|
01 | 18MAY1964 | 12SEP2018 | 54 |
02 | 18JUN1922 | 12SEP2018 | 96 |
03 | 08SEP1951 | 12SEP2018 | 67 |
I had created the b_date variable from the date variable birthdate(with datetime. format) using following code:
birthdate=datepart(b_date);
format birthdatef date.;
My data now looks like this after applying the code you suggested.
bene_id | birthdate | b_date | date | Age | |
1 | 18May64:00:06:08 | 18-May-64 | 12-Sep-18 | 54 | |
2 | 18Jun22:00:09:03 | 18-Jun-22 | 12-Sep-18 | -4 | |
3 | 8Sep51:00:01:06 | 8-Sep-51 | 12-Sep-18 | 67 |
What happens when you use a DATE9 format. I suspect you have a data integrity issue.
Your problem is not that the birth date is before 1960. The problem is that the birth date is after 2018.
Displaying the dates with only two digits of the year is confusing you, but not SAS.
Current_ current_ Obs bene_id b_date age_x age 1 1 18MAY1964 54 54 2 2 18JUN1922 -3 96 3 2 18JUN2022 -3 -3 4 3 08SEP1951 67 67
thanks, any suggestion on how to handle the negative age?
@pmpradhan wrote:
I tried to calculate age using the
current_age=int(yrdif(b_date, today(), 'ACTUAL')); Since the age is before 1960 for bene_id 02, It is giving me negative value.
Here is my data after calculating age:
bene_id b_date Current_age
01 18May64 54
02 18Jun22 -3
03 08sep51 67
Any help appreciated!
The most likely issue is that your dates had 2-digit years and when you read them the YEARCUTOFF option assumed the dates you thought were 19xx were actually 20xx.
Run this code:
proc options option=yearcutoff; run;
Check the log for the value displayed. Example from mine:
YEARCUTOFF=1926 Specifies the first year of a 100-year span that is used by date informats and functions to read a two-digit year.
Any year prior to 26 will be treated as 20XX.
Fix: either use 4 digit years or adjust the year cutoff value with
options yearcutoff=1920; (or similar).
Caution: if you use 1918 then any births in 2018 are in danger.
Then reread the data with the two digit years and then reset your yearcutoff option.
The above is one way.
Or you could test the year of b_date and adjust the birth date:
if year(b_date) > year(today()) then b_date=intnx('year',b_date,-100);
BEFORE calculating the age.
your negative value is because the second card has jun-18-2022 as the birthdate if you don't set the options yearcutoff=1910;
Here is my result from
proc options option=yearcutoff;
run;
SAS (r) Proprietary Software Release 9.4 TS1M5
YEARCUTOFF=1910 Specifies the first year of a 100-year span that is used by date informats and functions to read a two-digit
year.
is the 22 year 1922 or 2022. If 1922 then the options yearcutoff=1910, works correctly. If the person was born in 2022 then you have to wait until they are born to calculate their age.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.
Find more tutorials on the SAS Users YouTube channel.