Contents:  Main  Chapter  See Also:  Getting Started Manual  Programmer Manual 
One important place where you can use a computed expression is in a field that is computed. The COMPUTED field Data Type allows a computed expression to be stored in the data dictionary.
To define a field as COMPUTED, use the Modify File Attributes option and reply to the "DATA TYPE:" prompt with "computed."
Select OPTION: MODIFY FILE ATTRIBUTES DO YOU WANT TO USER THE SCREENMODE VERSION? Yes// NO (No) MODIFY WHAT FILE: PATIENT Select FIELD: AGE Are you adding 'AGE' as a new FIELD (the 13TH)? Y (YES) FIELD NUMBER: 13// DATA TYPE OF AGE: COMPUTED
You now enter the computed expression that will be stored in the AGE field. In this case, a function (TODAY), a field name (DATE OF BIRTH), and a numeric literal are combined with two arithmetic binary operators to give a numeric result.
'COMPUTEDFIELD' EXPRESSION: TODAYDATE OF BIRTH\365.25 TRANSLATES TO THE FOLLOWING CODE: S Y(16033,13,1)=$S($D(^DIZ(16033,D0,0)):^(0),1:""),X=DT S X=X,X1=X,X2=$P(Y(16033,13,1),U,3),X="" D:X2 ^%DTC:X1 S X=X\365.25
NOTE: You will only see the generated code if you have programmer access.
When creating a COMPUTED field that might have a numeric result, the following dialogue is presented:
NUMBER OF FRACTIONAL DIGITS TO OUTPUT (ONLY ANSWER IF NUMBERVALUED): 0
Pressing the Enter/Return key at this prompt means that the field is not numeric; it will be left justified on output. If you do answer, you indicate that the field is numeric and that you want the computed value rounded to a certain number of decimal places when it is printed. In this case, the number will be rounded to a whole number.
SHOULD VALUE ALWAYS BE INTERNALLY ROUNDED TO 0 DECIMAL PLACES? No// (No)
Since the value of a COMPUTED field can be used in other calculations, you need to indicate when rounding should occur. If you accept the default (i.e., "No"), rounding will not be done when the COMPUTED field is used in other calculations. A YES answer to this prompt means that you do want the rounded value used in calculations. Usually, you do not want values rounded at interim steps in a series of calculations. Thus, usually, you will accept the "No" default.
When a COMPUTED field is printed, the value will always be rounded to the number of decimal places you specify.
WHEN TOTALLING THIS FIELD, SHOULD THE SUM BE COMPUTED FROM THE SUMS OF THE COMPONENT FIELDS? No// (No)
If your computed expression involves division or multiplication, you will be asked how the field should be totaled. (You can total the values of a field in the Print File Entries option.) A NO answer to this prompt means that the COMPUTED field's expression will be evaluated for each entry and those results will be added. A YES answer means that values of each of the fields in the COMPUTED field's expression will be added first and then the COMPUTED field's expression will be applied to those totals.
For example, suppose A and B are the names of two fields and A/B is a computed expression. The table below shows the results of printing A, B, and A/B with different answers to the "WHEN TOTALLING THIS FIELD, ..." prompt:
A  B  A/B (YES: Total from totals of component fields) 
A/B (NO: Total from results for each entry) 

10  5  2  2  
100  50  2  2  
2  1  2  2  
Total  112  56  [112/56=] 2  [2+2+2=] 6 
To summarize, if you want your total to be the ratio or product of the total of the component fields, then answer this question YES. Otherwise, a NO answer is appropriate.
NOTE: The answer to this prompt only affects the Total produced by the Print File Entries option.
When defining a COMPUTED field, you will also be asked:
LENGTH OF FIELD: 8//
Here you can enter the maximum number of character positions that the field should occupy in output. The default value is eight, even if the COMPUTED field involves FREE TEXTtype fields. Be sure to allocate enough space to accommodate the results. If the COMPUTED field's value is numeric, the entire result is displayed regardless of the requested length.
The COMPUTEDtype field can be a very useful tool. Having set up such a field, you can then search or sort by it, and also include it in the definition of other COMPUTEDtype fields. In the latter case, independence is preserved. Thus, for example, if you define COMPUTED Field #2 in terms of COMPUTED Field #1 and then decide to redefine Field #1, Field #2 will automatically use the new Field #1 calculation. If you try to delete a field that is referenced by a COMPUTEDtype field, you will be warned.
Reviewed/Updated: March 4, 2007