Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Birthdate calculation not working 1

Status
Not open for further replies.

sap1958

Technical User
Oct 22, 2009
138
0
0
US
data Employee;
set Testdb.tblEmployee;
/*Age = (TODAY() - Birthdate) / 365.25; */
/*Age = intck('year',Birthdate,date()); */
/*Age = intck('year',Birthdate,TODAY()); */
Age = int(intck('year'Birthdate,TODAY())/12);
run;

I tried each of the above methods to calculate age and none seem to work. Here is an example of how the birthdate looks. 22AUG1966:00:00:00
 
sap1958 you know you didn't have to make a new thread to reply to the question?

The problem is that your birthdate variable is in datetime format so you'll need to use datepart(birthdate).

I'll use Klaz2002 solution as you seemed to have preferred theres,

age = intck('year',datepart(Birthdate),date());
 
jj72uk, one more question. Using that same script here is what I came up with
data Employee;
set Testdb.tblEmployee;
Age = intck('year',datepart(Birthdate),date());
run;

The age works fine so thanks so much!!!!! Now lets say I want to convert the datetime to a date when I first compile the data set Employee. The default for a date is datetime. 22AUG1966:00:00:00
Lets say i want the date to look like 08/22/1966 when it first runs.
 
Hi Sap1958,

Before you go any further, please note that:

Code:
Age = intck('year',datepart(Birthdate),date());

is the incorrect way to calculate age in SAS as it won't provide the correct age if the day of birth is after todays day.

For example, try running the following code and see if it matches the output you would expect:

Code:
data _null_ ;
   start='02Jan00'd;*Start day before end day;
   end  ='01Jan01'd;
   y=intck('year',start,end) ;
   z=floor((intck('month',start,end)-(day(end)<day(start)))/12); 
   format start end date9. ;
   put 'Not corrected for day: ' y 'year(s) between ' start  'and ' end ;
   put 'Corrected for day: ' z 'year(s) between ' start  'and ' end ;
   run;

Please see for more information on this, as well as two caveats to when this will not work (at the end of the page).

Regarding your second question: To convert the datetime into a date you will have to create another variable which contains the date portion, which can be done useing the datepart function others have already shown you and then applying a date format to it.

Code:
x=datepart(datetime) ;
format x ddmmyy8. ;

Alternatively, if you don't want to create another variable, you can create a custom picture format to blank out the time part. Just remember that formats do not change the underlying value.

Code:
proc format ;
   picture cdate 
       low-high = '%0d/%0m/%0y'(datatype=datetime);
	   run;
data _null_ ;
   end  ='31Jan01:00:09:00'dt;*datetime value;
   format end cdate.;
   put end;
   run;
 
The intck function shouldn't be used to calculate age. My bad! The function returns the difference in years and not 365 day years. (Try Dec 20 XXXX and Jan 10 XXX+1)

To get age use the first line in your post.
Code:
Age  = (DATE() - Birthdate) / 365.25;

You can format the AGE var using the 8. so that only the integer portion displays or you can truncate the value by using the INT() function.

Code:
Age  = int((DATE() - Birthdate) / 365.25);

Also make sure that your birtdate value is a date value and not a DateTime value. SAS is weird that way. SAS date values are the number of days since JAN 1, 1960. SAS datatime values are the number of seconds since JAN 1, 1960. There are 86,400 seconds in a day, so if you forget that your variable is supposed to be a datetime and then have that value use a SAS date you would still display a date of Jan 1 1960 (as there have only been 18660 days since that date :) )

Hope this helps.
Klaz
 
As per my orignal suggestion im still going with

age = floor((intck('month',birth,somedate)-(day(somedate)< day(birth))) / 12)

As per SAS solutions
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top