Age Calculation in tcl

Clovertech Forums Read Only Archives Cloverleaf Cloverleaf Age Calculation in tcl

  • Creator
    Topic
  • #49096
    David Harrison
    Participant

      I’m trying to use tcl to calculate an age in years. The format of the birthdate is dd/mm/yyyy where the day and month can be a single digit (eg 2/3/1947). I’m extracting the last 4 characters as the year using a string function. When I try to subtract the year from the current year, I get an error .

      Any help appreciated.

      Dave

    Viewing 10 reply threads
    • Author
      Replies
      • #60715
        Jonathan Presnell
        Participant

          try this:

          set birthYear [lindex [split $birthYear /] 2]

          set yrsOld [expr ($currentYear – $birthYear)]

        • #60716
          David Harrison
          Participant

            Still having problems. Here’s my code, in this example xlateInVals = {3/4/1953 00:00:00}

            Code:

            ######################################################################
            # Name: XltVPBulkAge
            # Purpose: Get age in years from bulk data file
            # UPoC type: xltp
            # Args: none
            # Notes: All data is presented through special variables.  The initial
            # upvar in this proc provides access to the required variables.
            #
            # This proc style only works when called from a code fragment
            # within an XLT.
            #

            proc XltVPBulkAge {} {
               upvar xlateId       xlateId
             xlateInList   xlateInList
             xlateInTypes  xlateInTypes
             xlateInVals   xlateInVals
             xlateOutList  xlateOutList
             xlateOutTypes xlateOutTypes
             xlateOutVals  xlateOutVals
             set byr [lindex [split $xlateInVals /] 2]
             set byr [lindex $byr 0]
             set nyr expr[clock format [clock seconds] -format “%Y”]
             set age [expr “$nyr – $byr”]
             set xlateOutVals $age
            }

            errorInfo:

            can’t use non-numeric string as operand of “-“

               while executing

            “expr {$nyr – $byr}”

               (procedure “XltVPBulkAge” line 6)

               invoked from within

            “XltVPBulkAge”

          • #60717
            Charlie Bursell
            Participant

              You have to check for those times when your input may be empty or non-numeric.

              Try this

              set inp [lindex $xlateInVals 0]

              # If not a valid date/time the clock scan command fails

              if {[catch {clock format [clock scan $inp] -format %Y} birthYear]} {

                     # Issue an error message and leave age blank

                      echo nnReceived invalid birthdatenn

                      set age {}

              } else {

                     # Valid birthdate

                     set nowYear [clock format [clock seconds] -format %Y]

                     set age [expr $nowYear – $birthYear]

              }

              set xlateOutVals

            • #60718
              David Harrison
              Participant

                I’ve got it to work now thanks. I spotted an error in my code:

                Code:

                set nyr expr[clock format [clock seconds] -format “%Y”]

                Should be:

                Code:

                set nyr [clock format [clock seconds] -format “%Y”]

              • #60719
                Barbi
                Participant

                  set inp [lindex $xlateInVals 0]

                  # If not a valid date/time the clock scan command fails

                  if {[catch {clock format [clock scan $inp] -format %Y} birthYear]} {

                        # Issue an error message and leave age blank

                         echo nnReceived invalid birthdatenn

                         set age {}

                  } else {

                        # Valid birthdate

                        set nowYear [clock format [clock seconds] -format %Y]

                        set age [expr $nowYear – $birthYear]

                  }

                  #set xlateOutVals

                    set xlateOutVals

                      1 3]]

                      echo pt age = $age

                      pt age = -188313

                      Does this calculate the days?  Why can’t I get the $age to stink down to position 1-3 only?

                  • #60720
                    Levy Lazarre
                    Participant

                      Hello, David

                      Please note that your code snippet as shown is missing a step. You cannot just subtract the birth year from the current year to obtain the age in years. You also have to account for whether the person has had a birthday yet this year.

                      With the example you have given (3/4/1953), the code would return an age of 65, which is wrong because the person has not reached a birthday yet this year and is still age 64.

                      Therefore you must also compare the day of birth (3/4) to the current day and subtract 1 from the age if the day of birth is greater (birthday not celebrated yet this year).

                    • #60721
                      Robert Kersemakers
                      Participant

                        Isn’t the proc ‘gc_calculateAge’ part of the standard Cloverleaf installation?

                        I’m never sure whether we got stuff from our Dutch supplier, or whether it’s part of standard Cloverleaf.

                        Zuyderland Medisch Centrum; Heerlen/Sittard; The Netherlands

                      • #60722
                        Levy Lazarre
                        Participant

                          Robert,

                          To my knowledge, this proc is not part of the standard Cloverleaf installation. You must have a supplemental library.

                        • #60723
                          David Barr
                          Participant

                            Rather than trying to figure out if the current date is before or after the birthday, why not take the current time in seconds since the epoch ([clock seconds]), subtract the birthdate in seconds since the epoch ([clock scan $birthdate], which may be negative if the patient was born before the epoch), and divide the result by the number of seconds in a year?

                          • #60724
                            David Barr
                            Participant

                              Actually that wouldn’t work because some years are longer than others. Maybe subtracting the years and figure out if the patient has reached the birthdate this year is best.

                            • #60725
                              Robert Kersemakers
                              Participant

                                gc_calculateAge is indeed part of a package supplied only to the Dutch market. Can’t share them with the world.

                                Googled it and found this proc, which should give the age in years as well:

                                Code:

                                # yearsBetween —
                                #
                                # Returns the years elapsed between two given timestamps
                                #
                                # Arguments:
                                #  oldertime
                                #  newertime
                                proc yearsBetween {oldertime newertime} {
                                   set i 1

                                   while {$oldertime < [clock scan "$i year ago" -base $newertime]} {
                                       incr i
                                   }

                                   return [expr {$i – 1}]
                                }

                                Example:

                                Code:


                                set date1 [clock scan 1987-01-18]
                                set date2 [clock seconds]

                                puts [yearsBetween $date1 $date2]

                                Zuyderland Medisch Centrum; Heerlen/Sittard; The Netherlands

                            Viewing 10 reply threads
                            • The forum ‘Cloverleaf’ is closed to new topics and replies.