Age Function Available on Ensemble Object Script?

220 views
Skip to first unread message

YM

unread,
Dec 1, 2011, 10:29:58 AM12/1/11
to InterSystems: Ensemble in Healthcare
Hi All,

On my HL7 Msg, on the PID Section, I have the field DateTimeOfBirth
and I want to calculate the Age so that if the Person (Baby) is < 2 I
Send the Msg otherwise it's ignored.

So on my Message Routing Rule I would like a Condition expression
like:

IF Age(source.{DateTimeOfBirth}) < “2”

Is that possible?

Currently using the $zdatetimeh function to get the date to $horolog
format - so I have:

Round(($h-$zdatetimeh(source.{DateTimeOfBirth},8))/365,1)

and then it’s a case of storing the age on an available field within
the Msg and re-routing the HL7 amd using ObjectScript code Etc Was
wondering if there was a more optimal way?

All comments appreciated - Many Thanks

Gary

unread,
Dec 1, 2011, 10:44:38 AM12/1/11
to InterSystems: Ensemble in Healthcare
Hi,

You could probably create a user defined function which will extend
Ens.Rule.Functionset and then use that in your Rule passing in the
date of birth field and using the less than condition.

Check out the documentation at
http://docs.intersystems.com/ens20102/csp/docbook/DocBook.UI.Page.cls?KEY=EGDV_prod_create#EGDV_prod_utility_functions
- this should give you enough to go on.

Regards,
Gary

Ricardo Santos

unread,
Dec 1, 2011, 11:07:19 AM12/1/11
to ensemble-in...@googlegroups.com

One way I've done this is how Gary mentioned. I created a UDF on a class extending Ens.Rule.Functionset, to which I passed a DOB.

Getting the horolog value of the DOB and today's, it was a matter of subtracting first element of the horolog value - $P($H,",") and subtracting the DOB's from today's value.  The result was the number of days elapsed since DOB and today.  Checking that is less than 365 * nrYears gave me the filtering threshold you are looking for.

Conversely, you can MOD the difference by 365 and that should give you patient's age in years.

Hope this helps!

Regards,
Ricardo  



--
You received this message because you are subscribed to the Google Groups "InterSystems: Ensemble in Healthcare Community" group.
To post to this group, send email to Ensemble-in...@googlegroups.com
To unsubscribe from this group, send email to Ensemble-in-Healt...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/Ensemble-in-Healthcare?hl=en

Lawrence Harris

unread,
Dec 1, 2011, 12:07:35 PM12/1/11
to <ensemble-in-healthcare@googlegroups.com>
Two items to note, the age is relative to some point in time, if you were to re-avaluate the message from an archive several years from now your calculation would be wrong.  I would base it at least on the date from the MSH header and not $H, usually I base it on a date from the OBR segment or the encounter date from the PV1.  Also a better divisor if you want to be simple is 365.25 which handles leap years better.  If you need to be more exact you are going to have to manage the day, month and year.  I have often had issues where the simple calculation is off by a few days and the complaints come back that the person is not yet x age or is x+1 at the time.

Creating a UDF based on Ens.Rule.FunctionSet is definitely the way to go.  At least you can change your mind and adjust it in one place and/or find all occurrences of the function call to make adjustments.  I would give the function two arguments; date-of-birth and as-of-date.

Good luck, Lawrence

YM

unread,
Dec 2, 2011, 4:15:42 AM12/2/11
to InterSystems: Ensemble in Healthcare
Hi - Thanks for those pointers and the divide by 365.25 which will
make it more accurate - wanted to know that as well.
Will look at the UDF and the way you've described and see how that
goes - Many Thanks.


On Dec 1, 5:07 pm, Lawrence Harris <lhar...@emrlogic.com> wrote:
> Two items to note, the age is relative to some point in time, if you were to re-avaluate the message from an archive several years from now your calculation would be wrong.  I would base it at least on the date from the MSH header and not $H, usually I base it on a date from the OBR segment or the encounter date from the PV1.  Also a better divisor if you want to be simple is 365.25 which handles leap years better.  If you need to be more exact you are going to have to manage the day, month and year.  I have often had issues where the simple calculation is off by a few days and the complaints come back that the person is not yet x age or is x+1 at the time.
>
> Creating a UDF based on Ens.Rule.FunctionSet is definitely the way to go.  At least you can change your mind and adjust it in one place and/or find all occurrences of the function call to make adjustments.  I would give the function two arguments; date-of-birth and as-of-date.
>
> Good luck, Lawrence
>
> On 2011-12-01, at 8:07 AM, Ricardo Santos wrote:
>
> One way I've done this is how Gary mentioned. I created a UDF on a class extending Ens.Rule.Functionset, to which I passed a DOB.
>
> Getting the horolog value of the DOB and today's, it was a matter of subtracting first element of the horolog value - $P($H,",") and subtracting the DOB's from today's value.  The result was the number of days elapsed since DOB and today.  Checking that is less than 365 * nrYears gave me the filtering threshold you are looking for.
>
> Conversely, you can MOD the difference by 365 and that should give you patient's age in years.
>
> Hope this helps!
>
> Regards,
> Ricardo
>

> <http://www.linkedin.com/in/ricardolsantos>


>
> On 1 Dec 2011, at 15:44, Gary wrote:
>
> Hi,
>
> You could probably create a user defined function which will extend
> Ens.Rule.Functionset and then use that in your Rule passing in the
> date of birth field and using the less than condition.
>

> Check out the documentation athttp://docs.intersystems.com/ens20102/csp/docbook/DocBook.UI.Page.cls...


> - this should give you enough to go on.
>
> Regards,
> Gary
>

> On Dec 1, 3:29 pm, YM <yunus.ma<http://yunus.ma>....@midyorks.nhs.uk> wrote:
> Hi All,
>
> On my HL7 Msg, on the PID Section, I have the field DateTimeOfBirth
> and I want to calculate the Age so that if the Person (Baby) is < 2 I
> Send the Msg otherwise it's ignored.
>
> So on my Message Routing Rule I would like a Condition expression
> like:
>
>           IF Age(source.{DateTimeOfBirth}) < “2”
>
> Is that possible?
>
> Currently using the $zdatetimeh function to get the date to $horolog
> format - so I have:
>
>           Round(($h-$zdatetimeh(source.{DateTimeOfBirth},8))/365,1)
>
> and then it’s a case of storing the age on an available field within
> the Msg and re-routing the HL7 amd using ObjectScript code Etc  Was
> wondering if there was a more optimal way?
>
> All comments appreciated - Many Thanks
>
> --
> You received this message because you are subscribed to the Google Groups "InterSystems: Ensemble in Healthcare Community" group.

> To post to this group, send email to Ensemble-in...@googlegroups.com<mailto:Ensemble-in-Healthcare@googl­egroups.com>
> To unsubscribe from this group, send email to Ensemble-in-Healt...@googlegroups.com<mailto:Ensemble-in-Heal­thcare-un...@googlegroups.com>
> For more options, visit this group athttp://groups.google.com/group/Ensemble-in-Healthcare?hl=en


>
> --
> You received this message because you are subscribed to the Google Groups "InterSystems: Ensemble in Healthcare Community" group.

> To post to this group, send email to Ensemble-in...@googlegroups.com<mailto:Ensemble-in-Healthcare@googl­egroups.com>
> To unsubscribe from this group, send email to Ensemble-in-Healt...@googlegroups.com<mailto:Ensemble-in-Heal­thcare-un...@googlegroups.com>

Dale du Preez

unread,
Dec 2, 2011, 8:30:30 AM12/2/11
to Ensemble-in...@googlegroups.com
For the date comparisons it may be simpler to use the
$SYSTEM.SQL.DATEDIFF() function to calculate the difference. You may
need to convert the date to one of the formats this function accepts,
but it should make it simpler to calculate this sort of difference
without resorting to calculations that don't precisely take into account
leap years and the like.

I hope that proves helpful,
Dale

David Loveluck

unread,
Dec 2, 2011, 8:55:17 AM12/2/11
to ensemble-in...@googlegroups.com
If anyone creates their own functions, I recommend that you name them distinctively and consistently. Perhaps by prefixing them XYZAge() where XYZ represents your organization.

This means you and our support will instantly recognize them as your functions not standard functions. It means they will appear together in the drop down list of the rules editor. And perhaps most importantly, if InterSystems add an Age() function at some stage in the future there will be no conflict.


Dave

Lawrence Harris

unread,
Dec 2, 2011, 2:42:42 PM12/2/11
to ensemble-in...@googlegroups.com
You are correct except that it does not work for the detailed case. My only point is that in some cases you get issues like normal ranges for tests that should include only patients say between 0 and 18 years. Then when one patient who's birthday was effectively tomorrow gets the wrong range because they are in their 19th year but not yet reached their 19th birthday. It's often frustrating because the 'effects' on the dataset are not clinically relevant but often people are pedantic and testing procedures even more so.

eg. The first should be 50, the second 51. It appears to only look at the year

USER>w $system.SQL.DATEDIFF("year","15 jun 1960","14 jun 2011")
51
USER>w $system.SQL.DATEDIFF("year","15 jun 1960","16 jun 2011")
51

Where as using 365.25 works most of the time though it has problems at low ages. There is where you might have to actually look carefully at the birthday to decide between age A and A+1.say

USER>w $system.SQL.DATEDIFF("day","15 jun 1960","16 jun 2011")/365.25\1
51
USER>w $system.SQL.DATEDIFF("day","15 jun 1960","14 jun 2011")/365.25\1
50

YM

unread,
Dec 6, 2011, 5:23:11 AM12/6/11
to InterSystems: Ensemble in Healthcare
Thank You - wasn't aware of $SYSTEM.SQL.DATEDIFF() which I'll now look
at as well. Thank You for all the help.

> > For more options, visit this group athttp://groups.google.com/group/Ensemble-in-Healthcare?hl=en- Hide quoted text -
>
> - Show quoted text -

Marc Lang

unread,
Dec 6, 2011, 5:57:50 AM12/6/11
to ensemble-in...@googlegroups.com
Left in some comments here, but I have used this function with success


ClassMethod CalculateAge(dateOfBirth As %String) As %Integer
{

set now = $ZDATE($NOW(), 3)

set dobYear = $SYSTEM.SQL.YEAR(dateOfBirth)
w "Dob year ",dobYear,!

set thisYear = $SYSTEM.SQL.YEAR(now)
w "This year ",thisYear,!

set dobMonth = ($SYSTEM.SQL.MONTH(dateOfBirth))
w "Dob month ",dobMonth,!

set thisMonth = ($SYSTEM.SQL.MONTH(now))
w "This month ",thisMonth,!

set dobDay = ($SYSTEM.SQL.DAYOFMONTH(dateOfBirth))
w "Dob day ",dobDay,!

set thisDay = ($SYSTEM.SQL.DAYOFMONTH(now))
w "This day ",thisDay,!

set age = thisYear - dobYear

if (dobMonth = thisMonth)
{
w "Dob month is equal to this month",!

if (dobDay > thisDay)
{
w "Persons birthday hasn't passed this year",!
set age = age - 1
}
}


w "age is ",age,!


quit age

}


It could be called like so:

do ##class(GGC.Utilities.DateHelper).CalculateAge("1982-12-13")

Reply all
Reply to author
Forward
0 new messages