Blogs Home
Wednesday, April 17, 2013 5:02 PM

Selection based on a subtotal (MAGIC or Client/Server)

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

Many NPR report writers have trouble writing a report that has to do selection based on a total. For example, list all patients with three or more visits to a particular doctor during a period. A fragment called from a computed select field can be used for this kind of report, but fragments are tedious and slow. Some loops in a start macro could be used, but that can be a bit too complex.

This tip will show you how you can use the temporary sort file built by your report plus a line check to effectively select based on a total. No fragment is needed, and just one simple loop (using something called "physical nexting") on the temp file will get the job done.

Even though Joel hates LC’s as a record selection technique, I had a recent request where the hospital asked me to change a report they originally insisted they wanted per visit to one that was per unit number and this trick saved a lot of re-write time. Joel wants me to point out that anything in the TR will be "wrong" because records suppressed with an LC still count in any totals or other statistics. You really don’t have those in a mailing list, however.

First, you need to add a sort by unit.number with a key and trailer, and set up your report to select all visits for a particular attending doctor for a date range.

You also prompt with an "IG" or IGNORE operator so that the desired minimum visit count can be found in c.xx.visit.count.

Without a bit of extra programming, we will see all visits for the selected doctor:

If we could count the visits before printing the set of detail records, we could use a line check to suppress the set of visits for patients with less than the minimum number.

Since we have added a sort, the report writer builds an index of all selected records before it prints any:

What we need to do is loop in the HK1 region (the sort per unit number) and count how many entries are in the temporary file so we can set a flag to control whether the set of visits print or not.

If we use a macro called AL HK1, we will have the local variable unit.number (do not use the @ sign) equal to the unit number of the patient who is about to be printed.

Notice how if we use the @ sign on the variable unit number, the translator converts that to a reference to stored data at the individual record level. In the HK region sorting by unit number, your urn is set to the value of the PRIOR patient or is nil for the first patient, so you should not use that variable to figure out which patient you are "on."

But if you use unit.number you get dzU, which is the local subscript that the report is looping on, and it represents the unit number of the patient you are about to print.

If we loop on the temp file like so, we can count the records and set a flag in OK to allow the visits to print if we have enough of them:

The > operator is doing "physical nexting." This goes through all subscripts of a structure with one loop, so you could have any number of sort subscripts in the temporary sort file after the unit number and the looping and counting would still work. If you used the "+" operator (logical next), you’d need to write a level of looping for each sort subscript and nest them.

The "NOT.AS.GOOD" code, which loops on /["temp" instead of [/R.TF, is deficient because MEDITECH will swap the temp sort file out to disk and it is kept in a different place, and your loop will fail if you run your report for a large set of records. Using /R.TF to loop on makes sure your code works whether the temp file is in memory or on the disk.

Now if we run the report, we will only see patients with three or more visits during the period for the selected doctor.

Sample reports called have been uploaded to our report library, along with a more complex MAGIC report:, which includes code to go to MRI.PAT to exclude expired patients and get the latest mailing address information from MRI.DRC. That report also has a "list visits" or "just list patients per unit number" option.