Focal Point
[CLOSED] Calculate Age while taking into account leap years

This topic can be found at:
https://forums.informationbuilders.com/eve/forums/a/tpc/f/7971057331/m/3507054295

October 27, 2010, 04:31 AM
wamweri
[CLOSED] Calculate Age while taking into account leap years
Hello

Am new to the forum and Web focus.

I would like to be able to work out a person's name while taking into account the leap years.

Dividing by 365.25 is not accurate enough for what I want to do as its quite sensitive. I've also tried to do a datediff but thats not accurate enough.

I have searched for previous posts but non of them have what I need.

PS: I can do this quite easily in MS SQL. See code below :

SELECT DATEDIFF(YY, @BirthDate, @CurrentDate) - CASE WHEN( (MONTH(@BirthDate)*100 + DAY(@BirthDate)) > (MONTH(@CurrentDate)*100 + DAY(@CurrentDate)) ) THEN 1 ELSE 0 END AS AGE

This message has been edited. Last edited by: Kerry,


WebFOCUS 7.7.3
PMF 5.3.1,
Windows,
Mainly HTML..eventually XLS, PDF AHTML.
October 27, 2010, 06:15 AM
GamP
You can do the same of course in webfocus. Extract all the same portions from the dates involved and do the same logic. It will look a little bit different from how it looks in MS SQL, but the logic is the same and it will give you the same result.


GamP

- Using AS 8.2.01 on Windows 10 - IE11.
in Focus since 1988
October 27, 2010, 09:30 AM
wamweri
Thanks.

I've been trying for most of the morning and am still no where close. Its all pretty new to me.


WebFOCUS 7.7.3
PMF 5.3.1,
Windows,
Mainly HTML..eventually XLS, PDF AHTML.
October 27, 2010, 10:49 AM
GamP
Ok, here goes. This is assuming you have the birthdate as a YYMD field. If not, you'll have to do some conversion.
DEFINE FILE xxxx
-*Extract years
CDYY/YY = &DATEYY;
BDYY/YY = BIRTHDATE;
-* Compute difference
YEARS/I4 = CDYY - BDYY;
-* Extract month/day
CDMD/I8 = &MDYY / 10000;
HELP/A8YYMD = BIRTHDATE;
BDMD/I8 = EDIT(EDIT(HELP,'$$$$9999'));
-* See if a year must be subtracted
SUBYEAR/I4 = IF BDMD GT CDMD THEN 1 ELSE 0;
-* Compute age
AGE/I4 = YEARS - SUBYEAR;
END
TABLE FILE xxxx
PRINT BIRTHDATE AGE
END
Hope this helps ...


GamP

- Using AS 8.2.01 on Windows 10 - IE11.
in Focus since 1988
October 27, 2010, 03:27 PM
Alan B
I must be missing something here. DATEDIF works quite well I find, leap years included.

DEFINE FILE CAR
-*TODAY/DMYY = '&DATEDMYY';	
TODAY/DMYY = '28/02/1997';	
PAST/YYMD = '1996/02/29';
YRS/I2 = DATEDIF(PAST,TODAY,'Y');
MTH/I2 = DATEDIF(DATEADD(PAST,'Y',YRS),TODAY,'M');
DAY/I2 = DATEDIF(DATEADD(PAST,'M',(YRS*12)+MTH),TODAY,'D');
END
TABLE FILE CAR
PRINT YRS AS 'Year(s)'
      MTH AS 'Month(s)'
      DAY AS 'Day(s)'  
BY HIGHEST 1 COUNTRY NOPRINT
END



Alan.
WF 7.705/8.007
October 27, 2010, 03:39 PM
dbeagan
Yea, not really a leap year issue. Looks like it was more about not counting the current year in the age until you have your birthday in the current year. Looks like GamP nailed it.


WebFOCUS 8.2.06
October 28, 2010, 02:51 AM
Alan B
...and that is what DATEDIF does. It looks at the complete dates, not just the yeaar portion, and provides the correct number of years, as in the correct age of a person.


Alan.
WF 7.705/8.007
October 28, 2010, 08:50 AM
jgelona
Alan B is correct, all you need is the DATEDIF function. Assume DOB is a Smart Date:
CURRDT/YYMD=&YYMD;
AGE/I4=DATEDIF(DOB,CURRDT,'Y');



In FOCUS since 1985. Prod WF 8.0.08 (z90/Suse Linux) DB (Oracle 11g), Self Serv, Report Caster, WebServer Intel/Linux.
November 01, 2010, 06:08 AM
MH
Hi,

I have tried this code,

DEFINE FILE xxxx
-*Extract years
CDYY/YY = &DATEYY;
BDYY/YY = BIRTHDATE;
-* Compute difference
YEARS/I4 = CDYY - BDYY;
-* Extract month/day
CDMD/I8 = &MDYY / 10000;
HELP/A8YYMD = BIRTHDATE;
BDMD/I8 = EDIT(EDIT(HELP,'$$$$9999'));
-* See if a year must be subtracted
SUBYEAR/I4 = IF BDMD GT CDMD THEN 1 ELSE 0;
-* Compute age
AGE/I4 = YEARS - SUBYEAR;
END
TABLE FILE xxxx
PRINT BIRTHDATE AGE
END

it does work in a procedure but when i call the procedure in HTML it gives me the following error

FIELDNAME OR COMPUTATIONAL ELEMENT NOT RECOGNIZED: CDYY

FIELDNAME OR COMPUTATIONAL ELEMENT NOT RECOGNIZED: CDMD

FIELDNAME OR COMPUTATIONAL ELEMENT NOT RECOGNIZED: YEARS

Do we need to declare variables? or what is missing. because it does work if i run the procedure indiviually.


WebFOCUS 7.6
Windows, All Outputs
November 02, 2010, 08:00 AM
njsden
Those &variables you're using are assumed to always be there so you shouldn't need to declare/default them.

Can you add:
-SET &ECHO=ON;
at the beginning of your procedure to see what code is WebFOCUS actually running.

In addition to that, could you please post the HTML piece where your procedure is being invoked?

When posting, please make sure to enclose your code using the </> button in the message toolbar.



Prod/Dev: WF Server 8008/Win 2008 - WF Client 8008/Win 2008 - Dev. Studio: 8008/Windows 7 - DBMS: Oracle 11g Rel 2
Test: Dev. Studio 8008 /Windows 7 (Local) Output:HTML, EXL2K.
January 27, 2012, 03:00 PM
John_Edwards
So, let me get this straight -- at no point did anyone in IB-land consider returning the result as a real number so you could just truncate the pig? This is a crazy level of effort to figure out how old somebody is.

This message has been edited. Last edited by: John_Edwards,