Calculate Age Down To MD&Y

Reply
Occasional Contributor
Posts: 15

Calculate Age Down To MD&Y

New here and to SAS - is it possible and how can I calculate today's date down to the day, month, and year.

Frequent Contributor
Posts: 128

Re: Calculate Age Down To MD&Y

I'm not sure exactly what you're asking for, are you looking to create a variable with today's date in the format ddmmyyyy?  Or what specifically are you wanting the output or variable to look like and in what format?  You could create a macro variable and call it, or you could create a field in a data set equal to the same value....

MACRO

%let today=%sysfunc(today(),ddmmyyn8.);

%put &today;

DATA STEP

data today;

format today ddmmyyn8.;

today=today();

run;

A little more information of what you're looking to do with it, or what expected output you want would help.

Occasional Contributor
Posts: 15

Re: Calculate Age Down To MD&Y

This is what I have right now -

data age;

    set members14to15;

    format today DATETIME22.3;

    today = today();

    year = year(today());

    age = year - year(datepart(birth_date));

    if birth_date > today then age=age-1;   

run;

But if someone has NOT had a birthday this year, then it is automatically assuming they have, and adding a year when it should not. I just want to see the age in whole numbers, with no decimals.

Grand Advisor
Posts: 17,332

Re: Calculate Age Down To MD&Y

Frequent Contributor
Posts: 128

Re: Calculate Age Down To MD&Y

As morgalr previously showed, I would look into the INTCK() or INTNX() function.  You can specify what you what you want to subtract from two separate dates, in this case years from today's date and the date of birth_date.  I would recommend going and researching that route as that is the best and most accurate way to subtract two dates.

Hope that helps!

Contributor
Posts: 26

Re: Calculate Age Down To MD&Y

I think this will give you the results you desire in myYears, myMonths, myDays:

data temp;
  myBirthday = '05-AUG-1974'd;
  myDate = today();

  myYears = intck('YEAR', myBirthday, myDate, 'C');
  myBirthday = intnx('YEAR', myBirthday, myYears, 'SAME');
  myMonths = intck('MONTH', myBirthday, myDate, 'C');
  myBirthday = intnx('MONTH', myBirthday, myMonths, 'SAME');
  myDays = intck('DAY', myBirthday, myDate, 'C');

  keep myYears myMonths myDays;
run;

Grand Advisor
Posts: 10,210

Re: Calculate Age Down To MD&Y

It really helps if you provide some example start data, in this case probably a couple of Birthdates and the date you want the value(s) calculated. Then for the same data show the desired results.

The answer to the question is yes. The details of how depend on the actual desired result.

Frequent Contributor
Posts: 144

Re: Calculate Age Down To MD&Y

To do that I use to call the rolland macro AGEDEC

http://www.datasavantconsulting.com/roland/agedec.sas

It have even a parameter to decide where a subject born in 29FEB must be birthdate only on 29FEB or in 1MAR.

Occasional Contributor
Posts: 15

Re: Calculate Age Down To MD&Y

This worked. I needed to round using floor. I was having people show up as 65, even if they are actually 64 and haven't had their birthday yet this year.

data age;

    set everything14to15;

    age = floor((today()-birth_dateall)/365);

run;

Ask a Question
Discussion stats
  • 8 replies
  • 287 views
  • 0 likes
  • 6 in conversation