Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Qlik Open Lakehouse is Now Generally Available! Discover the key highlights and partner resources here.
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

Age in Year Month Day Format

Hi,

Please give me some ideas about how to calculate Age for Patient in the hospital by using Visit Hospital Date - Date of Birth

and the result should be Year, Number of Month , Day

such as

Date of Birth 01/01/2011

Visit Hospital Date 11/03/2012

Age = 1 Year 2 Months 11 Days

Thank you in advance for sharing....

2 Replies
swuehl
MVP
MVP

ainiko,

this is a little complex using QV functions, but I think you can do it like this:

SET DateFormat='DD/MM/YYYY';

INPUT:

LOAD *

, if(day(VisitDay) >= day(DoB),

  mod(MonthIDVisitDay-MonthIDDoB,12),

  mod(MonthIDVisitDay-MonthIDDoB,12)-1) as AgeMonths

, if(day(VisitDay) >= day(DoB),

  day(VisitDay)-day(DoB),

  VisitDay-addmonths(DoB,12*AgeYears+mod(MonthIDVisitDay-MonthIDDoB,12)-1))+1 as AgeDays

;

LOAD *

, age(VisitDay,DoB) as AgeYears

, Year(DoB)*12+Month(DoB) as MonthIDDoB

, Year(VisitDay)*12+Month(VisitDay) as MonthIDVisitDay

INLINE [

DoB, VisitDay

01/01/2011, 11/03/2012

01/01/2010, 29/02/2012

01/03/2010, 29/02/2012

];

I just used a preceding load so I can use fields I calculated in the INLINE table load, this makes it somewhat easier to read.

Maybe there is a VBScript function I don't know of you can call as Macro code from the script, but above should work using only QV functions.

The created Age fields can then easily be concatenated to a string like

=AgeYears &' Years '&AgeMonths&' Months '&AgeDays&' Days'

in the frontend or also as a new field, to get a string like you requested above.

Hope this helps,

Stefan

edit: corrected bugs in AgeMonths and AgeDays

edit2: Corrected another bug when start and end date fall into the same calendar month.

Not applicable
Author

Thank you very much. It is helpful.