BookmarkSubscribeRSS Feed
vishyy
Obsidian | Level 7

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.

 

Thanks in advace.

Vishyy

 

13 REPLIES 13
vishyy
Obsidian | Level 7

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.

 

Thanks in advance.

vishyy

PaigeMiller
Diamond | Level 26

@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 @Kurt_Bremseris right, the intck function is a better way to do this, it will handle leap years, but your method will not.

--
Paige Miller
vishyy
Obsidian | Level 7

Hi 

 

Thanks for replying.

 

Could you please help me in using intck() function here.

 

Regards in advance

Vishyy

Kurt_Bremser
Super User

@vishyy wrote:

Hi 

 

Thanks for replying.

 

Could you please help me in using intck() function here.

 

Regards in advance

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.

vishyy
Obsidian | Level 7

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.

 

art297
Opal | Level 21

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

 

art297
Opal | Level 21

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

 

 

Peter_C
Rhodochrosite | Level 12

Hi Art

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

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

 

regards

Peter

art297
Opal | Level 21

@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/1973
Joe 02/28/1976
Rutger 03/29/1976
Broguen 03/01/1976
Susan 12/12/1976
Michael 02/14/1971
LeCe 11/09/1967
Hans 07/02/1955
Lou 07/30/1960
Art 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

 

 

ballardw
Super User

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.

vishyy
Obsidian | Level 7

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.

ballardw
Super User

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

Remove the quote marks.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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