Blogs Home
Friday, June 24, 2011 12:37 PM

Formatting Phone Numbers (MAGIC or Client/Server)

Written by Joe Cocuzzo, Senior VP of Report Writing Services - iatricSystems

In data export, you often get a specification that looks like this:

Field Length
Patient's home phone 15
Patient's Street 30

Often, the party receiving the data has a very specific required format for a telephone number, which they don't bother to describe in their specification. They don't realize that a telephone number in the MEDITECH HCIS can be any string that fits in the box.


If we sent the IATRIC,JOE home phone number of "DO NOT CALL ME" to most vendors, they would reject the record and then reveal their secret phone number format requirement.

This month we will describe a phone number formatting utility that can take Meditech's phone number data (any string) and return a desired format. If you want to skip all the geeky stuff and go right to our Client/Server or MAGIC report library and get the utility, feel free – I won't be offended.

The utility accepts three arguments:
A is the phone number data
B is an optional default area code (in case the phone number data is only 7 digits)
C is an integer to control the format returned:
5 = NNNNNNNNNN (all digits)
6 = queued string |0 = area code |1 = exchange |2 = number

You could modify the program if you need a format that we did not think of.

The utility uses the "not tilde" syntax to remove all but the digits from the phone number data and then parses the phone number into an area code, exchange, number and extension, then puts the pieces together according to the format requested. If the data contains less than 7 digits, nothing is returned.

The '~ syntax is a variation on the use of the ~ "translate" command.

The translate operator takes each character of the string on the left, takes its ASCII value, and goes to that position of the string on the right and if a character exists at that position, the character is substituted, if nothing exists at the position, a space is substituted.

This is why "lower case"~$.L.TO.U returns LOWER CASE. The $L.TO.U is a system string that looks like this:


The $L.TO.U and $U.TO.L system strings are a MAGIC platform feature. In C/S you use STRING@Tr.l2u or STRING@Tr.u2l instead of STRING~$L.TO.U and STRING~$U.TO.L

It contains upper case A-Z in the positions 97-122 so that a-z get converted to A-Z.

Using '~ (not translate) takes each character of the string on the left, takes its ASCII value, and goes to that position of the string on the right and if a character exists at that position, the character is substituted and if a ASCII 255 exists at that position the character is stripped.

In our utility we build a string that contains just the digits and a D(255) for every other position from 0-127.

The '~ allows you to build a "filter string" and remove all the characters you do not want, without needing to process each character of the source string in a loop.


The macro call is,B,C)

Here is some sample output of the various conversion schemes, including the syntax of the calls used.

Notice in the call to return the area code, exchange, number, and extention as separate queued pieces (format 6) the program does assignment to a list of local variables in {}

This is the equivalent to:,978,6)^RESULT,
But,978,6)^{AREA,EXC,NUM,EXT} is a much more compact way to write it.


The report has been uploaded to our MAGIC report library as and to our C/S report library as

You can find additional NPR Tips on our website at, as well as information about our on-site NPR Report Writer Training and NPR Report Writing Services.

Upcoming NPR Training Opportunities:

We are pleased to offer NPR Report Writer training sessions at host sites. Details and a course description are available on our website at

Location Level Instructor Date Cost
Greater Baltimore Medical Center
Baltimore, MD
Beginner / Intermediate Jim Fahnestock September 13-15, 2011 $750
Greater Baltimore Medical Center
Baltimore, MD
Intermediate / Advanced Philip Sherry October 10-12, 2011 $750

To subscribe for email notifications for new classes, please follow this link:

For more information or to reserve a seat, please contact our NPR report writing team at

(This article originally appeared in the June 2011 issue of Iatric Systems’ Updates! newsletter.)