## Date Function

Occasional Contributor
Posts: 19

# Date Function

Hi All.

I am calculating here the age based on Birth_Date.

Birth_date is of type numeric and length 8 Format and Informat MMDDYY10.

I have the below code snippets and its working fine.

data date;

set age;

age_date= put(Birth_Date,10.);

Age= (today()-age_date)/365);

run;

Question:

today() produces a column of type NUMERIC with length 8.

age_date here will produce a column of type Character with length 10.

then how this Age= (today()-age_date)/365) is working fine.

Vishyy

Super User
Posts: 10,280

## Re: Date Function

Forget that useless conversion to character. Use the intck() function with today() and birth_date.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Occasional Contributor
Posts: 19

## Re: Date Function

Hi

Thanks for replying, I am new ro SAS.

Could you please let me know the code for using intck() function with today() and birth_date.

vishyy

Posts: 3,066

## Re: Date Function

[ Edited ]

vishyy wrote:

Hi All.

I am calculating here the age based on Birth_Date.

Birth_date is of type numeric and length 8 Format and Informat MMDDYY10.

I have the below code snippets and its working fine.

data date;

set age;

age_date= put(Birth_Date,10.);

Age= (today()-age_date)/365);

run;

Question:

today() produces a column of type NUMERIC with length 8.

age_date here will produce a column of type Character with length 10.

then how this Age= (today()-age_date)/365) is working fine.

If the character value is entirely numbers and no non-numeric characters, then you can do math functions on it, the character variable will be converted to numeric for performing arithmetic.

By the way, I don't see any point to creating age_date as a character, or why you need it at all. It seems to me like it is a wasted and meaningless step. It seems to me that all you need is:

```data date;
set age;
Age= (today()-birth_date)/365);
run;
```

And @KurtBremseris right, the intck function is a better way to do this, it will handle leap years, but your method will not.

--
Paige Miller
Occasional Contributor
Posts: 19

Hi

Vishyy

Super User
Posts: 10,280

## Re: Date Function

vishyy wrote:

Hi

Vishyy

Just google for "sas intck function", and you'll get the documentation. It's all in there.

Don't be afraid to think on your own, it doesn't hurt.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Occasional Contributor
Posts: 19

## Re: Date Function

Hi

Used the below code snippets.

data intck;

set age;

Age= intck('YEAR','Birth_Date',today());

run;

The Age is coming as .

Birth_date is of type numeric and length 8 Format and Informat MMDDYY10.

PROC Star
Posts: 8,169

## Re: Date Function

I have found the following to be the easiest and best way to calculate age:

`YRDIF(dob,eventdt,'AGE');`

However, to answer your question, the only way I can see that you are getting a reasonable calculation of age is that your variable, dirth_date, is actually not just a number, but a SAS date (i.e., the number of days since 01JAN1960).

As such, while you do some unnecessary stuff (like the put statement), you are simply calculating the number of days between birth and today and dividing by 365.

Come close, but doesn't account for leap years.

Art, CEO, AnalystFinder.com

PROC Star
Posts: 8,169

## Re: Date Function

Soapbox:

I've only been using SAS for 43+ years, so maybe I just don't yet know where to find answers to some questions.

The documentation for calculating age is clearly lacking. I still think that the AGE option in the yrdif function is the best method, but the SAS documentation doesn't make the answer clear. The first Google hit is a 2010 note, by Susan Slaughter, stating why she and Laura removed the yrdif function from the Little SAS book.

I think that note is now out of date and should be removed.

Regardless, here is a note from SAS support regarding how to use the intck function to calculate age:

http://support.sas.com/kb/24/567.html

Art, CEO, AnalystFinder.com

Valued Guide
Posts: 2,191

## Re: Date Function

Hi Art

http://support.sas.com/kb/24/567.html

is entitled "Sample 24567: Calculate a person's age"

regards

Peter

PROC Star
Posts: 8,169

## Re: Date Function

@Peter_C: Back to the soapbox .. that documentation is the one I suggested IF the OP was determined to use the intck function to calculate age.

I have a problem with it and I can't find ANYTHING from SAS that suggests which method is the best one to use.

The method suggested in that post is shown below (minus the unnecessary (IMHO) use of the proc fcmp function):

```data birth; input name \$ bday :mmddyy10.; datalines;Miguel 12/31/1973Joe 02/28/1976Rutger 03/29/1976Broguen 03/01/1976Susan 12/12/1976Michael 02/14/1971LeCe 11/09/1967Hans 07/02/1955Lou 07/30/1960Art 02/29/1976;run; data ages; set birth; retain current '28feb2017'd; format bday current worddate20.; intckage=int(intck('month',bday,current)/12); if month(bday)=month(current) then  intckage = intckage-(day(bday)>day(current)); yrdifage=int(yrdif(bday,current,'age'));run;
```

If you run the above code you'll find that the one date that is different between the two methods is when the date is the 28th of February on a non-leap year and the person's birth date is February 29th of a leap year.

The intck method won't age Art (not me .. I'm not quite that young) until March 1st, 2017, while the yrdif calculation will consider the person as having aged another year on February 28th, 2017.

Which one is correct? I happen to think it is the yrdif calculation, but the documentation doesn't say (or at least I can't find it).

Art, CEO, AnalystFinder.com

Super User
Posts: 13,583

## Re: Date Function

I get to work with data where Age is not automatically years but need to use age in weeks, two-week periods, and months. So the question I always have to ask is are we concerned with the typical legal age (number of birth days observed) or some actual biologic process.

I'm just waiting for some health data system to revert back to using fortnights.

Occasional Contributor
Posts: 19

## Re: Date Function

Hi

Used the below code snippets.

data intck;

set age;

Age= intck('YEAR','Birth_Date',today());

run;

The Age is coming as .

Birth_date is of type numeric and length 8 Format and Informat MMDDYY10.

Super User
Posts: 13,583

## Re: Date Function

'Birth_date' in quotes is a literal value and not the name of your variable.

Remove the quote marks.

Discussion stats
• 13 replies
• 311 views
• 5 likes
• 6 in conversation