Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Birthdate calculation not working

Birthdate calculation not working

Birthdate calculation not working

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);

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

RE: Birthdate calculation not working

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());  

RE: Birthdate calculation not working

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());  

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.

RE: Birthdate calculation not working

Hi Sap1958,

Before you go any further, please note that:


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:


data _null_ ;
   start='02Jan00'd;*Start day before end day;
   end  ='01Jan01'd;
   y=intck('year',start,end) ;
   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 ;

Please see http://support.sas.com/kb/24/808.html 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.


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.


proc format ;
   picture cdate
       low-high = '%0d/%0m/%0y'(datatype=datetime);
data _null_ ;
   end  ='31Jan01:00:09:00'dt;*datetime value;
   format end cdate.;
   put end;

RE: Birthdate calculation not working

kdt82 if you look at this post by the same OP (they made a dup. post)

I too stated your birthdate solution, someone else replied with an incorrect alternative which I didn't test/took for granted to work as the OP was using it.

RE: Birthdate calculation not working

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.


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.


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.

RE: Birthdate calculation not working

As per my orignal suggestion im still going with

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

As per SAS solutions

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close