0% found this document useful (0 votes)
17 views780 pages

TIB WFWF 8207.27.0 Functions

The document is a manual for TIBCO WebFOCUS® Release 8207, detailing the use of functions within the software. It includes sections on how to access and call functions, types of functions available, and specific examples of analytic functions. The manual serves as a comprehensive guide for users to effectively utilize various functions in their data analysis tasks.

Uploaded by

Nandhininathan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views780 pages

TIB WFWF 8207.27.0 Functions

The document is a manual for TIBCO WebFOCUS® Release 8207, detailing the use of functions within the software. It includes sections on how to access and call functions, types of functions available, and specific examples of analytic functions. The manual serves as a comprehensive guide for users to effectively utilize various functions in their data analysis tasks.

Uploaded by

Nandhininathan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 780

TIBCO WebFOCUS®

Using Functions

Release 8207
March 2021
DN4501670.0321

Copyright © 2021. TIBCO Software Inc. All Rights Reserved.


Contents
1. How to Use This Manual . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Available Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Operating Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
2. Introducing Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .19
Using Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Types of Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .20
WebFOCUS-specific Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Simplified Analytic Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Simplified Character Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .23
Character Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Variable Length Character Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Character Functions for DBCS Code Pages. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Maintain-specific Character Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .30
Data Source and Decoding Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Simplified Date and Date-Time Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Date Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Standard Date Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Legacy Date Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Date-Time Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .36
Maintain-specific Date and Time Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
Maintain-specific Standard Date and Time Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . .38
Maintain-specific Legacy Date Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Simplified Conversion Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Format Conversion Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
Maintain-specific Light Update Support Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .42
Simplified Numeric Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
Numeric Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
Maintain-specific Script Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Simplified Statistical Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Machine Learning (Python-based) Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Simplified System Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .46
System Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47

Using Functions 3
Contents

Simplified Geography Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49


Character Chart for ASCII and EBCDIC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50

3. Accessing and Calling a Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59


Calling a Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
Supplying an Argument in a Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Argument Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Argument Formats. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
Argument Length. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .63
Number and Order of Arguments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
Verifying Function Parameters. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
Calling a Function From a DEFINE, COMPUTE, or VALIDATE Command . . . . . . . . . . . . . . . . . . . . . . .67
Calling a Function From a Dialogue Manager Command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
Assigning the Result of a Function to a Variable. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
Branching Based on the Result of a Function. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
Calling a Function From an Operating System RUN Command. . . . . . . . . . . . . . . . . . . . . . . . . 72
Calling a Function From Another Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
Calling a Function in WHERE or IF Criteria . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
Using a Calculation or Compound IF Command. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .75
Calling a Function in WHEN Criteria . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
Calling a Function From a RECAP Command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
Storing and Accessing an External Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
Storing and Accessing a Function on z/OS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .78
Storing and Accessing a Function on UNIX. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
Storing and Accessing a Function on Windows. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79

4. Simplified Analytic Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81


FORECAST_MOVAVE: Using a Simple Moving Average . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .81
FORECAST_EXPAVE: Using Single Exponential Smoothing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
FORECAST_DOUBLEXP: Using Double Exponential Smoothing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
FORECAST_SEASONAL: Using Triple Exponential Smoothing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
FORECAST_LINEAR: Using a Linear Regression Equation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
PARTITION_AGGR: Creating Rolling Calculations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
PARTITION_REF: Using Prior or Subsequent Field Values in Calculations . . . . . . . . . . . . . . . . . . . .110

4
Contents

INCREASE: Calculating the Difference Between the Current and a Prior Value of a Field . . . . . . 114
PCT_INCREASE: Calculating the Percentage Difference Between the Current and a Prior Value
of a Field . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
PREVIOUS: Retrieving a Prior Value of a Field . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .121
RUNNING_AVE: Calculating an Average Over a Group of Rows . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
RUNNING_MAX: Calculating a Maximum Over a Group of Rows . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
RUNNING_MIN: Calculating a Minimum Over a Group of Rows . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
RUNNING_SUM: Calculating a Sum Over a Group of Rows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .132

5. Simplified Character Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135


CHAR_LENGTH: Returning the Length in Characters of a String . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
CONCAT: Concatenating Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
DIFFERENCE: Measuring the Phonetic Similarity Between Character Strings . . . . . . . . . . . . . . . . 139
DIGITS: Converting a Number to a Character String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .142
GET_TOKEN: Extracting a Token Based on a String of Delimiters . . . . . . . . . . . . . . . . . . . . . . . . . . 144
INITCAP: Capitalizing the First Letter of Each Word in a String . . . . . . . . . . . . . . . . . . . . . . . . . . . . .145
LAST_NONBLANK: Retrieving the Last Field Value That is Neither Blank nor Missing . . . . . . . . . 146
LEFT: Returning Characters From the Left of a Character String . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
LOWER: Returning a String With All Letters Lowercase . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
LPAD: Left-Padding a Character String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
LTRIM: Removing Blanks From the Left End of a String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
OVERLAY: Replacing Characters in a String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
PATTERNS: Returning a Pattern That Represents the Structure of the Input String . . . . . . . . . . . 156
POSITION: Returning the First Position of a Substring in a Source String . . . . . . . . . . . . . . . . . . . 158
POSITION: Returning the Position of a Search String in a Source String . . . . . . . . . . . . . . . . . . . . 160
Regular Expression Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
Using Regular Expressions on z/OS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
REGEX: Matching a String to a Regular Expression. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
REGEXP_COUNT: Counting the Number of Matches to a Pattern in a String. . . . . . . . . . . . 165
REGEXP_INSTR: Returning the First Position of a Pattern in a String. . . . . . . . . . . . . . . . . . .169
REGEXP_REPLACE: Replacing All Matches to a Pattern in a String. . . . . . . . . . . . . . . . . . . . 172
REGEXP_SUBSTR: Returning the First Match to a Pattern in a String. . . . . . . . . . . . . . . . . . 174
REPEAT: Repeating a String a Given Number of Times . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176

Using Functions 5
Contents

REPLACE: Replacing a String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .177


RIGHT: Returning Characters From the Right of a Character String . . . . . . . . . . . . . . . . . . . . . . . . .179
RPAD: Right-Padding a Character String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .181
RTRIM: Removing Blanks From the Right End of a String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
SPACE: Returning a String With a Given Number of Spaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .184
SPLIT: Extracting an Element From a String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
SUBSTRING: Extracting a Substring From a Source String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
TOKEN: Extracting a Token From a String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
TRIM_: Removing a Leading Character, Trailing Character, or Both From a String . . . . . . . . . . . . 190
UPPER: Returning a String With All Letters Uppercase . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193

6. Character Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195


Character Function Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .196
ARGLEN: Measuring the Length of a String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
ASIS: Distinguishing Between Space and Zero . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .197
BITSON: Determining If a Bit Is On or Off . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
BITVAL: Evaluating a Bit String as an Integer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
BYTVAL: Translating a Character to Decimal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .202
CHKFMT: Checking the Format of a String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .204
CHKNUM: Checking a String for Numeric Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
CTRAN: Translating One Character to Another . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
CTRFLD: Centering a Character String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
EDIT: Extracting or Adding Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
GETTOK: Extracting a Substring (Token) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
LCWORD: Converting a String to Mixed-Case . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
LCWORD2: Converting a String to Mixed-Case . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
LCWORD3: Converting a String to Mixed-Case . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
LJUST: Left-Justifying a String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
LOCASE: Converting Text to Lowercase . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
OVRLAY: Overlaying a Character String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
PARAG: Dividing Text Into Smaller Lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
PATTERN: Generating a Pattern From a String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
POSIT: Finding the Beginning of a Substring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227

6
Contents

REVERSE: Reversing the Characters in a String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229


RJUST: Right-Justifying a Character String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .230
SOUNDEX: Comparing Character Strings Phonetically . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
SPELLNM: Spelling Out a Dollar Amount . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
SQUEEZ: Reducing Multiple Spaces to a Single Space . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234
STRIP: Removing a Character From a String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
STRREP: Replacing Character Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
SUBSTR: Extracting a Substring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239
TRIM: Removing Leading and Trailing Occurrences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
UPCASE: Converting Text to Uppercase . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244
XMLDECOD: Decoding XML-Encoded Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
XMLENCOD: XML-Encoding Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247

7. Variable Length Character Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251


Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
LENV: Returning the Length of an Alphanumeric Field . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .252
LOCASV: Creating a Variable Length Lowercase String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
POSITV: Finding the Beginning of a Variable Length Substring . . . . . . . . . . . . . . . . . . . . . . . . . . . . .254
SUBSTV: Extracting a Variable Length Substring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
TRIMV: Removing Characters From a String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258
UPCASV: Creating a Variable Length Uppercase String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260

8. Character Functions for DBCS Code Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .263


DCTRAN: Translating A Single-Byte or Double-Byte Character to Another . . . . . . . . . . . . . . . . . . . . 263
DEDIT: Extracting or Adding Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264
DSTRIP: Removing a Single-Byte or Double-Byte Character From a String . . . . . . . . . . . . . . . . . . . 266
DSUBSTR: Extracting a Substring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
JPTRANS: Converting Japanese Specific Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268
KKFCUT: Truncating a String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
SFTDEL: Deleting the Shift Code From DBCS Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274
SFTINS: Inserting the Shift Code Into DBCS Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276

9. Maintain-specific Character Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279


CHAR2INT: Translating a Character Into an Integer Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
INT2CHAR: Translating an Integer Value Into a Character . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280

Using Functions 7
Contents

LCWORD and LCWORD2: Converting a Character String to Mixed-Case . . . . . . . . . . . . . . . . . . . . . 281


LENGTH: Determining the Length of a Character String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282
LJUST: Left-Justifying a Character String (Maintain) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .283
LOWER: Converting a Character String to Lowercase . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
MASK: Extracting or Adding Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284
MNTGETTOK: Extracting Tokens From a String Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285
NLSCHR: Converting Characters From the Native English Code Page . . . . . . . . . . . . . . . . . . . . . . .288
OVRLAY: Overlaying a Character String (Maintain) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289
POSIT: Finding the Beginning of a Substring (Maintain) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290
RJUST: Right-Justifying a Character String (Maintain) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291
SELECTS: Decoding a Value From a Stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292
STRAN: Substituting One Substring for Another . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293
STRCMP: Comparing Character Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295
STRICMP: Comparing Character Strings and Ignoring Case . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296
STRNCMP: Comparing Character Substrings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297
STRTOKEN: Extracting a Substring Based on Delimiters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .297
SUBSTR: Extracting a Substring (Maintain) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
TRIM: Removing Trailing Occurrences (Maintain) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300
TRIMLEN: Determining the Length of a String Excluding Trailing Spaces . . . . . . . . . . . . . . . . . . . . 300
UPCASE: Converting Text to Uppercase (Maintain) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301

10. Data Source and Decoding Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .303


CHECKMD5: Computing an MD5 Hash Check Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304
CHECKSUM: Computing a Hash Sum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .305
COALESCE: Returning the First Non-Missing Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306
DB_EXPR: Inserting an SQL Expression Into a Request . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308
DB_INFILE: Testing Values Against a File or an SQL Subquery . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310
DB_LOOKUP: Retrieving Data Source Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317
DECODE: Decoding Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .320
FIND: Verifying the Existence of a Value in a Data Source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324
IMPUTE: Replacing Missing Values With Aggregated Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326
LAST: Retrieving the Preceding Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .331
LOOKUP: Retrieving a Value From a Cross-referenced Data Source . . . . . . . . . . . . . . . . . . . . . . . . 333

8
Contents

Using the Extended LOOKUP Function. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337


NULLIF: Returning a Null Value When Parameters Are Equal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338

11. Simplified Date and Date-Time Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341


DAYNAME: Returning the Name of the Day From a Date Expression . . . . . . . . . . . . . . . . . . . . . . . 342
DT_CURRENT_DATE: Returning the Current Date . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343
DT_CURRENT_DATETIME: Returning the Current Date and Time . . . . . . . . . . . . . . . . . . . . . . . . . . . 343
DT_CURRENT_TIME: Returning the Current Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344
DT_TOLOCAL: Converting Universal Coordinated Time to Local Time . . . . . . . . . . . . . . . . . . . . . . . 345
DT_TOUTC: Converting Local Time to Universal Coordinated Time . . . . . . . . . . . . . . . . . . . . . . . . . 347
DTADD: Incrementing a Date or Date-Time Component . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351
DTDIFF: Returning the Number of Component Boundaries Between Date or Date-Time Values . 354
DTIME: Extracting Time Components From a Date-Time Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . .355
DTPART: Returning a Date or Date-Time Component in Integer Format . . . . . . . . . . . . . . . . . . . . . 357
DTRUNC: Returning the Start of a Date Period for a Given Date . . . . . . . . . . . . . . . . . . . . . . . . . . . 359
MONTHNAME: Returning the Name of the Month From a Date Expression . . . . . . . . . . . . . . . . . . 362

12. Date Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .365


Overview of Date Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366
Using Standard Date Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .367
Specifying Work Days. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367
Specifying Business Days. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .367
Specifying Holidays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368
Enabling Leading Zeros For Date and Time Functions in Dialogue Manager. . . . . . . . . . . . .373
DATEADD: Adding or Subtracting a Date Unit to or From a Date . . . . . . . . . . . . . . . . . . . . . . . . . . . 375
DATECVT: Converting the Format of a Date . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .378
DATEDIF: Finding the Difference Between Two Dates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 380
DATEMOV: Moving a Date to a Significant Point . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .383
DATETRAN: Formatting Dates in International Formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390
DPART: Extracting a Component From a Date . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .406
FIQTR: Obtaining the Financial Quarter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 408
FIYR: Obtaining the Financial Year . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 410
FIYYQ: Converting a Calendar Date to a Financial Date . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .412
TODAY: Returning the Current Date . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415

Using Functions 9
Contents

Using Legacy Date Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .416


Using Old Versions of Legacy Date Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417
Using Dates With Two- and Four-Digit Years. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417
AYM: Adding or Subtracting Months . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 419
AYMD: Adding or Subtracting Days . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420
CHGDAT: Changing How a Date String Displays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 421
DA Functions: Converting a Legacy Date to an Integer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 424
DMY, MDY, YMD: Calculating the Difference Between Two Dates . . . . . . . . . . . . . . . . . . . . . . . . . 426
DOWK and DOWKL: Finding the Day of the Week . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .427
DT Functions: Converting an Integer to a Date . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 428
GREGDT: Converting From Julian to Gregorian Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .430
JULDAT: Converting From Gregorian to Julian Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 432
YM: Calculating Elapsed Months . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433

13. Date-Time Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435


Using Date-Time Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 436
Date-Time Parameters. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .436
Specifying the Order of Date Components. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .436
Specifying the First Day of the Week for Use in Date-Time Functions. . . . . . . . . . . . . 437
Controlling Processing of Date-Time Values. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 438
Supplying Arguments for Date-Time Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .439
Using Date-Time Formats. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 440
Numeric String Format. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 441
Formatted-string Format. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 441
Translated-string Format. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 442
Time Format. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 442
Assigning Date-Time Values. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443
HADD: Incrementing a Date-Time Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .446
HCNVRT: Converting a Date-Time Value to Alphanumeric Format . . . . . . . . . . . . . . . . . . . . . . . . . . 449
HDATE: Converting the Date Portion of a Date-Time Value to a Date Format . . . . . . . . . . . . . . . . 450
HDIFF: Finding the Number of Units Between Two Date-Time Values . . . . . . . . . . . . . . . . . . . . . . . 452
HDTTM: Converting a Date Value to a Date-Time Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453

10
Contents

HEXTR: Extracting Components of a Date-Time Value and Setting Remaining Components to


Zero . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 455
HGETC: Storing the Current Local Date and Time in a Date-Time Field . . . . . . . . . . . . . . . . . . . . . .457
HGETZ: Storing the Current Coordinated Universal Time in a Date-Time Field . . . . . . . . . . . . . . . .458
HHMMSS: Retrieving the Current Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 460
HHMS: Converting a Date-Time Value to a Time Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 461
HINPUT: Converting an Alphanumeric String to a Date-Time Value . . . . . . . . . . . . . . . . . . . . . . . . . 462
HMIDNT: Setting the Time Portion of a Date-Time Value to Midnight . . . . . . . . . . . . . . . . . . . . . . . 464
HMASK: Extracting Date-Time Components and Preserving Remaining Components . . . . . . . . . .465
HNAME: Retrieving a Date-Time Component in Alphanumeric Format . . . . . . . . . . . . . . . . . . . . . . .467
HPART: Retrieving a Date-Time Component as a Numeric Value . . . . . . . . . . . . . . . . . . . . . . . . . . . 469
HSETPT: Inserting a Component Into a Date-Time Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 471
HTIME: Converting the Time Portion of a Date-Time Value to a Number . . . . . . . . . . . . . . . . . . . . .473
HTMTOTS or TIMETOTS: Converting a Time to a Timestamp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474
HYYWD: Returning the Year and Week Number From a Date-Time Value . . . . . . . . . . . . . . . . . . . .476

14. Maintain-specific Date and Time Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 479


Maintain-specific Standard Date and Time Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 479
HHMMSS: Retrieving the Current Time (Maintain). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 479
Initial_HHMMSS: Returning the Time the Application Was Started. . . . . . . . . . . . . . . . . . . . 480
Initial_TODAY: Returning the Date the Application Was Started. . . . . . . . . . . . . . . . . . . . . . . 480
TODAY: Retrieving the Current Date (Maintain). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 480
TODAY2: Returning the Current Date. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 481
ADD: Adding Days to a Date. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .482
DAY: Extracting the Day of the Month From a Date. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 483
JULIAN: Determining How Many Days Have Elapsed in the Year. . . . . . . . . . . . . . . . . . . . . . 483
MONTH: Extracting the Month From a Date. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 484
QUARTER: Determining the Quarter. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .485
SETMDY: Setting the Value to a Date. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485
SUB: Subtracting a Value From a Date. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 486
WEEKDAY: Determining the Day of the Week for a Date. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 487
YEAR: Extracting the Year From a Date. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .488

15. Simplified Conversion Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 489

Using Functions 11
Contents

CHAR: Returning a Character Based on a Numeric Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 489


COMPACTFORMAT: Displaying Numbers in an Abbreviated Format . . . . . . . . . . . . . . . . . . . . . . . . . 490
CTRLCHAR: Returning a Non-Printable Control Character . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 492
DT_FORMAT: Converting a Date or Date-Time Value to an Alphanumeric String . . . . . . . . . . . . . . 494
FPRINT: Displaying a Value in a Specified Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 495
HEXTYPE: Returning the Hexadecimal View of an Input Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 497
PHONETIC: Returning a Phonetic Key for a String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 499
TO_INTEGER: Converting a Character String to an Integer Value . . . . . . . . . . . . . . . . . . . . . . . . . . . 501
TO_NUMBER: Converting a Character String to a Numeric Value . . . . . . . . . . . . . . . . . . . . . . . . . . .502

16. Format Conversion Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 503


ATODBL: Converting an Alphanumeric String to Double-Precision Format . . . . . . . . . . . . . . . . . . . 503
EDIT: Converting the Format of a Field . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .505
FPRINT: Converting Fields to Alphanumeric Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 506
FTOA: Converting a Number to Alphanumeric Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 511
HEXBYT: Converting a Decimal Integer to a Character . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 512
ITONUM: Converting a Large Binary Integer to Double-Precision Format . . . . . . . . . . . . . . . . . . . . 515
ITOPACK: Converting a Large Binary Integer to Packed-Decimal Format . . . . . . . . . . . . . . . . . . . . . 516
ITOZ: Converting a Number to Zoned Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 518
PCKOUT: Writing a Packed Number of Variable Length . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 519
PTOA: Converting a Packed-Decimal Number to Alphanumeric Format . . . . . . . . . . . . . . . . . . . . . . 520
TSTOPACK: Converting an MSSQL or Sybase Timestamp Column to Packed Decimal . . . . . . . . .522
UFMT: Converting an Alphanumeric String to Hexadecimal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 524
XTPACK: Writing a Packed Number With Up to 31 Significant Digits to an Output File . . . . . . . . .525

17. Maintain-specific Light Update Support Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . .529


IWC.FindAppCGIValue: Retrieving a WebFOCUS Parameter or Variable Value . . . . . . . . . . . . . . . . 529
IWC.GetAppCGIValue: Importing a WebFOCUS Parameter or Variable . . . . . . . . . . . . . . . . . . . . . . 530

18. Simplified Numeric Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 533


ASCII: Returning the ASCII Code for the Leftmost Character in a String . . . . . . . . . . . . . . . . . . . . .534
CEILING: Returning the Smallest Integer Value Greater Than or Equal to a Value . . . . . . . . . . . . 534
EXPONENT: Raising e to a Power . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 536
FLOOR: Returning the Largest Integer Less Than or Equal to a Value . . . . . . . . . . . . . . . . . . . . . . 537
LOG10: Calculating the Base 10 Logarithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 538

12
Contents

MOD: Calculating the Remainder From a Division . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 539


POWER: Raising a Value to a Power . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .541
ROUND: Rounding a Number to a Given Number of Decimal Places . . . . . . . . . . . . . . . . . . . . . . . .542
SIGN: Returning the Sign of a Number . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .543
TRUNCATE: Truncating a Number to a Given Number of Decimal Places . . . . . . . . . . . . . . . . . . . . 544

19. Numeric Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 547


ABS: Calculating Absolute Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .548
ASIS: Distinguishing Between a Blank and a Zero . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 548
BAR: Producing a Bar Chart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 549
CHKPCK: Validating a Packed Field . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 551
DMOD, FMOD, and IMOD: Calculating the Remainder From a Division . . . . . . . . . . . . . . . . . . . . . .553
EXP: Raising e to the Nth Power . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 555
EXPN: Evaluating a Number in Scientific Notation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 556
FMLCAP: Retrieving FML Hierarchy Captions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .557
FMLFOR: Retrieving FML Tag Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 558
FMLINFO: Returning FOR Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 559
FMLLIST: Returning an FML Tag List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 561
INT: Finding the Greatest Integer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 562
LOG: Calculating the Natural Logarithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .562
MAX and MIN: Finding the Maximum or Minimum Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 563
MIRR: Calculating the Modified Internal Return Rate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .564
NORMSDST and NORMSINV: Calculating Normal Distributions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 568
NORMSDST: Calculating Standard Cumulative Normal Distribution. . . . . . . . . . . . . . . . . . . . 568
NORMSINV: Calculating Inverse Cumulative Normal Distribution. . . . . . . . . . . . . . . . . . . . . . 571
PRDNOR and PRDUNI: Generating Reproducible Random Numbers . . . . . . . . . . . . . . . . . . . . . . . . 572
RDNORM and RDUNIF: Generating Random Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .575
SQRT: Calculating the Square Root . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 576
XIRR: Calculating the Modified Internal Return Rate (Periodic or Non-Periodic) . . . . . . . . . . . . . . .577

20. Maintain-specific Script Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 581


IWCLink: Displaying a URL in a Browser or Frame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 581
IWCSwitchToSecure and IWCSwitchToUnsecure: Turning the Secure Sockets Layer On and
Off . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 583

Using Functions 13
Contents

IWCTrigger: Calling a Maintain Function From a Script Handler . . . . . . . . . . . . . . . . . . . . . . . . . . . . 584


IWC.FindAppCGIValue: Finding a WebFOCUS Parameter or Variable Value . . . . . . . . . . . . . . . . . . 585
IWC.GetAppCGIValue: Retrieving a WebFOCUS Parameter or Variable . . . . . . . . . . . . . . . . . . . . . . 587
21. Simplified Statistical Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .589
Specify the Partition Size for Simplified Statistical Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 589
CORRELATION: Calculating the Degree of Correlation Between Two Sets of Data . . . . . . . . . . . . 590
KMEANS_CLUSTER: Partitioning Observations Into Clusters Based on the Nearest Mean
Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 591
MULTIREGRESS: Creating a Multivariate Linear Regression Column . . . . . . . . . . . . . . . . . . . . . . . 594
OUTLIER: Identifying Outliers in Numeric Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 596
RSERVE: Running an R Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 598
STDDEV: Calculating the Standard Deviation for a Set of Data Values . . . . . . . . . . . . . . . . . . . . . .603
22. Machine Learning (Python-based) Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 605
ANOMALY_IF: Detecting Outliers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 606
CLASSIFY_BLR: Binary Logistic Regression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .609
CLASSIFY_KNN: K-Nearest Neighbors Classification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 613
CLASSIFY_RF: Random Forest Classification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 615
CLASSIFY_XGB: Extreme Gradient Boosting Classification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 619
REGRESS_KNN: K-Nearest Neighbors Regression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 622
REGRESS_POLY: Polynomial Regression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .624
REGRESS_RF: Random Forest Regression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 628
REGRESS_XGB: Extreme Gradient Boosting Regression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .631
RUN_MODEL and RUN_MODEL2: Running a Saved Python Model . . . . . . . . . . . . . . . . . . . . . . . . . 634
23. Simplified System Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 639
EDAPRINT: Inserting a Custom Message in the EDAPRINT Log File . . . . . . . . . . . . . . . . . . . . . . . . 639
ENCRYPT: Encrypting a Password . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 640
GETENV: Retrieving the Value of an Environment Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 641
PUTENV: Assigning a Value to an Environment Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 641
SLACK: Posting a Message to a Slack Channel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 642
24. System Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 645
CHECKPRIVS: Retrieving the Privilege State for the Connected User . . . . . . . . . . . . . . . . . . . . . . . 646
CLSDDREC: Closing All Files Opened by the PUTDDREC Function . . . . . . . . . . . . . . . . . . . . . . . . . .646

14
Contents

FEXERR: Retrieving an Error Message . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 647


FGETENV: Retrieving the Value of an Environment Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 648
FINDMEM: Finding a Member of a Partitioned Data Set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 649
FPUTENV: Assigning a Value to an Environment Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 650
GETCOOKI: Retrieving a Browser Cookie Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .652
GETHEADR: Retrieving an HTTP Header Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 653
GETPDS: Determining If a Member of a Partitioned Data Set Exists . . . . . . . . . . . . . . . . . . . . . . . .654
GETUSER: Retrieving a User ID . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 656
GRPLIST: Retrieving the Group List of the Connected User . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 657
JOBNAME: Retrieving the Current Process Identification String . . . . . . . . . . . . . . . . . . . . . . . . . . . . 658
MVSDYNAM: Passing a DYNAM Command to the Command Processor . . . . . . . . . . . . . . . . . . . . 659
PUTCOOKI: Submitting a Value to a Browser Cookie . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 660
PUTDDREC: Writing a Character String as a Record in a Sequential File . . . . . . . . . . . . . . . . . . . . 661
SLEEP: Suspending Execution for a Given Number of Seconds . . . . . . . . . . . . . . . . . . . . . . . . . . . . 664
SPAWN: Creating a Subprocess From a Procedure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 665
SYSTEM: Calling a System Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 666
SYSVAR: Retrieving the Value of a z/OS System Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 668

25. Simplified Geography Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 671


Sample Geography Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .672
GIS_DISTANCE: Calculating the Distance Between Geometry Points . . . . . . . . . . . . . . . . . . . . . . . 676
GIS_DRIVE_ROUTE: Calculating the Driving Directions Between Geometry Points . . . . . . . . . . . . 678
GIS_GEOCODE_ADDR: Geocoding a Complete Address . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 682
GIS_GEOCODE_ADDR_CITY: Geocoding an Address Line, City, and State . . . . . . . . . . . . . . . . . . . 683
GIS_GEOCODE_ADDR_POSTAL: Geocoding an Address Line and Postal Code . . . . . . . . . . . . . . . 685
GIS_GEOMETRY: Building a JSON Geometry Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .686
GIS_IN_POLYGON: Determining if a Point is in a Complex Polygon . . . . . . . . . . . . . . . . . . . . . . . . . 690
GIS_LINE: Building a JSON Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 691
GIS_POINT: Building a Geometry Point . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .695
GIS_REVERSE_COORDINATE: Returning a Geographic Component . . . . . . . . . . . . . . . . . . . . . . . . . 698
GIS_SERVICE_AREA: Calculating a Geometry Area Around a Given Point . . . . . . . . . . . . . . . . . . . . 700
GIS_SERV_AREA_XY: Calculating a Service Area Around a Given Coordinate . . . . . . . . . . . . . . . . 704

26. SQL Character Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 711

Using Functions 15
Contents

LOCATE: Returning the Position of a Substring in a String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 711

27. SQL Miscellaneous Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 713


CHR: Returning the ASCII Character Given a Numeric Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 713

A. Creating a Subroutine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 715


Writing a Subroutine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 715
Naming a Subroutine. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 717
Creating Arguments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 717
Language Considerations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 718
Programming a Subroutine. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 721
Executing a Subroutine at an Entry Point. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 722
Including More Than 200 Arguments in a Subroutine Call. . . . . . . . . . . . . . . . . . . . . . 723
Compiling and Storing a Subroutine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 726
Compiling and Storing a Subroutine on z/OS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 727
Compiling and Storing a Subroutine on UNIX. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .727
Compiling and Storing a Subroutine on Windows. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 727
Testing the Subroutine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 727
Using a Custom Subroutine: The MTHNAM Subroutine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .728
Writing the MTHNAM Subroutine. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 728
Calling the MTHNAM Subroutine From a Request. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 734
Subroutines Written in REXX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 735
Formats and REXX Subroutines. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 740

B. ASCII and EBCDIC Codes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 747


ASCII and EBCDIC Code Chart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .747

Legal and Third-Party Notices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 761

16
Chapter 1 How to Use This Manual
This manual describes the functions supplied with your Information Builders product. It is
intended for application developers who call these functions from their programs to
perform calculations or manipulate data. Other users who access corporate data to
produce reports can call these functions.

This manual also explains how to create functions tailored to individual needs (called
subroutines) for use with your Information Builders product.

In this chapter:

Available Languages

Operating Systems

Available Languages
A function is available in the reporting language, the Maintain language, or both:

The reporting language includes all commands used to create a report. It is available to
users of any Information Builders product.

The Maintain language includes all commands used to maintain data sources with the
Maintain product. It is available only to those who purchased Maintain.

Look in the description of an individual function for the available language, or in the
categorized list of functions in Introducing Functions on page 19.

Operating Systems
Except in cases noted specifically, all functions run on all server-supported operating systems.

Using Functions 17
Operating Systems

18
Chapter 2 Introducing Functions
The following topics offer an introduction to functions and explain the different types of
functions available.
In this chapter:

Using Functions

Types of Functions

Character Chart for ASCII and EBCDIC

Using Functions
Functions operate on one or more arguments and return a single value. The returned value can
be stored in a field, assigned to a Dialogue Manager variable, used in a calculation or other
processing, or used in a selection or validation test. Functions provide a convenient way to
perform certain calculations and manipulations.

There are three types of functions:

Internal functions. Built into the WebFOCUS language, requiring no extra work to access or
use. The following functions are internal functions. You cannot replace any of these internal
functions with your own functions of the same name. All other functions are external.

ABS

ASIS

DMY, MDY, and YMD

DECODE

EDIT

FIND

LAST

LOG

LOOKUP

Using Functions 19
Types of Functions

MAX and MIN

SQRT

All Maintain-specific functions

External functions. Stored in an external library that must be accessed. When invoking
these functions, an argument specifying the output field or format of the result is required.
External functions are distributed with WebFOCUS. You can replace these functions with
your own functions of the same name. However, in this case, you must set
USERFNS=LOCAL.

Subroutines. Written by the user and stored externally. For details, see Creating a
Subroutine on page 715.

For information on how to use an internal or external function, see Accessing and Calling a
Function on page 59.

Types of Functions
You can access any of the following types of functions:

Simplified analytic functions. Perform calculations using multiple rows in the internal
matrix. For details, see Simplified Analytic Functions on page 22.

Simplified character functions. Character functions with streamlined parameter lists and
no output arguments, similar to those used by SQL functions. For details, see Simplified
Character Functions on page 23.

Character functions. Manipulate alphanumeric fields or character strings. For details, see
Character Functions on page 25.

Variable length character functions. Manipulate AnV fields or character strings. For details,
see Variable Length Character Functions on page 28.

Character functions for DBCS code pages. Manipulate alphanumeric fields or character
strings on DBCS code pages. For details, see Character Functions for DBCS Code Pages on
page 29.

Maintain-specific character functions. Manipulate alphanumeric fields or character strings.


These functions are available only in Maintain Data. For details, see Maintain-specific
Character Functions on page 30.

Data source and decoding functions. Search for or retrieve data source records or values,
and assign values. For details, see Data Source and Decoding Functions on page 31.

20
2. Introducing Functions

Simplified date and date-time functions. Date and date-time functions with streamlined
parameter lists and no output arguments, similar to those used by SQL functions. For
details, see Simplified Date and Date-Time Functions on page 33.

Date functions. Manipulate dates. For details, see Date Functions on page 33.

Date-time functions. Manipulate date-time values. For details, see Date-Time Functions on
page 36.

Maintain-specific date and time functions. Manipulate dates and times. These functions
are available only in Maintain Data. For details, see Maintain-specific Date and Time
Functions on page 38.

Simplified conversion functions. Convert fields from one format to another using
streamlined parameter lists. For details, see Simplified Conversion Functions on page 39.

Format conversion functions. Convert fields from one format to another. For details, see
Format Conversion Functions on page 40.

Maintain-specific Light Update Support functions. Retrieve WebFOCUS variable or


parameter data implicitly from within a Maintain procedure. These functions are available
only in Maintain Data. For details, see Maintain-specific Light Update Support Functions on
page 42.

Simplified numeric functions. Perform calculations on numeric constants and fields using
streamlined parameter lists. For details, see Simplified Numeric Functions on page 42.

Numeric functions. Perform calculations on numeric constants and fields. For details, see
Numeric Functions on page 42.

Maintain-specific Script functions. Integrate JavaScript and VBScripts into your Maintain
Data application and perform client-side execution without returning to the WebFOCUS
Server. These functions are available only in Maintain Data. For details, see Maintain-
specific Script Functions on page 45.

Simplified statistical functions. Perform statistical calculations. For details, see Simplified
Statistical Functions on page 45.

Machine Leaning (Python-based) Functions. Run Python scripts that perform classification
and regression. For details, see Machine Learning (Python-based) Functions on page 45.

Simplified system functions. Call the operating system to obtain information about the
operating environment or to use a system service, using streamlined parameter lists. For
details, see Simplified System Functions on page 46.

Using Functions 21
Types of Functions

System functions. Call the operating system to obtain information about the operating
environment or to use a system service. For details, see System Functions on page 47.

Simplified Geography Functions. Perform location-based calculations and retrieve


geocoded points for various types of location data. For details, see Simplified Geography
Functions on page 49

WebFOCUS-specific Functions
Most Information Builders-supplied functions are available in both WebFOCUS and FOCUS.
However, some functions are available only in WebFOCUS. They are:

SPAWN

SYSTEM

For details on these functions, see the individual topics.

Simplified Analytic Functions


The following functions perform calculations based on multiple rows in the internal matrix. For
details, see Simplified Analytic Functions on page 81.
FORECAST_MOVAVE
Calculates a simple moving average column.
FORECAST_EXPAVE
Calculates a single exponential smoothing column.
FORECAST_DOUBLEXP
Calculates a double exponential smoothing column.
FORECAST_SEASONAL
Calculates a triple exponential smoothing column.
FORECAST_LINEAR
Calculates a linear regression column.
PARTITION_AGGR
Creates rolling calculations.
PARTITION_REF
Retrieves prior or subsequent fields.
INCREASE
Calculates the difference between a value in the current row and a prior row within a
partition.

22
2. Introducing Functions

PCT_INCREASE
Calculates the percent difference between a value in the current row and a prior row within
a partition.
PREVIOUS
Retrieves a prior value within a partition.
RUNNING_AVE
Calculate the average over a group of rows within a partition.
RUNNING_MIN
Calculate the minimum over a group of rows within a partition.
RUNNING_MAX
Calculate the maximum over a group of rows within a partition.
RUNNING_SUM
Calculate the sum over a group of rows within a partition.

Simplified Character Functions


The following functions manipulate alphanumeric fields or character strings and have simplified
parameter lists. For details, see Simplified Character Functions on page 135.
CHAR_LENGTH
Returns the length, in characters, of a string.

Available Languages: reporting


DIGITS
Converts a number to a character string of the specified length.

Available Languages: reporting


GET_TOKEN
Extracts a token (substring) based on a token number and a string listing acceptable
delimiter characters.

Available Languages: reporting


INITCAP
Capitalizes the first letter of every word in a string and makes all other letters lowercase,
where a word starts at the beginning of the string, after a blank space, or after a special
character.

Available Languages: reporting

Using Functions 23
Types of Functions

LAST_NONBLANK
retrieves the last field value that is neither blank nor missing. If all previous values are
either blank or missing, returns a missing value.
LOWER
Translates a string to lowercase.

Available Languages: reporting


LPAD
Left-pads a string with a given character.

Available Languages: reporting


LTRIM
Removes all blanks from the left end of a string.

Available Languages: reporting


PATTERNS
Returns a pattern that represents the structure of the source string.

Available Languages: reporting


POSITION
Returns the first position (in characters) of a substring in a source string.

Available Languages: reporting


REGEX
Matches a string to a regular expression and returns true (1) or false (0).
RPAD
Right-pads a string with a given character.

Available Languages: reporting


RTRIM
Removes all blanks from the right end of a string.

Available Languages: reporting


SUBSTRING
Extracts a substring from a source string.

Available Languages: reporting

24
2. Introducing Functions

TOKEN
Extracts a token (substring) based on a token number and a delimiter string.

Available Languages: reporting


TRIM_
Removes all occurrences of a single character from either the beginning or end of a string,
or both.

Available Languages: reporting


UPPER
Translates a string to uppercase.

Available Languages: reporting

Character Functions
The following functions manipulate alphanumeric fields or character strings. For details, see
Character Functions on page 195.
ARGLEN
Measures the length of a character string within a field, excluding trailing blanks.

Available Languages: reporting, Maintain


ASIS
Distinguishes between a blank and a zero in Dialogue Manager.

Available Languages: reporting


BITSON
Evaluates an individual bit within a character string to determine whether it is on or off.

Available Languages: reporting, Maintain


BITVAL
Evaluates a string of bits within a character string and returns its value.

Available Languages: reporting, Maintain


BYTVAL
Translates a character to its corresponding ASCII or EBCDIC decimal value.

Available Languages: reporting, Maintain

Using Functions 25
Types of Functions

CHKFMT
Checks a character string for incorrect characters or character types.

Available Languages: reporting, Maintain


CTRAN
Translates a character within a character string to another character based on its decimal
value.

Available Languages: reporting, Maintain


CTRFLD
Centers a character string within a field.

Available Languages: reporting, Maintain


EDIT
Extracts characters from or adds characters to a character string.

Available Languages: reporting


GETTOK
Divides a character string into substrings, called tokens, where a specific character, called
a delimiter, occurs in the string.

Available Languages: reporting, Maintain


LCWORD
Converts the letters in a character string to mixed case.

Available Languages: reporting, Maintain


LCWORD2
Converts the letters in a character string to mixed case.

Available Languages: reporting, Maintain


LCWORD3
Converts the letters in a character string to mixed case.

Available Languages: reporting, Maintain


LJUST
Left-justifies a character string within a field.

Available Languages: reporting

26
2. Introducing Functions

LOCASE
Converts alphanumeric text to lowercase.

Available Languages: reporting, Maintain


OVRLAY
Overlays a base character string with a substring.

Available Languages: reporting


PARAG
Divides a line of text into smaller lines by marking them with a delimiter.

Available Languages: reporting, Maintain


POSIT
Finds the starting position of a substring within a larger string.

Available Languages: reporting


REVERSE
Reverses the characters in a character string.

Available Languages: reporting, Maintain


RJUST
Right-justifies a character string.

Available Languages: reporting


SOUNDEX
Searches for a character string phonetically without regard to spelling.

Available Languages: reporting, Maintain


SPELLNM
Takes an alphanumeric string or a numeric value with two decimal places and spells it out
with dollars and cents.

Available Languages: reporting, Maintain


SQUEEZ
Reduces multiple contiguous spaces within a character string to a single space.

Available Languages: reporting, Maintain

Using Functions 27
Types of Functions

STRIP
Removes all occurrences of a specific character from a string.

Available Languages: reporting, Maintain


STRREP
Replaces all occurrences of a specific character string.

Available Languages: reporting, Maintain


SUBSTR
Extracts a substring based on where it begins and its length in the parent string.

Available Languages: reporting


TRIM
Removes leading and/or trailing occurrences of a pattern within a character string.

Available Languages: reporting


UPCASE
Converts a character string to uppercase.

Available Languages: reporting

Variable Length Character Functions


The following functions manipulate variable length alphanumeric fields or character strings. For
details, see Variable Length Character Functions on page 251.
LENV
Returns the actual length of an AnV field or the size of an An field.

Available Languages: reporting


LOCASV
Converts alphanumeric text to lowercase in an AnV field.

Available Languages: reporting


POSITV
Finds the starting position of a substring in an AnV field.

Available Languages: reporting

28
2. Introducing Functions

SUBSTV
Extracts a substring based on where it begins and its length in the parent string in an AnV
field.

Available Languages: reporting


TRIMV
Removes leading and/or trailing occurrences of a pattern within a character string in an
AnV field.

Available Languages: reporting


UPCASV
Converts a character string to uppercase in an AnV field.

Available Languages: reporting

Character Functions for DBCS Code Pages


The following functions manipulate character strings for DBCS code pages. For details, see
Character Functions for DBCS Code Pages on page 263.
DCTRAN
Translates a single-byte or double-byte character to another character.
DEDIT
Extracts characters from or adds characters to a string.
DSTRIP
Removes a single-byte or double-byte character from a string.
DSUBSTR
Extracts a substring based on its length and position in the source string.
JPTRANS
Converts Japanese specific characters.

Using Functions 29
Types of Functions

Maintain-specific Character Functions


The following functions manipulate alphanumeric fields or character strings. They are available
only in the Maintain language. For details, see Maintain-specific Character Functions on page
279.
CHAR2INT
Translates an ASCII or EBCDIC character to the integer value it represents, depending on
the operating system.
INT2CHAR
Translates an integer into the equivalent ASCII or EBCDIC character, depending on the
operating system.
LCWORD and LCWORD2
Converts the letters in a character string to mixed case.
LENGTH
Measures the length of a character string, including trailing blanks.
LJUST
Left-justifies a character string within a field.
LOWER
Converts a character string to lowercase.
MASK
Extracts characters from or adds characters to a character string.
MNTGETTOK
Divides a character string into substrings, called tokens.
NLSCHR
Converts a character from the native English code page to the running code page.
OVRLAY
Overlays a base character string with a substring.
POSIT
Finds the starting position of a substring within a larger string.
RJUST
Right-justifies a character string.

30
2. Introducing Functions

SELECTS
Decodes a value from a stack.
STRAN
Substitutes a substring for another substring in a character string.
STRCMP
Compares two alphanumeric strings using the ASCII or EBCDIC collating sequence.
STRICMP
Compares two alphanumeric strings using the ASCII or EBCDIC collating sequence, but
ignoring case differences.
STRNCMP
Compares a specified number of characters in two character strings starting at the
beginning of the strings using the EBCDIC or ASCII collating sequence.
SUBSTR
Extracts a substring based on where it begins and its length in the parent string.
TRIM
Removes trailing occurrences of a pattern within a character string.
TRIMLEN
Determines the length of a character string excluding trailing spaces.
UPCASE
Converts a character string to uppercase.

Data Source and Decoding Functions


The following functions search for data source records, retrieve data source records or values,
and assign values. For details, see Data Source and Decoding Functions on page 303.
COALESCE
Returns the value of the first non-missing argument.

Available Languages: reporting


DB_EXPR
Inserts an SQL expression into the SQL generated for a request against a relational data
source.

Available Languages: reporting, MODIFY

Using Functions 31
Types of Functions

DB_INFILE
Compares values in a source file to values in a target file, or if the source file is a
relational data source, to values retrieved by a subquery.

Available Languages: reporting, MODIFY


CHECKMD5
Computes an MD5 hash check value of its input parameter.

Available Languages: reporting


CHECKSUM
Computes hash sum of its input parameter.

Available Languages: reporting


DB_LOOKUP
Retrieves a data value from a lookup data source.

Available Languages: reporting, MODIFY


DECODE
Assigns values based on the coded value of an input field.

Available Languages: reporting, Maintain


FIND
Determines if an incoming data value is in an indexed FOCUS data source field.

Available Languages: reporting


IMPUTE
Replaces missing values with aggregated values.
LAST
Retrieves the preceding value for a field.

Available Languages: reporting


LOOKUP
Retrieves a data value from a cross-referenced FOCUS data source in a MODIFY request.

Available Languages: MODIFY, Maintain


NULLIF
Returns a missing value when its parameters have equal values.

32
2. Introducing Functions

Available Languages: reporting

Simplified Date and Date-Time Functions


The following functions manipulate date and date- time values. For details see Simplified Date
and Date-Time Functions on page 341.
DT_CURRENT_DATE
Returns the current date.
DT_CURRENT_DATETIME
Returns the current date and time.
DT_CURRENT_TIME
Returns the current time.
DTADD
Returns a new date after adding the specified number of a supported component

Available Languages: reporting, Maintain


DTDIFF
Returns the number of given component boundaries between the two dates.

Available Languages: reporting, Maintain


DTIME
Extracts time components from a date-time value.
DTPART
Returns a component value in integer format.

Available Languages: reporting, Maintain


DTRUNC
Returns the first date within a period

Available Languages: reporting, Maintain

Date Functions
The following functions manipulate dates. For details see Date Functions on page 365.

Standard Date Functions


DATEADD
Adds a unit to or subtracts a unit from a date format.

Available Languages: reporting, Maintain

Using Functions 33
Types of Functions

DATECVT
Converts date formats.

Available Languages: reporting, Maintain


DATEDIF
Returns the difference between two dates in units.

Available Languages: reporting, Maintain


DATEMOV
Moves a date to a significant point on the calendar.

Available Languages: reporting, Maintain


DATETRAN
Formats dates in international formats.

Available Languages: reporting, Maintain


DPART
Extracts a component from a date field and returns it in numeric format.

Available Languages: reporting, Maintain


FIYR
Returns the financial year, also known as the fiscal year, corresponding to a given calendar
date based on the financial year starting date and the financial year numbering convention.

Available Languages: reporting, Maintain


FIQTR
Returns the financial quarter corresponding to a given calendar date based on the financial
year starting date and the financial year numbering convention.

Available Languages: reporting, Maintain


FIYYQ
Returns a financial date containing both the financial year and quarter that corresponds to
a given calendar date.

Available Languages: reporting, Maintain


HMASK
Extracts components from a date-time value and moves them to a target date-time field
with all other components of the target field preserved.

34
2. Introducing Functions

Available Languages: reporting, Maintain


TODAY
Retrieves the current date from the system.

Available Languages: reporting, Maintain

Legacy Date Functions


AYM
Adds or subtracts months from dates that are in year-month format.

Available Languages: reporting, Maintain


AYMD
Adds or subtracts days from dates that are in year-month-day format.

Available Languages: reporting, Maintain


CHGDAT
Rearranges the year, month, and day portions of alphanumeric dates, and converts dates
between long and short date formats.

Available Languages: reporting, Maintain


DA
Convert dates to the corresponding number of days elapsed since December 31, 1899.

DADMY converts dates in day-month-year format.

DADYM converts dates in day-year-month format.

DAMDY converts dates in month-day-year format.

DAMYD converts dates in month-year-day format.

DAYDM converts dates in year-day-month format.

DAYMD converts dates in year-month-day format.

Available Languages: reporting, Maintain


DMY, MDY, and YMD
Calculate the difference between two dates.

Available Languages: reporting, Maintain


DOWK and DOWKL
Find the day of the week that corresponds to a date.

Using Functions 35
Types of Functions

Available Languages: reporting, Maintain


DT
Converts the number of days elapsed since December 31, 1899 to the corresponding
date.

DTDMY converts numbers to day-month-year dates.

DTDYM converts numbers to day-year-month dates.

DTMDY converts numbers to month-day-year dates.

DTMYD converts numbers to month-year-day dates.

DTYDM converts numbers to year-day-month dates.

DTYMD converts numbers to year-month-day dates.

Available Languages: reporting, Maintain


GREGDT
Converts dates in Julian format to year-month-day format.

Available Languages: reporting, Maintain


JULDAT
Converts dates from year-month-day format to Julian (year-day format).

Available Languages: reporting, Maintain


YM
Calculates the number of months that elapse between two dates. The dates must be in
year-month format.

Available Languages: reporting, Maintain

Date-Time Functions
The following functions manipulate date-time values. For details see Date-Time Functions on
page 435.
HADD
Increments a date-time field by a given number of units.

Available Languages: reporting, Maintain


HCNVRT
Converts a date-time field to a character string.

36
2. Introducing Functions

Available Languages: reporting, Maintain


HDATE
Extracts the date portion of a date-time field, converts it to a date format, and returns the
result in the format YYMD.

Available Languages: reporting, Maintain


HDIFF
Calculates the number of units between two date-time values.

Available Languages: reporting, Maintain


HDTTM
Converts a date field to a date-time field. The time portion is set to midnight.

Available Languages: reporting, Maintain


HEXTR
Extracts components from a date-time value and moves them to a target date-time field
with all other components set to zero.

Available Languages: reporting, Maintain


HGETC
Stores the current date and time in a date-time field.

Available Languages: reporting, Maintain


HMASK
Extracts components from a date-time value and moves them to a target date-time field
with all other components of the target field preserved.

Available Languages: reporting, Maintain


HHMMSS
Retrieves the current time from the system.

Available Languages: reporting


HINPUT
Converts an alphanumeric string to a date-time value.

Available Languages: reporting, Maintain


HMIDNT
Changes the time portion of a date-time field to midnight (all zeros).

Using Functions 37
Types of Functions

Available Languages: reporting, Maintain


HNAME
Extracts a specified component from a date-time field and returns it in alphanumeric
format.

Available Languages: reporting, Maintain


HPART
Extracts a specified component from a date-time field and returns it in numeric format.

Available Languages: reporting, Maintain


HSETPT
Inserts the numeric value of a specified component into a date-time field.

Available Languages: reporting, Maintain


HTIME
Converts the time portion of a date-time field to the number of milliseconds or
microseconds.

Available Languages: reporting, Maintain


HTMTOTS/TIMETOTS
Converts a time to a timestamp.

Available Languages: reporting, Maintain

Maintain-specific Date and Time Functions


The following functions manipulate dates and times. They are available only in the Maintain
language. For details, see Maintain-specific Date and Time Functions on page 479.

Maintain-specific Standard Date and Time Functions


HHMMSS
Retrieves the current time from the system.
Initial_HHMMSS
Retrieves the time that the Maintain module was started.
Initial_TODAY
Retrieves the date that the Maintain module was started.

38
2. Introducing Functions

TODAY
Retrieves the current date from the system.
TODAY2
Retrieves the current date from the system.

Maintain-specific Legacy Date Functions


ADD
Adds a given number of days to a date.
DAY
Extracts the day of the month from a date.
JULIAN
Determines the number of days that have elapsed so far in the year up to a given date.
MONTH
Extracts the month from a date.
QUARTER
Determines the quarter of the year in which a date resides.
SETMDY
Sets a value to a date.
SUB
Subtracts a given number of days from a date.
WEEKDAY
Determines the day of the week for a date.
YEAR
Extracts the year from a date.

Simplified Conversion Functions


The following functions convert fields from one format to another, using streamlined parameter
lists. For details, see Simplified Conversion Functions on page 489.
CHAR
Returns a character based on a numeric code.

Using Functions 39
Types of Functions

COMPACTFORMAT
Converts a numeric value to an alphanumeric value that represents the number in an
abbreviated format, using the characters K, M, B, and T to represent the abbreviation.

Available Languages: reporting


CTRLCHAR
Returns a non-printable control character.
DT_FORMAT
Converts a date or date-time value to an alphanumeric string.
FPRINT
Converts a numeric, date, or date-time value to a character string.
HEXTYPE
Returns the hexadecimal view of an input value.
PHONETIC
Returns a phonetic key.

Format Conversion Functions


The following functions convert fields from one format to another. For details, see Format
Conversion Functions on page 503.
ATODBL
Converts a number in alphanumeric format to double-precision format.

Available Languages: reporting, Maintain


EDIT
Converts an alphanumeric field that contains numeric characters to numeric format or
converts a numeric field to alphanumeric format.

Available Languages: reporting


FPRINT
Converts a field to alphanumeric format.

Available Languages: reporting


FTOA
Converts a number in a numeric format to alphanumeric format.

Available Languages: reporting, Maintain

40
2. Introducing Functions

HEXBYT
Obtains the ASCII or EBCDIC character equivalent of a decimal integer value.

Available Languages: reporting, Maintain


ITONUM
Converts a large binary integer in a non-FOCUS data source to double-precision format.

Available Languages: reporting, Maintain


ITOPACK
Converts a large binary integer in a non-FOCUS data source to packed-decimal format.

Available Languages: reporting, Maintain


ITOZ
Converts a number in numeric format to zoned format.

Available Languages: reporting, Maintain


PCKOUT
Writes a packed number of variable length to an extract file.

Available Languages: reporting, Maintain


PTOA
Converts a packed decimal number from numeric format to alphanumeric format.

Available Languages: reporting, Maintain


TSTOPACK
Converts a Microsoft SQL Server or Sybase TIMESTAMP column (which contains an
incremented counter) to packed decimal.

Available Languages: reporting


UFMT
Converts characters in alphanumeric field values to hexadecimal representation.

Available Languages: reporting, Maintain


XTPACK
Stores a packed number with up to 31 significant digits in an alphanumeric field, retaining
decimal data.

Using Functions 41
Types of Functions

Maintain-specific Light Update Support Functions


The following functions retrieve WebFOCUS variable or parameter data implicitly from within a
Maintain procedure. These functions are available only in Maintain Data. For details, see
Maintain-specific Light Update Support Functions on page 529.
IWC.GetAppCGIValue
Imports the value of a WebFOCUS parameter or variable into a Maintain Data variable.
IWC.FindAppCGIValue
Retrieves WebFOCUS parameter or variable values.

Simplified Numeric Functions


The following functions perform calculations on numeric constants or fields, using streamlined
parameter lists. For details, see Simplified Numeric Functions on page 533
CEILING
Returns the smallest integer value greater than or equal to a value.
EXPONENT
Raises e to a power.
FLOOR
Returns the largest integer value less than or equal to a value.
MOD
Calculates the remainder from a division.
POWER
Raises a value to a power.

Numeric Functions
The following functions perform calculations on numeric constants or fields. For details, see
Numeric Functions on page 547
ABS
Returns the absolute value of a number.

Available Languages: reporting, Maintain


ASIS
Distinguishes between a blank and a zero in Dialogue Manager.

Available Languages: reporting

42
2. Introducing Functions

BAR
Produces a horizontal bar chart.

Available Languages: reporting, Maintain


CHKPCK
Validates the data in a field described as packed format.

Available Languages: reporting, Maintain


DMOD, FMOD, and IMOD
Calculate the remainder from a division.

Available Languages: reporting, Maintain


EXP
Raises the number "e" to a specified power.

Available Languages: reporting, Maintain


EXPN
Is an operator that evaluates a number expressed in scientific notation. For information,
see Using Expressions in the Creating Reports With WebFOCUS Language manual.
FMLINFO
Returns the FOR value associated with each row in an FML report.

Available Languages: reporting


FMLLIST
Returns a string containing the complete tag list for each row in an FML request.

Available Languages: reporting


FMLFOR
Retrieves the tag value associated with each row in an FML request.

Available Languages: reporting


FMLCAP
Returns the caption value for each row in an FML hierarchy request.

Available Languages: reporting


INT
Returns the integer component of a number.

Using Functions 43
Types of Functions

Available Languages: reporting, Maintain


LOG
Returns the natural logarithm of a number.

Available Languages: reporting, Maintain


MAX and MIN
Return the maximum or minimum value, respectively, from a list of values.

Available Languages: reporting, Maintain


MIRR
Calculates the modified internal rate of return for a series of periodic cash flows.

Available Languages: reporting


NORMSDST and NORMSINV
Perform calculations on a standard normal distribution curve.

Available Languages: reporting


PRDNOR and PRDUNI
Generate reproducible random numbers.

Available Languages: reporting, Maintain


RDNORM and RDUNIF
Generate random numbers.

Available Languages: reporting, Maintain


SQRT
Calculates the square root of a number.

Available Languages: reporting, Maintain


XIRR
Calculates the internal rate of return for a series of cash flows that can be periodic or non-
periodic.

Available Languages: reporting

44
2. Introducing Functions

Maintain-specific Script Functions


Script functions integrate JavaScript and VBScripts into your Maintain Data applications and
perform client-side execution without returning to the WebFOCUS Server. These functions are
available only in Maintain Data. For details, see Maintain-specific Script Functions on page 581
IWCLink
Executes external procedures.
IWCSwitchToSecure and IWCSwitchToUnsecure
Turns the Secure Sockets layer on and off, respectively.
IWCTrigger
Returns control from the script to your application.

Simplified Statistical Functions


The following functions perform statistical functions. For details, see Simplified Statistical
Functions on page 589.
CORRELATION
Calculates the degree of correlation between two independent sets of data.
KMEANS_CLUSTER
Partitions observations into clusters based on the nearest mean value.
MULTIREGRESS
Calculates a linear regression column based on multiple fields.
OUTLIER
Identifies outliers in a numeric field using the 1.5 * IQR rule.
RSERVE
Runs an R script.
STDDEV
Calculates the standard deviation in a set of data values.

Machine Learning (Python-based) Functions


The following functions run Python scripts to perform classification and regression. For details,
see Machine Learning (Python-based) Functions.
ANOMALY_IF
Detects outliers using an Isolation Forest.

Using Functions 45
Types of Functions

CLASSIFY_BLR
Finds the best linear separation between two classes in the space spanned by the
predictors, and returns either a class assignment (0 or 1) or the probability of belonging to
class 1.
CLASSIFY_KNN
Assigns a class membership to a data point by assigning the class most common among
its k nearest neighbors.
CLASSIFY_RF
Creates a random forest (ensemble of decision trees), and returns the prediction that is
the average of the individual predictions.
CLASSIFY_XGB
Creates a random forest (ensemble of decision trees), where each new tree attempts to
improve on the predictive capabilities of the prior trees.
REGRESS_KNN
Predicts a target value for a data point by assigning the average of the target values of its
k nearest neighbors.
REGRESS_POLY
Fits the target column to a polynomial expression of the predictor columns.
REGRESS_RF
Creates a random forest (ensemble of decision trees), and returns the prediction that is
the majority vote of the individual classification predictions.
REGRESS_XGB
Creates a random forest (ensemble of decision trees), where each new tree attempts to
improve on the predictive capabilities of the prior trees.
RUN_MODEL and RUN_MODEL2
RUN_MODEL runs a saved model where the field names in the data source used to run the
model are the same as the field names in the data source used to create the model.
RUN_MODEL2 runs a saved model where the field names in the data source used to run
the model are not the same as the field names in the data source used to create the
model.

Simplified System Functions


The following functions call the operating system to obtain information about the operating
environment or to use a system service, using streamlined parameter lists. For details, see
Simplified System Functions on page 639
EDAPRINT
Inserts a custom message in the EDAPRINT log file.

46
2. Introducing Functions

ENCRYPT
Encrypts a password.
GETENV
Retrieves the value of an environment variable.
PUTENV
Assigns a value to an environment variable.
SLACK
Posts a message to a Slack channel from a WebFOCUS request.

System Functions
The following functions call the operating system to obtain information about the operating
environment or to use a system service. For details, see System Functions on page 645
CLSDDREC
Closes a file and frees the memory used to store information about open files.

Available Languages: reporting, Maintain


FEXERR
Retrieves an Information Builders error message.

Available Languages: reporting, Maintain


FINDMEM
Determines if a specific member of a partitioned data set (PDS) exists in batch processing.

Available Operating Systems: z/OS

Available Languages: reporting, Maintain


GETCOOKI
Retrieves the value of a browser cookie.

Available Languages: reporting, Maintain


GETHEADR
Retrieves the value of an HTTP Header variable.

Available Languages: reporting, Maintain


GETPDS
Determines if a specific member of a partitioned data set (PDS) exists, and if it does,
returns the PDS name.

Available Operating Systems: z/OS

Using Functions 47
Types of Functions

Available Languages: reporting, Maintain


GETUSER
Retrieves the ID of the connected user.

Available Languages: reporting, Maintain


MVSDYNAM
Transfers a FOCUS DYNAM command to the DYNAM command processor.

Available Operating Systems: z/OS

Available Languages: reporting, Maintain


PUTCOOKI
Submits a value to a browser cookie.

Available Languages: reporting, Maintain


PUTDDREC
Writes a character string as a record in a sequential file. Opens the file if it is closed.

Available Languages: reporting, Maintain


SLEEP
Suspends execution for a specified number of seconds.

Available Languages: reporting


SPAWN
Spawns a child process to execute system commands without terminating the current
procedure. After the child process terminates, control returns to the parent process. This
function is available only for WebFOCUS.

Available Operating Systems: UNIX

Available Languages: reporting

SYSTEM
Calls a DOS program, a DOS batch program, or a Windows application. This function is
available only for WebFOCUS.

Available Operating Systems: Windows

Available Languages: reporting

48
2. Introducing Functions

SYSVAR
Retrieves the value of a z/OS system variable.

Available Operating Systems: z/OS

Available Languages: reporting

Simplified Geography Functions


These functions perform location-based calculations and retrieve geocoded points for various
types of location data. For details, see Simplified Geography Functions on page 671.
GIS_DISTANCE
Calculates the distance between geography points.
GIS_DRIVE_ROUTE
Calculates the driving directions between geography points.
GIS_POINT
Builds a geometry point.
GIS_GEOCODE_ADDR
Geocodes a complete address.
GIS_GEOCODE_ADDR_CITY
Geocodes an address line, city, and state.
GIS_GEOCODE_ADDR_POSTAL
Geocodes an address line and postal code.
GIS_GEOMETRY
Builds a JSON geometry object.
GIS_IN_POLYGON
Determines whether a point is in a complex polygon.
GIS_LINE
Builds a JSON line.
GIS_REVERSE_COORDINATE
Given a longitude, latitude, and component name, returns the applicable geographic
component.
GIS_SERVICE_AREA
Calculates a geometry area around a given point.
GIS_SERV_AREA_XY
Calculates a geometry area around a given coordinate.

Using Functions 49
Character Chart for ASCII and EBCDIC

Character Chart for ASCII and EBCDIC


This chart shows the primary printable characters in the ASCII and EBCDIC character sets and
their decimal equivalents. Extended ASCII codes (above 127) are not included

Decimal ASCII EBCDIC

33 ! exclamation point

34 " quotation mark

35 # number sign

36 $ dollar sign

37 % percent

38 & ampersand

39 ' apostrophe

40 ( left parenthesis

41 ) right parenthesis

42 * asterisk

43 + plus sign

44 , comma

45 - hyphen

46 . period

47 / slash

48 0 0

49 1 1

50 2 2

51 3 3

50
2. Introducing Functions

Decimal ASCII EBCDIC

52 4 4

53 5 5

54 6 6

55 7 7

56 8 8

57 9 9

58 : colon

59 ; semicolon

60 < less-than sign

61 = equal sign

62 > greater-than sign

63 ? question mark

64 @ at sign

65 A A

66 B B

67 C C

68 D D

69 E E

70 F F

71 G G

72 H H

73 I I

Using Functions 51
Character Chart for ASCII and EBCDIC

Decimal ASCII EBCDIC

74 J J ¢ cent sign

75 K K . period

76 L L < less-than sign

77 M M ( left parenthesis

78 N N + plus sign

79 O O | logical or

80 P P & ampersand

81 Q Q

82 R R

83 S S

84 T T

85 U U

86 V V

87 W W

88 X X

89 Y Y

90 Z Z ! exclamation point

91 [ opening bracket $ dollar sign

92 \ back slant * asterisk

93 ] closing bracket ) right parenthesis

94 ^ caret ; semicolon

95 _ underscore ¬ logical not

52
2. Introducing Functions

Decimal ASCII EBCDIC

96 ` grave accent - hyphen

97 a a / slash

98 b b

99 c c

100 d d

101 e e

102 f f

103 g g

104 h h

105 i i

106 j j

107 k k , comma

108 l l % percent

109 m m _ underscore

110 n n > greater-than sign

111 o o ? question mark

112 p p

113 q q

114 r r

115 s s

116 t t

117 u u

Using Functions 53
Character Chart for ASCII and EBCDIC

Decimal ASCII EBCDIC

118 v v

119 w w

120 x x

121 y y

122 z z : colon

123 { opening brace # number sign

124 | vertical line @ at sign

125 } closing brace ' apostrophe

126 ~ tilde = equal sign

127 " quotation mark

129 a a

130 b b

131 c c

132 d d

133 e e

134 f f

135 g g

136 h h

137 i i

145 j j

146 k k

147 l l

54
2. Introducing Functions

Decimal ASCII EBCDIC

148 m m

149 n n

150 o o

151 p p

152 q q

153 r r

162 s s

163 t t

164 u u

165 v v

166 w w

167 x x

168 y y

169 z z

185 ` grave accent

193 A A

194 B B

195 C C

196 D D

197 E E

198 F F

199 G G

Using Functions 55
Character Chart for ASCII and EBCDIC

Decimal ASCII EBCDIC

200 H H

201 I I

209 J J

210 K K

211 L L

212 M M

213 N N

214 O O

215 P P

216 Q Q

217 R R

226 S S

227 T T

228 U U

229 V V

230 W W

231 X X

232 Y Y

233 Z Z

240 0 0

241 1 1

242 2 2

56
2. Introducing Functions

Decimal ASCII EBCDIC

243 3 3

244 4 4

245 5 5

246 6 6

247 7 7

248 8 8

249 9 9

Using Functions 57
Character Chart for ASCII and EBCDIC

58
Chapter 3 Accessing and Calling a Function
The following topics describe the considerations for supplying arguments in a function,
and explain how to use a function in a command and access functions stored externally.
In this chapter:

Calling a Function

Supplying an Argument in a Function

Calling a Function From a DEFINE, COMPUTE, or VALIDATE Command

Calling a Function From a Dialogue Manager Command

Calling a Function From Another Function

Calling a Function in WHERE or IF Criteria

Calling a Function in WHEN Criteria

Calling a Function From a RECAP Command

Storing and Accessing an External Function

Calling a Function
You can call a function from a COMPUTE, DEFINE, or VALIDATE command. You can also call
functions from a Dialogue Manager command, a Financial Modeling Language (FML) command,
or a Maintain command. A function is called with the function name, arguments, and, for
external functions, an output field.

For more information on external functions, see Types of Functions on page 20.

Some Maintain-specific functions require that the MNTUWS function library be retrieved when
calling the function. For functions that require this, it is specified in the detailed information for
that function. For more information on retrieving the MNTUWS library, see How to Access the
Maintain MNTUWS Function Library on page 61.

Using Functions 59
Calling a Function

Syntax: How to Call a Function

function(arg1, arg2, ... [outfield])

where:
function
Is the name of the function.
arg1, arg2, ...
Are the arguments.
outfield
Is the field that contains the result, or the format of the output value enclosed in single
quotation marks. This argument is required only for external functions.

In Dialogue Manager, you must specify the format. In Maintain Data, you must specify the
name of the field.

Syntax: How to Store Output in a Field

COMPUTE field/fmt = function(input1, input2,... [outfield]);

or

DEFINE FILE file


field/fmt = function(input1, input2,... [outfield]);

or

-SET &var = function(input1, input2,... [outfield]);

where:
DEFINE

Creates a virtual field that may be used in a request as though it is a real data source
field.
COMPUTE

Calculates one or more temporary fields in a request. The field is calculated after all
records have been selected, sorted, and summed.
field
Is the field that contains the result.

60
3. Accessing and Calling a Function

file
Is the file in which the virtual field is created.
var
Is the variable that contains the result.
fmt
Is the format of the field that contains the result.
function
Is the name of the function, up to eight characters long.
input1, input2,...
Are the input arguments, which are data values or fields used in function processing. For
more information about arguments, see Supplying an Argument in a Function on page
61.
outfield
Is the field that contains the result, or the format of the output value enclosed in single
quotation marks. This argument is required only for external functions.

In Dialogue Manager, you must specify the format. In Maintain Data, you must specify the
name of the field.

Syntax: How to Access the Maintain MNTUWS Function Library


Place the following statement directly after the MAINTAIN command at the top of your
procedure:
MODULE IMPORT (MNTUWS);

Supplying an Argument in a Function


When supplying an argument in a function, you must understand which types of arguments are
acceptable, the formats and lengths for these arguments, and the number and order of these
arguments.

Argument Types
The following are acceptable arguments for a function:

Numeric constant, such as 6 or 15.

Date constant, such as 022802.

Using Functions 61
Supplying an Argument in a Function

Date in alphanumeric, numeric, date, or AnV format.

Alphanumeric literal, such as STEVENS or NEW YORK NY. A literal must be enclosed in
single quotation marks.

Number in alphanumeric format.

Field name, such as FIRST_NAME or HIRE_DATE. A field can be a data source field or
temporary field. The field name can be up to 66 characters long or a qualified field name,
unique truncation, or alias.

Expression, such as a numeric, date, or alphanumeric expression. An expression can use


arithmetic operators and the concatenation sign (|). For example, the following are valid
expressions:
CURR_SAL * 1.03

and
FN || LN

Dialogue Manager variable, such as &CODE or &DDNAME.

Format of the output value enclosed in single quotation marks.

Another function.

Label or other row or column reference (such as R or E), or name of another RECAP
calculation, when the function is called in an FML RECAP command.

Argument Formats
Depending on the function, an argument can be in alphanumeric, numeric, or date format. If
you supply an argument in the wrong format, you will cause an error or the function will not
return correct data. The following are the types of argument formats:

Alphanumeric argument. An alphanumeric argument is stored internally as one character


per byte. An alphanumeric argument can be a literal, an alphanumeric field, a number or
date stored in alphanumeric format, an alphanumeric expression, or the format of an
alphanumeric field. A literal is enclosed in single quotation marks, except when specified in
operating systems that support Dialogue Manager RUN commands (for example, -MVS
RUN).

62
3. Accessing and Calling a Function

Numeric argument. A numeric argument is stored internally as a binary or packed number.


A numeric argument includes integer (I), floating-point single-precision (F), floating-point
double-precision (D), and packed decimal (P) formats. A numeric argument can be a
numeric constant, field, or expression, or the format of a numeric field.

All numeric arguments are converted to floating-point double-precision format when used
with a function, but results are returned in the format specified for the output field.

Note: With CDN ON, numeric arguments must be delimited by a comma followed by a
space.

Date argument. A date argument can be in either alphanumeric, numeric, or date format.
The list of arguments for the individual function will specify what type of format the function
accepts. A date argument can be a date in alphanumeric, numeric, or date format; a date
field or expression; or the format of a date field.

If you supply an argument with a two-digit year, the function assigns a century based on the
YRTHRESH and DEFCENT parameter settings.

Argument Length
An argument is passed to a function by reference, meaning that the memory location of the
argument is passed. No indication of the length of the argument is given.

You must supply the argument length for alphanumeric strings. Some functions require a
length for the input and output arguments (for example, SUBSTR), and others use one length
for both arguments (for example, UPCASE).

Be careful to ensure that all lengths are correct. Providing an incorrect length can cause
incorrect results:

If the specified length is shorter than the actual length, a subset of the string is used. For
example, passing the argument 'ABCDEF' and specifying a length of 3 causes the function
to process a string of 'ABC'.

If the specified length is too long, whatever is in memory up to that length is included. For
example, passing an argument of 'ABC' and specifying a length of 6 causes the function to
process a string beginning with 'ABC' plus the three characters in the next three positions
of memory. Depending on memory utilization, the extra three characters could be anything.

Some operating system routines are very sensitive to incorrectly specified lengths and read
them into incorrectly formatted memory areas.

Using Functions 63
Supplying an Argument in a Function

Number and Order of Arguments


The number of arguments required varies according to each function. Functions supplied by
Information Builders may require up to six arguments. User-written subroutines may require a
maximum of 200 arguments including the output argument. If a function requires more than
200 arguments, you must use two or more calls to pass the arguments to the function.

Arguments must be specified in the order shown in the syntax of each function. The required
order varies according to the function.

Verifying Function Parameters


The USERFCHK setting controls the level of verification applied to DEFINE FUNCTION and
Information Builders-supplied function arguments. It does not affect verification of the number
of parameters; the correct number must always be supplied.

USERFCHK is not supported from Maintain Data.

Functions typically expect parameters to be a specific type or have a length that depends on
the value of another parameter. It is possible in some situations to enforce these rules by
truncating the length of a parameter and, therefore, avoid generating an error at run time.

The level of verification and possible conversion to a valid format performed depends on the
specific function. The following two situations can usually be converted satisfactorily:

If a numeric parameter specifies a maximum size for an alphanumeric parameter, but the
alphanumeric string supplied is longer than the specified size, the string can be truncated.

If a parameter supplied as a numeric literal specifies a value larger than the maximum size
for a parameter, it can be reduced to the proper value.

Syntax: How to Enable Parameter Verification


Parameter verification can be enabled only for DEFINE FUNCTIONs and functions supplied by
Information Builders. If your site has a locally written function with the same name as an
Information Builders-supplied function, the USERFNS setting determines which function is
used.

SET USERFNS= {SYSTEM|LOCAL}

where:
SYSTEM

Gives precedence to functions supplied by Information Builders. SYSTEM is the default


value. This setting is required in order to enable parameter verification.

64
3. Accessing and Calling a Function

LOCAL

Gives precedence to locally written functions. Parameter verification is not performed with
this setting in effect.

Note: When USERFNS is set to LOCAL, DT functions only display a six-digit date.

Syntax: How to Control Function Parameter Verification


Issue the following command in FOCPARM, FOCPROF, on the command line, in a FOCEXEC, or
in an ON TABLE command. Note that the USERFNS=SYSTEM setting must be in effect.
SET USERFCHK = setting

where:
setting
Can be one of the following:

ON is the default value. Verifies parameters in requests, but does not verify parameters
for functions used in Master File DEFINEs. If a parameter has an incorrect length, an
attempt is made to fix the problem. If such a problem cannot be fixed, an error
message is generated and the evaluation of the affected expression is terminated.

Because parameters are not verified for functions specified in a Master File, no errors
are reported for those functions until the DEFINE field is used in a subsequent request
when, if a problem occurs, the following message is generated:
(FOC003) THE FIELDNAME IS NOT RECOGNIZED

OFF does not verify parameters except in the following cases:

If a parameter that is too long would overwrite the memory area in which the
computational code is stored, the size is automatically reduced without issuing a
message.

If an alphanumeric parameter is too short, it is padded with blanks to the correct


length.

Note:

The OFF setting will be deprecated in a future release.

We strongly recommend that you not use the OFF setting, as disabling parameter
checking can lead to unexpected issues.

FULL is the same as ON, but also verifies parameters for functions used in Master File
DEFINEs.

Using Functions 65
Supplying an Argument in a Function

ALERT verifies parameters in a request without halting execution when a problem is


detected. It does not verify parameters for functions used in Master File DEFINEs. If a
parameter has an incorrect length and an attempt is made to fix the problem behind
the scenes, the problem is corrected with no message. If such a problem cannot be
fixed, a warning message is generated. Execution then continues as though the setting
were OFF, but the results may be incorrect.
Note:

If a parameter provided is the incorrect type, verification fails and processing


terminates.

Errors encountered during subroutine processing, unless fatal at the system level,
are communicated to the calling routine through the return of an unchanged return
parameter, which is the last parameter in the subroutine call. This is always
communicated as spaces for alphanumeric outputs.

Example: Verifying Parameters With Correctable Errors


The following request uses SUBSTR to extract the substring that starts in position 6 and ends
in position 14 of the TITLE field. The fifth argument specifies a substring length (500) that is
too long (it should be no longer than 9).
SET USERFCHK = ON
TABLE FILE MOVIES
PRINT TITLE
COMPUTE
NEWTITLE/A9 = SUBSTR(39, TITLE, 6 ,14, 500, NEWTITLE);
WHERE CATEGORY EQ 'CHILDREN'
END

When the request is executed with USERFCHK=ON or OFF, the incorrect length is corrected and
the request continues processing:
TITLE NEWTITLE
----- --------
SMURFS, THE S, THE
SHAGGY DOG, THE Y DOG, TH
SCOOBY-DOO-A DOG IN THE RUFF Y-DOO-A D
ALICE IN WONDERLAND IN WONDE
SESAME STREET-BEDTIME STORIES AND SONGS E STREET-
ROMPER ROOM-ASK MISS MOLLY R ROOM-AS
SLEEPING BEAUTY ING BEAUT
BAMBI

66
3. Accessing and Calling a Function

Example: Verifying Parameters With Uncorrectable Errors


The following request has an incorrect data type in the last argument to SUBSTR. This
parameter should specify an alphanumeric field or format for the extracted substring:
SET USERFCHK = ON
TABLE FILE MOVIES
PRINT TITLE
COMPUTE
NEWTITLE/F9 = SUBSTR(39, TITLE, 6 ,14, 500, 'F9');
WHERE CATEGORY EQ 'CHILDREN'
END

When the request is executed with USERFCHK=ON, a message is produced and the
request terminates:
ERROR AT OR NEAR LINE 5 IN PROCEDURE USERFC3 FOCEXEC
(FOC279) NUMERIC ARGUMENTS IN PLACE WHERE ALPHA ARE CALLED FOR
(FOC009) INCOMPLETE REQUEST STATEMENT
UNKNOWN FOCUS COMMAND WHERE
BYPASSING TO END OF COMMAND

When the request is executed with USERFCHK=OFF, no verification is done and no


message is produced. The request executes and produces incorrect results. In some
environments, this type of error may cause abnormal termination of the application:
DIRECTOR TITLE NEWTITLE
-------- ----- --------
SMURFS, THE *********
BARTON C. SHAGGY DOG, THE *********
SCOOBY-DOO-A DOG IN THE RUFF *********
GEROMINI ALICE IN WONDERLAND 1
SESAME STREET-BEDTIME STORIES AND SONGS -265774
ROMPER ROOM-ASK MISS MOLLY *********
DISNEY W. SLEEPING BEAUTY *********
DISNEY W. BAMBI 0

Calling a Function From a DEFINE, COMPUTE, or VALIDATE Command


You can call a function from a DEFINE command or Master File attribute, a COMPUTE
command, or a VALIDATE command.

Syntax: How to Call a Function From a COMPUTE, DEFINE, or VALIDATE Command

DEFINE [FILE filename]


tempfield[/format] = function(input1, input2, input3, ... [outfield]);
COMPUTE
tempfield[/format] = function(input1, input2, input3, ... [outfield]);
VALIDATE
tempfield[/format] = function(input1, input2, input3, ... [outfield]);

Using Functions 67
Calling a Function From a Dialogue Manager Command

where:
filename
Is the data source being used.
tempfield
Is the temporary field created by the DEFINE or COMPUTE command. This is the same field
specified in outfield. If the function call supplies the format of the output value in outfield,
the format of the temporary field must match the outfield argument.
format
Is the format of the temporary field. The format is required if it is the first time the field is
created; otherwise, it is optional. The default value is D12.2.
function
Is the name of the function.
input1, input2, input3...
Are the arguments.
outfield
Is the field that contains the result, or the format of the output value enclosed in single
quotation marks. This is required only for external functions.

In Dialogue Manager, you must specify the format. In Maintain Data, you must specify the
name of the field.

Calling a Function From a Dialogue Manager Command


You can call a function with Dialogue Manager in the following ways:

From a -SET command, storing the result of a function in a variable. For more information,
see Assigning the Result of a Function to a Variable on page 69.

From an -IF command. For more information, see Calling a Function in WHERE or IF Criteria
on page 73.

From an operating system -RUN command. For more information, see Calling a Function
From an Operating System RUN Command on page 72.

Dialogue Manager converts a numeric argument to double-precision format. This occurs when
the value of the argument is numeric; this is not affected by the format expected by the
function. This means you must be careful when supplying arguments for a function in Dialogue
Manager.

68
3. Accessing and Calling a Function

If the function expects an alphanumeric string and the input is a numeric string, incorrect
results will occur because of conversion to floating-point double-precision. To resolve this
problem, append a non-numeric character to the end of the string, but do not count this extra
character in the length of the argument.

Dialogue Manager date variables such as &YYMD return alphanumeric legacy dates, not a date
format (an offset from a base date). If a function requires a date offset rather than a legacy
date, you must convert any date variable to a date offset (using the DATECVT function) before
using it as an argument. You can then convert the result back to a legacy date, again with the
DATECVT function. For example:
-SET &TODAY_OFFSET=DATECVT(&YYMD , 'I8YYMD' , 'YYMD');
-SET &BEG_CUR_YR=DATEMOV(&TODAY_OFFSET.EVAL , 'BOY');
-SET &CLOSE_DTBOY=DATECVT(&BEG_CUR_YR.EVAL , 'YYMD' , 'I8YYMD')';

Assigning the Result of a Function to a Variable


You can store the result of a function in a variable with the -SET command.

A Dialogue Manager variable contains only alphanumeric data. If a function returns a numeric
value to a Dialogue Manager variable, the value is truncated to an integer and converted to
alphanumeric format before being stored in the variable.

Syntax: How to Assign the Result of a Function to a Variable

-SET &variable = function(arg1, arg2[.LENGTH],..., 'format');

where:
variable
Is the variable to which the result will be assigned.
function
Is the function.
arg1, arg2
Are the function's arguments.
.LENGTH

Returns the length of the variable. If a function requires the length of a character string as
an input argument, you can prompt for the character string and determine the length with
the .LENGTH suffix.

Using Functions 69
Calling a Function From a Dialogue Manager Command

format
Is the format of the result enclosed in single quotation marks. You cannot specify a
Dialogue Manager variable for the output argument unless you use the .EVAL suffix;
however, you can specify a variable for an input argument.

Example: Calling a Function From a -SET Command


AYMD adds 14 days to the value of &INDATE. The &INDATE variable is previously set in the
procedure in the six-digit year-month-day format.
-SET &OUTDATE = AYMD(&INDATE, 14, 'I6');

The format of the output date is a six-digit integer (I6). Although the format indicates that the
output is an integer, it is stored in the &OUTDATE variable as a character string. For this
reason, if you display the value of &OUTDATE, you will not see slashes separating the year,
month, and day.

Branching Based on the Result of a Function


You can branch based on the result of a function by calling a function from a Dialogue Manager
-IF command.

If a branching command spans more than one line, continue it on the next line by placing a
dash (-) in the first column.

Syntax: How to Branch Based on the Result of a Function

-IF function(args) relation expression GOTO label1 [ELSE GOTO label2];

where:
function
Is the function.
args
Are the arguments.
relation
Is an operator that determines the relationship between the function and expression, for
example, EQ or LE.
expression
Is a value, logical expression, or function. Do not enclose a literal in single quotation
marks unless it contains a comma or embedded blank.

70
3. Accessing and Calling a Function

label1, label2
Are user-defined names up to 12 characters long. Do not use embedded blanks or the
name of any other Dialogue Manager command except -QUIT or -EXIT. Do not use a word
that can be confused with a function, or an arithmetic or logical operation.

The label text can precede or follow the -IF criteria in the procedure.
ELSE GOTO

Passes control to label2 when the -IF test fails.

Example: Branching Based on the Function’s Result


The result of the AYMD function provides a condition for a -IF test. One of two requests is
executed, depending on the function's result:
-LOOP
1. -IF &INDATE EQ 0 GOTO EXIT;
2. -SET &WEEKDAY = DOWK(&INDATE, 'A4');
3. -TYPE START DATE IS &WEEKDAY &INDATE
4. -IF AYMD(&INDATE, &DAYS, 'I6YMD') LT 960101 GOTO EARLY;
5. -TYPE LONG PROJECT
-*EX LONGPROJ
-RUN
-GOTO EXIT
6. -EARLY
-TYPE SHORT PROJECT
-*EX SHRTPROJ
-RUN
-EXIT

The procedure processes as follows:

1. If you enter a 0, it passes control to -EXIT which terminates execution.


2. The DOWK function obtains the day of the week for the start date.
3. The -TYPE command displays the day of the week and start date of the project.
4. The AYMD function calculates the date that the project will finish. If this date is before
January 1, 1996, the -IF command branches to the label EARLY.
5. If the project will finish on or after January 1, 1996, the TYPE command displays the words
LONG PROJECT and exits.
6. If the procedure branches to the label EARLY, the TYPE command displays the words
SHORT PROJECT and exits.

Using Functions 71
Calling a Function From a Dialogue Manager Command

Calling a Function From an Operating System RUN Command


You can call a function that contains only alphanumeric arguments from a Dialogue Manager -
TSO RUN or -MVS RUN command. This type of function performs a specific task but typically
does not return a value.

If a function requires an argument in numeric format, you must first convert it to floating-point
double-precision format using the ATODBL function because, unlike the -SET command, an
operating system RUN command does not automatically convert a numeric argument to double-
precision.

Syntax: How to Call a Function From an Operating System -RUN Command

{-TSO|-MVS} RUN function, input1, input2, ... [,&output]

where:
-TSO|-MVS

Is the operating system.


function
Is the name of the function.
input1, input2,...
Are the arguments. Separate the function name and each argument with a comma. Do not
enclose an alphanumeric literal in single quotation marks. If a function requires the length
of a character string as an argument, you can prompt for the character string, then use
the .LENGTH suffix to test the length.
&output
Is a Dialogue Manager variable. Include this argument if the function returns a value;
otherwise, omit it. If you specify an output variable, you must pre-define its length using a -
SET command.

For example, if the function returns a value that is eight bytes long, define the variable with
eight characters enclosed in single quotation marks before the function call:
-SET &output = '12345678';

72
3. Accessing and Calling a Function

Example: Calling a Function From an Operating System -RUN Command


The following example calls the CHGDAT function from a -MVS RUN command:

-SET &RESULT = '12345678901234567';


-MVS RUN CHGDAT, YYMD., MXDYY, &YYMD, &RESULT
-TYPE &RESULT

Calling a Function From Another Function


A function can be an argument for another function.

Syntax: How to Call a Function From Another Function

field = function([arguments,] function2[arguments2,] arguments);

where:
field
Is the field that contains the result of the function.
function
Is a function.
arguments
Are arguments for function.
function2
Is the function that is an argument for function.
arguments2
Are arguments for function2.

Example: Calling a Function From Another Function


In the following example, the AYMD function is an argument for the YMD function:

-SET &DIFF = YMD(&YYMD, AYMD(&YYMD, 4, 'I8'));

Calling a Function in WHERE or IF Criteria


You can call a function in WHERE or IF criteria. When you do this, the output value of the
function is compared against a test value.

Using Functions 73
Calling a Function in WHERE or IF Criteria

Syntax: How to Call a Function in WHERE Criteria

WHERE function relation expression

where:
function
Is a function.
relation
Is an operator that determines the relationship between the function and expression, for
example, EQ or LE.
expression
Is a constant, field, or function. A literal must be enclosed in single quotation marks.

Syntax: How to Call a Function in IF Criteria

IF function relation value

where:
function
Is a function.
relation
Is an operator that determines the relationship between the function and expression, for
example, EQ or LE.
value
Is a constant. In a DEFINE or COMPUTE command, the value must be enclosed in single
quotation marks.

Example: Calling a Function in WHERE Criteria


The SUBSTR function extracts the first two characters of LAST_NAME as a substring, and the
request prints an employee's name and salary if the substring is MC.
TABLE FILE EMPLOYEE
PRINT FIRST_NAME LAST_NAME CURR_SAL
WHERE SUBSTR(15, LAST_NAME, 1, 2, 2, 'A2') IS 'MC';
END

74
3. Accessing and Calling a Function

The output is:


FIRST_NAME LAST_NAME CURR_SAL
---------- --------- --------
JOHN MCCOY $18,480.00
ROGER MCKNIGHT $16,100.00

Using a Calculation or Compound IF Command


You must specify the format of the output value in a calculation or compound IF command.
There are two ways to do this:

Pre-define the format within a separate command. In the following example, the AMOUNT
field is pre-defined with the format D8.2 and the function returns a value to the output field
AMOUNT. The IF command tests the value of AMOUNT and stores the result in the
calculated value, AMOUNT_FLAG.

COMPUTE
AMOUNT/D8.2 =;
AMOUNT_FLAG/A5 = IF function(input1, input2, AMOUNT) GE 500
THEN 'LARGE' ELSE 'SMALL';

Supply the format as the last argument in the function call. In the following example, the
command tests the returned value directly. This is possible because the function defines
the format of the returned value (D8.2).

DEFINE
AMOUNT_FLAG/A5 = IF function(input1, input2, 'D8.2') GE 500
THEN 'LARGE' ELSE 'SMALL';

Calling a Function in WHEN Criteria


You can call a function in WHEN criteria as part of a Boolean expression.

Syntax: How to Call a Function in WHEN Criteria

WHEN({function|value} relation {function|value});

or

WHEN NOT(function)

where:
function
Is a function.

Using Functions 75
Calling a Function From a RECAP Command

value
Is a value or logical expression.
relation
Is an operator that determines the relationship between the value and function, for
example, LE or GT.

Example: Calling a Function in WHEN Criteria


This request checks the values in LAST_NAME against the result of the CHKFMT function.
When a match occurs, the request prints a sort footing.
TABLE FILE EMPLOYEE
PRINT DEPARTMENT BY LAST_NAME
ON LAST_NAME SUBFOOT
"*** LAST NAME <LAST_NAME DOES MATCH MASK"
WHEN NOT CHKFMT(15, LAST_NAME, 'SMITH ', 'I6');
END

The output is:


LAST_NAME DEPARTMENT
--------- ----------
BANNING PRODUCTION
BLACKWOOD MIS
CROSS MIS
GREENSPAN MIS
IRVING PRODUCTION
JONES MIS
MCCOY MIS
MCKNIGHT PRODUCTION
ROMANS PRODUCTION
SMITH MIS
PRODUCTION
*** LAST NAME SMITH DOES MATCH MASK
STEVENS PRODUCTION

Calling a Function From a RECAP Command


You can call a function from an FML RECAP command.

Syntax: How to Call a Function From a RECAP Command

RECAP name[(n)|(n,m)|(n,m,i)][/format1] =
function(input1,...,['format2']);

where:
name
Is the name of the calculation.

76
3. Accessing and Calling a Function

n
Displays the value in the column number specified by n. If you omit the column number,
the value appears in all columns.
n,m
Displays the value in all columns beginning with the column number specified by n and
ending with the column number specified by m.
n,m,i
Displays the value in the columns beginning with the column number specified by n and
ending with the column number specified by m by the interval specified by i. For example, if
n is 1, m is 5, and i is 2, the value displays in columns 1, 3, and 5.
format1
Is the format of the calculation. The default value is the format of the report column.
function
Is the function.
input1,...
Are the input arguments, which can include numeric constants, alphanumeric literals, row
and column references (R notation, E notation, or labels), and names of other RECAP
calculations.
format2
Is the format of the output value enclosed in single quotation marks. If the calculation's
format is larger than the column width, the value appears in that column as asterisks.

Example: Calling a Function in a RECAP Command


This request sums the AMOUNT field for account 1010 using the label CASH, account 1020
using the label DEMAND, and account 1030 using the label TIME. The MAX function displays
the maximum value of these accounts.
TABLE FILE LEDGER
SUM AMOUNT FOR ACCOUNT
1010 AS 'CASH ON HAND' LABEL CASH OVER
1020 AS 'DEMAND DEPOSITS' LABEL DEMAND OVER
1030 AS 'TIME DEPOSITS' LABEL TIME OVER
BAR OVER
RECAP MAXCASH = MAX(CASH, DEMAND, TIME); AS 'MAX CASH'
END

Using Functions 77
Storing and Accessing an External Function

The output is:


AMOUNT
------
CASH ON HAND 8,784
DEMAND DEPOSITS 4,494
TIME DEPOSITS 7,961
------
MAX CASH 8,784

Storing and Accessing an External Function


Internal functions are built in and do not require additional work to access. External functions
are stored in load libraries from which they must be retrieved. The way these external functions
are accessed is determined by your platform. These techniques may not have to be used every
time a function is accessed. Access to a load library may be set only once at the time of
installation.

You can also access private user-written subroutines. If you have a private collection of
subroutines (that is, you created your own or use customized subroutines), do not store them
in the function library. Store them separately to avoid overwriting them whenever your site
installs a new release. For more information on creating a subroutine, see Creating a
Subroutine on page 715.

Storing and Accessing a Function on z/OS


On z/OS, load libraries are partitioned data sets containing link-edited modules. These
libraries are stored as EDALIB.LOAD or FUSELIB.LOAD. In addition, your site may have private
subroutine collections stored in separate load libraries. If so, you must allocate those libraries.

Procedure: How to Allocate a Load Library in z/OS Batch


To use a function stored as a load library, allocate the load library to ddname USERLIB in your
JCL or CLIST.

The search order is USERLIB, STEPLIB, JOBLIB, link pack area, and linklist.

The Server also adds the TASKLIB keyword to the search for non-APF authorized libraries. For
more information, see the Server Installation, Configuration and Operations for MVS manual.

Example: Allocating the Load Library BIGLIB.LOAD in z/OS Batch (JCL)


//USERLIB DD DISP=SHR,DSN=BIGLIB.LOAD

78
3. Accessing and Calling a Function

Syntax: How to Allocate a Load Library

DYNAM ALLOC FILE USERLIB DA lib SHR

where:
USERLIB

Is the ddname to which you allocate a load library.


lib1 lib2 lib3...
Are the names of the load libraries, concatenated to ddname USERLIB.

Example: Allocating the FUSELIB.LOAD Load Library


DYNAM ALLOC FILE USERLIB DA MVS.FUSELIB.LOAD SHR

Example: Concatenating a Load Library to USERLIB In TSO


Suppose a report request calls two functions: BENEFIT stored in library SUBLIB.LOAD, and
EXCHANGE stored in library BIGLIB.LOAD. To concatenate the BIGLIB and SUBLIB load libraries
in the allocation for ddname USERLIB, issue the following commands:

DYNAM ALLOC FILE USERLIB DA SUBLIB.LOAD SHR


DYNAM ALLOC FILE BIGLIB DA BIGLIB.LOAD SHR
DYNAM CONCAT FILE USERLIB BIGLIB

The load libraries are searched in the order in which they are specified in the ALLOCATE
command.

Example: Concatenating a Load Library to STEPLIB in Batch (JCL)


Concatenate the load library to the ddname STEPLIB in your JCL:

//FOCUS EXEC PGM=FOCUS


//STEPLIB DD DSN=FOCUS.FOCLIB.LOAD,DISP=SHR
// DD DSN=FOCUS.FUSELIB.LOAD,DISP=SHR
.
.
.

Storing and Accessing a Function on UNIX


No extra work is required.

Storing and Accessing a Function on Windows


No extra work is required.

Using Functions 79
Storing and Accessing an External Function

80
Chapter 4 Simplified Analytic Functions
The analytic functions enable you do perform calculations and retrievals using multiple
rows in the internal matrix.
In this chapter:

FORECAST_MOVAVE: Using a Simple INCREASE: Calculating the Difference


Moving Average Between the Current and a Prior Value of
a Field
FORECAST_EXPAVE: Using Single
Exponential Smoothing PCT_INCREASE: Calculating the
Percentage Difference Between the
FORECAST_DOUBLEXP: Using Double
Current and a Prior Value of a Field
Exponential Smoothing
PREVIOUS: Retrieving a Prior Value of a
FORECAST_SEASONAL: Using Triple
Field
Exponential Smoothing
RUNNING_AVE: Calculating an Average
FORECAST_LINEAR: Using a Linear
Over a Group of Rows
Regression Equation
RUNNING_MAX: Calculating a Maximum
PARTITION_AGGR: Creating Rolling
Over a Group of Rows
Calculations
RUNNING_MIN: Calculating a Minimum
PARTITION_REF: Using Prior or
Over a Group of Rows
Subsequent Field Values in Calculations
RUNNING_SUM: Calculating a Sum Over
a Group of Rows

FORECAST_MOVAVE: Using a Simple Moving Average


A simple moving average is a series of arithmetic means calculated with a specified number of
values from a field. Each new mean in the series is calculated by dropping the first value used
in the prior calculation, and adding the next data value to the calculation.

Simple moving averages are sometimes used to analyze trends in stock prices over time. In
this scenario, the average is calculated using a specified number of periods of stock prices. A
disadvantage to this indicator is that because it drops the oldest values from the calculation
as it moves on, it loses its memory over time. Also, mean values are distorted by extreme
highs and lows, since this method gives equal weight to each point.

Using Functions 81
FORECAST_MOVAVE: Using a Simple Moving Average

Predicted values beyond the range of the data values are calculated using a moving average
that treats the calculated trend values as new data points.

The first complete moving average occurs at the nth data point because the calculation
requires n values. This is called the lag. The moving average values for the lag rows are
calculated as follows: the first value in the moving average column is equal to the first data
value, the second value in the moving average column is the average of the first two data
values, and so on until the nth row, at which point there are enough values to calculate the
moving average with the number of values specified.

Syntax: How to Calculate a Simple Moving Average Column

FORECAST_MOVAVE(display, infield, interval,


npredict, npoint1)

where:
display
Keyword

Specifies which values to display for rows of output that represent existing data. Valid
values are:

INPUT_FIELD. This displays the original field values for rows that represent existing
data.

MODEL_DATA. This displays the calculated values for rows that represent existing
data.

Note: You can show both types of output for any field by creating two independent
COMPUTE commands in the same request, each with a different display option.
infield
Is any numeric field. It can be the same field as the result field, or a different field. It
cannot be a date-time field or a numeric field with date display options.
interval
Is the increment to add to each sort field value (after the last data point) to create the
next value. This must be a positive integer. To sort in descending order, use the BY
HIGHEST phrase. The result of adding this number to the sort field values is converted
to the same format as the sort field.
For date fields, the minimal component in the format determines how the number is
interpreted. For example, if the format is YMD, MDY, or DMY, an interval value of 2 is
interpreted as meaning two days. If the format is YM, the 2 is interpreted as meaning two
months.

82
4. Simplified Analytic Functions

npredict
Is the number of predictions for FORECAST to calculate. It must be an integer greater
than or equal to zero. Zero indicates that you do not want predictions, and is only
supported with a non-recursive FORECAST.
npoint1
Is the number of values to average for the MOVAVE method.

Example: Calculating a New Simple Moving Average Column


This request defines an integer value named PERIOD to use as the independent variable for
the moving average. It predicts three periods of values beyond the range of the retrieved data.
The MOVAVE column on the report output shows the calculated moving average numbers for
existing data points.
DEFINE FILE GGSALES
SDATE/YYM = DATE;
SYEAR/Y = SDATE;
SMONTH/M = SDATE;
PERIOD/I2 = SMONTH;
END
TABLE FILE GGSALES
SUM UNITS DOLLARS
COMPUTE MOVAVE/D10.1= FORECAST_MOVAVE(MODEL_DATA, DOLLARS,1,3,3);
BY CATEGORY BY PERIOD
WHERE SYEAR EQ 97 AND CATEGORY NE 'Gifts'
ON TABLE SET STYLE *
GRID=OFF,$
ENDSTYLE
END

Using Functions 83
FORECAST_MOVAVE: Using a Simple Moving Average

The output is:

In the report, the number of values to use in the average is 3 and there are no UNITS or
DOLLARS values for the generated PERIOD values.

Each average (MOVAVE value) is computed using DOLLARS values where they exist. The
calculation of the moving average begins in the following way:

The first MOVAVE value (801,123.0) is equal to the first DOLLARS value.

84
4. Simplified Analytic Functions

The second MOVAVE value (741,731.5) is the mean of DOLLARS values one and two:
(801,123 + 682,340) /2.

The third MOVAVE value (749,513.7) is the mean of DOLLARS values one through three:
(801,123 + 682,340 + 765,078) / 3.

The fourth MOVAVE value (712,897.3) is the mean of DOLLARS values two through four:
(682,340 + 765,078 + 691,274) /3.

For predicted values beyond the supplied values, the calculated MOVAVE values are used as
new data points to continue the moving average. The predicted MOVAVE values (starting with
694,975.6 for PERIOD 13) are calculated using the previous MOVAVE values as new data
points. For example, the first predicted value (694,975.6) is the average of the data points
from periods 11 and 12 (620,264 and 762,328) and the moving average for period 12
(702,334.7). The calculation is: 694,975 = (620,264 + 762,328 + 702,334.7)/3.

Example: Displaying Original Field Values in a Simple Moving Average Column


This request defines an integer value named PERIOD to use as the independent variable for
the moving average. It predicts three periods of values beyond the range of the retrieved data.
It uses the keyword INPUT_FIELD as the first argument in the FORECAST parameter list. The
trend values do not display in the report. The actual data values for DOLLARS are followed by
the predicted values in the report column.
DEFINE FILE GGSALES
SDATE/YYM = DATE;
SYEAR/Y = SDATE;
SMONTH/M = SDATE;
PERIOD/I2 = SMONTH;
END
TABLE FILE GGSALES
SUM UNITS DOLLARS
COMPUTE MOVAVE/D10.1 = FORECAST_MOVAVE(INPUT_FIELD,DOLLARS,1,3,3);
BY CATEGORY BY PERIOD
WHERE SYEAR EQ 97 AND CATEGORY NE 'Gifts'
ON TABLE SET STYLE *
GRID=OFF,$
ENDSTYLE
END

Using Functions 85
FORECAST_MOVAVE: Using a Simple Moving Average

The output is shown in the following image:

86
4. Simplified Analytic Functions

FORECAST_EXPAVE: Using Single Exponential Smoothing


The single exponential smoothing method calculates an average that allows you to choose
weights to apply to newer and older values.

The following formula determines the weight given to the newest value.

k = 2/(1+n)

where:
k
Is the newest value.
n
Is an integer greater than one. Increasing n increases the weight assigned to the earlier
observations (or data instances), as compared to the later ones.
The next calculation of the exponential moving average (EMA) value is derived by the following
formula:

EMA = (EMA * (1-k)) + (datavalue * k)

This means that the newest value from the data source is multiplied by the factor k and the
current moving average is multiplied by the factor (1-k). These quantities are then summed to
generate the new EMA.

Note: When the data values are exhausted, the last data value in the sort group is used as the
next data value.

Syntax: How to Calculate a Single Exponential Smoothing Column

FORECAST_EXPAVE(display, infield, interval,


npredict, npoint1)

where:
display
Keyword

Specifies which values to display for rows of output that represent existing data. Valid
values are:

INPUT_FIELD. This displays the original field values for rows that represent existing
data.

MODEL_DATA. This displays the calculated values for rows that represent existing
data.

Using Functions 87
FORECAST_EXPAVE: Using Single Exponential Smoothing

Note: You can show both types of output for any field by creating two independent
COMPUTE commands in the same request, each with a different display option.
infield
Is any numeric field. It can be the same field as the result field, or a different field. It
cannot be a date-time field or a numeric field with date display options.
interval
Is the increment to add to each sort field value (after the last data point) to create the
next value. This must be a positive integer. To sort in descending order, use the BY
HIGHEST phrase. The result of adding this number to the sort field values is converted
to the same format as the sort field.
For date fields, the minimal component in the format determines how the number is
interpreted. For example, if the format is YMD, MDY, or DMY, an interval value of 2 is
interpreted as meaning two days. If the format is YM, the 2 is interpreted as meaning two
months.
npredict
Is the number of predictions for FORECAST to calculate. It must be an integer greater
than or equal to zero. Zero indicates that you do not want predictions, and is only
supported with a non-recursive FORECAST.
npoint1
For EXPAVE, this number is used to calculate the weights for each component in the
average. This value must be a positive whole number. The weight, k, is calculated by
the following formula:
k=2/(1+npoint1)

Example: Calculating a Single Exponential Smoothing Column


The following defines an integer value named PERIOD to use as the independent variable for
the moving average. It predicts three periods of values beyond the range of retrieved data.
DEFINE FILE GGSALES
SDATE/YYM = DATE;
SYEAR/Y = SDATE;
SMONTH/M = SDATE;
PERIOD/I2 = SMONTH;
END
TABLE FILE GGSALES
SUM UNITS DOLLARS
COMPUTE EXPAVE/D10.1= FORECAST_EXPAVE(MODEL_DATA,DOLLARS,1,3,3);
BY CATEGORY BY PERIOD
WHERE SYEAR EQ 97 AND CATEGORY NE 'Gifts'
ON TABLE SET STYLE *
GRID=OFF,$
ENDSTYLE
END

88
4. Simplified Analytic Functions

The output is shown in the following image:


Category PERIOD Unit Sales Dollar Sales EXPAVE
-------- ------ ---------- ------------ ------
Coffee 1 61666 801123 801,123.0
2 54870 682340 741,731.5
3 61608 765078 753,404.8
4 57050 691274 722,339.4
5 59229 720444 721,391.7
6 58466 742457 731,924.3
7 60771 747253 739,588.7
8 54633 655896 697,742.3
9 57829 730317 714,029.7
10 57012 724412 719,220.8
11 51110 620264 669,742.4
12 58981 762328 716,035.2
13 0 0 739,181.6
14 0 0 750,754.8
15 0 0 756,541.4
Food 1 54394 672727 672,727.0
2 54894 699073 685,900.0
3 52713 642802 664,351.0
4 58026 718514 691,432.5
5 53289 660740 676,086.3
6 58742 734705 705,395.6
7 60127 760586 732,990.8
8 55622 695235 714,112.9
9 55787 683140 698,626.5
10 57340 713768 706,197.2
11 57459 710138 708,167.6
12 57290 705315 706,741.3
13 0 0 706,028.2
14 0 0 705,671.6
15 0 0 705,493.3
In the report, three predicted values of EXPAVE are calculated within each value of CATEGORY.
For values outside the range of the data, new PERIOD values are generated by adding the
interval value (1) to the prior PERIOD value.

Each average (EXPAVE value) is computed using DOLLARS values where they exist. The
calculation of the moving average begins in the following way:

The first EXPAVE value (801,123.0) is the same as the first DOLLARS value.

The second EXPAVE value (741,731.5) is calculated as follows. Note that because of
rounding and the number of decimal places used, the value derived in this sample
calculation varies slightly from the one displayed in the report output:
n=3 (number used to calculate weights)

k = 2/(1+n) = 2/4 = 0.5

EXPAVE = (EXPAVE*(1-k))+(new-DOLLARS*k) = (801123*0.5) + (682340*0.50) =


400561.5 + 341170 = 741731.5

Using Functions 89
FORECAST_DOUBLEXP: Using Double Exponential Smoothing

The third EXPAVE value (753,404.8) is calculated as follows:

EXPAVE = (EXPAVE*(1-k))+(new-DOLLARS*k) = (741731.5*0.5)+(765078*0.50) =


370865.75 + 382539 = 753404.75

FORECAST_DOUBLEXP: Using Double Exponential Smoothing


Double exponential smoothing produces an exponential moving average that takes into
account the tendency of data to either increase or decrease over time without repeating. This
is accomplished by using two equations with two constants.

The first equation accounts for the current time period and is a weighted average of the
current data value and the prior average, with an added component (b) that represents the
trend for the previous period. The weight constant is k:

DOUBLEXP(t) = k * datavalue(t) + (1-k) * ((DOUBLEXP(t-1) + b(t-1))

The second equation is the calculated trend value, and is a weighted average of the
difference between the current and previous average and the trend for the previous time
period. b(t) represents the average trend. The weight constant is g:

b(t) = g * (DOUBLEXP(t)-DOUBLEXP(t-1)) + (1 - g) * (b(t-1))

These two equations are solved to derive the smoothed average. The first smoothed average is
set to the first data value. The first trend component is set to zero. For choosing the two
constants, the best results are usually obtained by minimizing the mean-squared error (MSE)
between the data values and the calculated averages. You may need to use nonlinear
optimization techniques to find the optimal constants.

The equation used for forecasting beyond the data points with double exponential smoothing is

forecast(t+m) = DOUBLEXP(t) + m * b(t)

where:
m
Is the number of time periods ahead for the forecast.

Syntax: How to Calculate a Double Exponential Smoothing Column

FORECAST_DOUBLEXP(display, infield,
interval, npredict, npoint1, npoint2)

90
4. Simplified Analytic Functions

where:
display
Keyword

Specifies which values to display for rows of output that represent existing data. Valid
values are:

INPUT_FIELD. This displays the original field values for rows that represent existing
data.

MODEL_DATA. This displays the calculated values for rows that represent existing
data.

Note: You can show both types of output for any field by creating two independent
COMPUTE commands in the same request, each with a different display option.
infield
Is any numeric field. It can be the same field as the result field, or a different field. It
cannot be a date-time field or a numeric field with date display options.
interval
Is the increment to add to each sort field value (after the last data point) to create the
next value. This must be a positive integer. To sort in descending order, use the BY
HIGHEST phrase. The result of adding this number to the sort field values is converted
to the same format as the sort field.
For date fields, the minimal component in the format determines how the number is
interpreted. For example, if the format is YMD, MDY, or DMY, an interval value of 2 is
interpreted as meaning two days. If the format is YM, the 2 is interpreted as meaning two
months.
npredict
Is the number of predictions for FORECAST to calculate. It must be an integer greater
than or equal to zero. Zero indicates that you do not want predictions, and is only
supported with a non-recursive FORECAST.
npoint1
For DOUBLEXP, this number is used to calculate the weights for each component in
the average. This value must be a positive whole number. The weight, k, is calculated
by the following formula:
k=2/(1+npoint1)

npoint2
For DOUBLEXP, this positive whole number is used to calculate the weights for each
term in the trend. The weight, g, is calculated by the following formula:

Using Functions 91
FORECAST_SEASONAL: Using Triple Exponential Smoothing

g=2/(1+npoint2)

Example: Calculating a Double Exponential Smoothing Column


The following sums the TRANSTOT field of the VIDEOTRK data source by TRANSDATE, and
calculates a single exponential and double exponential moving average. The report columns
show the calculated values for existing data points.
TABLE FILE VIDEOTRK
SUM TRANSTOT
COMPUTE EXP/D15.1 = FORECAST_EXPAVE(MODEL_DATA,TRANSTOT,1,0,3);
DOUBLEXP/D15.1 = FORECAST_DOUBLEXP(MODEL_DATA,TRANSTOT,1,0,3,3);
BY TRANSDATE
WHERE TRANSDATE NE '19910617'
ON TABLE SET STYLE *
GRID=OFF,$
END

The output is shown in the following image:

FORECAST_SEASONAL: Using Triple Exponential Smoothing


Triple exponential smoothing produces an exponential moving average that takes into account
the tendency of data to repeat itself in intervals over time. For example, sales data that is
growing and in which 25% of sales always occur during December contains both trend and
seasonality. Triple exponential smoothing takes both the trend and seasonality into account by
using three equations with three constants.

92
4. Simplified Analytic Functions

For triple exponential smoothing you, need to know the number of data points in each time
period (designated as L in the following equations). To account for the seasonality, a seasonal
index is calculated. The data is divided by the prior season index and then used in calculating
the smoothed average.

The first equation accounts for the current time period, and is a weighted average of the
current data value divided by the seasonal factor and the prior average adjusted for the
trend for the previous period. The weight constant is k:

SEASONAL(t) = k * (datavalue(t)/I(t-L)) + (1-k) * (SEASONAL(t-1) +


b(t-1))

The second equation is the calculated trend value, and is a weighted average of the
difference between the current and previous average and the trend for the previous time
period. b(t) represents the average trend. The weight constant is g:

b(t) = g * (SEASONAL(t)-SEASONAL(t-1)) + (1-g) * (b(t-1))

The third equation is the calculated seasonal index, and is a weighted average of the
current data value divided by the current average and the seasonal index for the previous
season. I(t) represents the average seasonal coefficient. The weight constant is p:

I(t) = p * (datavalue(t)/SEASONAL(t)) + (1 - p) * I(t-L)

These equations are solved to derive the triple smoothed average. The first smoothed average
is set to the first data value. Initial values for the seasonality factors are calculated based on
the maximum number of full periods of data in the data source, while the initial trend is
calculated based on two periods of data. These values are calculated with the following steps:
1. The initial trend factor is calculated by the following formula:

b(0) = (1/L) ((y(L+1)-y(1))/L + (y(L+2)-y(2))/L + ... + (y(2L) -


y(L))/L )

2. The calculation of the initial seasonality factor is based on the average of the data values
within each period, A(j) (1<=j<=N):

A(j) = ( y((j-1)L+1) + y((j-1)L+2) + ... + y(jL) ) / L

3. Then, the initial periodicity factor is given by the following formula, where N is the number
of full periods available in the data, L is the number of points per period and n is a point
within the period (1<= n <= L):

I(n) = ( y(n)/A(1) + y(L+n)/A(2) + ... + y((N-1)L+n)/A(N) ) / N

Using Functions 93
FORECAST_SEASONAL: Using Triple Exponential Smoothing

The three constants must be chosen carefully. The best results are usually obtained by
choosing the constants to minimize the mean-squared error (MSE) between the data values
and the calculated averages. Varying the values of npoint1 and npoint2 affect the results, and
some values may produce a better approximation. To search for a better approximation, you
may want to find values that minimize the MSE.

The equation used to forecast beyond the last data point with triple exponential smoothing is:
forecast(t+m) = (SEASONAL(t) + m * b(t)) / I(t-L+MOD(m/L))

where:
m
Is the number of periods ahead for the forecast.

Syntax: How to Calculate a Triple Exponential Smoothing Column

FORECAST_SEASONAL(display, infield,
interval, npredict, nperiod, npoint1, npoint2, npoint3)

where:
display
Keyword

Specifies which values to display for rows of output that represent existing data. Valid
values are:

INPUT_FIELD. This displays the original field values for rows that represent existing
data.

MODEL_DATA. This displays the calculated values for rows that represent existing
data.

Note: You can show both types of output for any field by creating two independent
COMPUTE commands in the same request, each with a different display option.
infield
Is any numeric field. It can be the same field as the result field, or a different field. It
cannot be a date-time field or a numeric field with date display options.
interval
Is the increment to add to each sort field value (after the last data point) to create the
next value. This must be a positive integer. To sort in descending order, use the BY
HIGHEST phrase. The result of adding this number to the sort field values is converted
to the same format as the sort field.

94
4. Simplified Analytic Functions

For date fields, the minimal component in the format determines how the number is
interpreted. For example, if the format is YMD, MDY, or DMY, an interval value of 2 is
interpreted as meaning two days. If the format is YM, the 2 is interpreted as meaning two
months.
npredict
Is the number of predictions for FORECAST to calculate. It must be an integer greater
than or equal to zero. Zero indicates that you do not want predictions, and is only
supported with a non-recursive FORECAST. For the SEASONAL method, npredict is the
number of periods to calculate. The number of points generated is:
nperiod * npredict

nperiod
For the SEASONAL method, is a positive whole number that specifies the number of
data points in a period.
npoint1
For SEASONAL, this number is used to calculate the weights for each component in
the average. This value must be a positive whole number. The weight, k, is calculated
by the following formula:
k=2/(1+npoint1)

npoint2
For SEASONAL, this positive whole number is used to calculate the weights for each
term in the trend. The weight, g, is calculated by the following formula:
g=2/(1+npoint2)

npoint3
For SEASONAL, this positive whole number is used to calculate the weights for each
term in the seasonal adjustment. The weight, p, is calculated by the following formula:
p=2/(1+npoint3)

Using Functions 95
FORECAST_SEASONAL: Using Triple Exponential Smoothing

Example: Calculating a Triple Exponential Smoothing Column


In the following, the data has seasonality but no trend. Therefore, npoint2 is set high (1000) to
make the trend factor negligible in the calculation:
TABLE FILE VIDEOTRK
SUM TRANSTOT
COMPUTE SEASONAL/D10.1 = FORECAST_SEASONAL(MODEL_DATA,TRANSTOT,
1,3,3,3,1000,1);
BY TRANSDATE
WHERE TRANSDATE NE '19910617'
ON TABLE SET STYLE *
GRID=OFF,$
ENDSTYLE
END

In the output, npredict is 3. Therefore, three periods (nine points, nperiod * npredict) are
generated.

96
4. Simplified Analytic Functions

FORECAST_LINEAR: Using a Linear Regression Equation


The linear regression equation estimates values by assuming that the dependent variable (the
new calculated values) and the independent variable (the sort field values) are related by a
function that represents a straight line:
y = mx + b

where:
y
Is the dependent variable.
x
Is the independent variable.
m
Is the slope of the line.
b
Is the y-intercept.
FORECAST_LINEAR uses a technique called Ordinary Least Squares to calculate values for m
and b that minimize the sum of the squared differences between the data and the resulting
line.

The following formulas show how m and b are calculated.

where:
n
Is the number of data points.
y
Is the data values (dependent variables).
x
Is the sort field values (independent variables).
Trend values, as well as predicted values, are calculated using the regression line equation.

Using Functions 97
FORECAST_LINEAR: Using a Linear Regression Equation

Syntax: How to Calculate a Linear Regression Column

FORECAST_LINEAR(display, infield, interval,


npredict)

where:
display
Keyword

Specifies which values to display for rows of output that represent existing data. Valid
values are:

INPUT_FIELD. This displays the original field values for rows that represent existing
data.

MODEL_DATA. This displays the calculated values for rows that represent existing
data.

Note: You can show both types of output for any field by creating two independent
COMPUTE commands in the same request, each with a different display option.
infield
Is any numeric field. It can be the same field as the result field, or a different field. It
cannot be a date-time field or a numeric field with date display options.
interval
Is the increment to add to each sort field value (after the last data point) to create the
next value. This must be a positive integer. To sort in descending order, use the BY
HIGHEST phrase. The result of adding this number to the sort field values is converted
to the same format as the sort field.
For date fields, the minimal component in the format determines how the number is
interpreted. For example, if the format is YMD, MDY, or DMY, an interval value of 2 is
interpreted as meaning two days. If the format is YM, the 2 is interpreted as meaning two
months.
npredict
Is the number of predictions for FORECAST to calculate. It must be an integer greater
than or equal to zero. Zero indicates that you do not want predictions, and is only
supported with a non-recursive FORECAST.

98
4. Simplified Analytic Functions

Example: Calculating a New Linear Regression Field


The following request calculates a regression line using the VIDEOTRK data source of
QUANTITY by TRANSDATE. The interval is one day, and three predicted values are calculated.
TABLE FILE VIDEOTRK
SUM QUANTITY
COMPUTE FORTOT=FORECAST_LINEAR(MODEL_DATA,QUANTITY,1,3);
BY TRANSDATE
ON TABLE SET PAGE NOLEAD
ON TABLE SET STYLE *
GRID=OFF,$
ENDSTYLE
END

The output is shown in the following image:

Note:

Three predicted values of FORTOT are calculated. For values outside the range of the data,
new TRANSDATE values are generated by adding the interval value (1) to the prior
TRANSDATE value.
There are no QUANTITY values for the generated FORTOT values.
Each FORTOT value is computed using a regression line, calculated using all of the actual
data values for QUANTITY.

Using Functions 99
PARTITION_AGGR: Creating Rolling Calculations

TRANSDATE is the independent variable (x) and QUANTITY is the dependent variable (y).
The equation is used to calculate QUANTITY FORECAST trend and predicted values.

The following version of the request charts the data values and the regression line.
GRAPH FILE VIDEOTRK
SUM QUANTITY
COMPUTE FORTOT=FORECAST_LINEAR(MODEL_DATA,QUANTITY,1,3);
BY TRANSDATE
ON GRAPH PCHOLD FORMAT JSCHART
ON GRAPH SET LOOKGRAPH VLINE
END

The output is shown in the following image.

PARTITION_AGGR: Creating Rolling Calculations


Using the PARTITION_AGGR function, you can generate rolling calculations based on a block of
rows from the internal matrix of a TABLE request. In order to determine the limits of the rolling
calculations, you specify a partition of the data based on either a sort field or the entire TABLE.
Within either type of break, you can start calculating from the beginning of the break or a
number of rows prior to or subsequent to the current row. You can stop the rolling calculation
at the current row, a row past the start point, or the end of the partition.

100
4. Simplified Analytic Functions

By default, the field values used in the calculations are the summed values of a measure in
the request. Certain prefix operators can be used to add a column to the internal matrix and
use that column in the rolling calculations. The rolling calculation can be SUM, AVE, CNT, MIN,
MAX, FST, or LST.

Syntax: How to Generate Rolling Calculations Using PARTITION_AGGR

PARTITION_AGGR([prefix.]measure,reset_key,lower,upper,operation)

where:
prefix.
Defines an aggregation operator to apply to the measure before using it in the rolling
calculation. Valid operators are:

SUM. which calculates the sum of the measure field values. SUM is the default
operator.

CNT. which calculates a count of the measure field values.

AVE. which calculates the average of the measure field values.

MIN. which calculates the minimum of the measure field values.

MAX. which calculates the maximum of the measure field values.

FST. which retrieves the first value of the measure field.

LST. which retrieves the last value of the measure field.

STDP. which calculates the population standard deviation.

STDS. which calculates the sample standard deviation.

Note: The operators PCT., RPCT., TOT., MDN., and DST. are not supported. COMPUTEs
that reference those unsupported operators are also not supported.
measure
Is the measure field to be aggregated. It can be a real field in the request or a calculated
value generated with the COMPUTE command, as long as the COMPUTE does not
reference an unsupported prefix operator.
reset_key
Identifies the point at which the calculation restarts. Valid values are:

The name of a sort field in the request.

Using Functions 101


PARTITION_AGGR: Creating Rolling Calculations

PRESET, which uses the value of the PARTITION_ON parameter, as described in How to
Specify the Partition Size for Simplified Statistical Functions on page 589.

TABLE, which indicates that there is no break on a sort field.

The sort field may use BY HIGHEST to indicate a HIGH-TO-LOW sort. ACROSS COLUMNS
AND is supported. BY ROWS OVER and FOR are not supported.
lower
Identifies the starting point for the rolling calculation. Valid values are:

n, -n, which starts the calculation n rows forward or back from the current row.

B, which starts the calculation at the beginning of the current sort break (the first line
with the same sort field value as the current line).
upper
Identifies the ending point of the rolling calculation. The lower row value must precede
upper row value.

Valid values are:

C, which ends the rolling calculation at the current row in the internal matrix.

n, -n, which ends the calculation n rows forward or back from the current row.

E, which ends the rolling calculation at the end of the sort break (the last line with the
same sort value as the current row.)

Note: The values used in the calculations depend on the sort sequence (ascending or
descending) specified in the request. Be aware that displaying a date or time dimension in
descending order may produce different results than those you may expect.
operation
Specifies the rolling calculation used on the values in the internal matrix. Supported
operations are:

SUM, which calculates a rolling sum.

AVE, which calculates a rolling average.

CNT, which counts the rows in the partition.

MEDIAN.

MIN, which returns the minimum value in the partition.

MAX, which returns the maximum value in the partition.

102
4. Simplified Analytic Functions

MEDIAN, which returns the median value in the partition.

MODE, which returns the mode value in the partition.

FST, which returns the first value in the partition.

LST, which returns the last value in the partition.

STDP, which returns the population standard deviation in the partition. Requires using
the verb PRINT to avoid duplicate aggregation.

STDS, which returns the sample standard deviation in the partition. Requires using the
verb PRINT to avoid duplicate aggregation.

The calculation is performed prior to any WHERE TOTAL tests, but after any WHERE_GROUPED
tests.

Example: Calculating a Rolling Average


The following request calculates a rolling average of the current line and the previous line in
the internal matrix, within the quarter.
TABLE FILE WF_RETAIL_LITE
SUM COGS_US
COMPUTE AVE1/D12.2M = PARTITION_AGGR(COGS_US, TIME_QTR, -1, C, AVE);
BY BUSINESS_REGION
BY TIME_QTR
BY TIME_MTH
WHERE BUSINESS_REGION EQ 'North America' OR 'South America'
ON TABLE SET PAGE NOLEAD
ON TABLE SET STYLE *
GRID=OFF,$
ENDSTYLE
END

Using Functions 103


PARTITION_AGGR: Creating Rolling Calculations

The output is shown in the following image. Within each quarter, the first average is just the
value from Q1, as going back 1 would cross a boundary. The second average is calculated
using the first two rows within that quarter, and the third average is calculated using rows 2
and 3 within the quarter.

The following changes the rolling average to start from the beginning of the sort break.
COMPUTE AVE1/D12.2M = PARTITION_AGGR(COGS_US, TIME_QTR ,B, C, AVE);

104
4. Simplified Analytic Functions

The output is shown in the following image. Within each quarter, the first average is just the
value from Q1, as going back would cross a boundary. The second average is calculated using
the first two rows within that quarter, and the third average is calculated using rows 1 through
3 within the quarter.

The following command uses the partition boundary TABLE.


COMPUTE AVE1/D12.2M = PARTITION_AGGR(COGS_US, TABLE, B, C, AVE);

Using Functions 105


PARTITION_AGGR: Creating Rolling Calculations

The output is shown in the following image. The rolling average keeps adding the next row to
the average with no sort field break.

Reference: Usage Notes for PARTITION_AGGR


Fields referenced in the PARTITION_AGGR parameters but not previously mentioned in the
request will not be counted in column notation or propagated to HOLD files.

Using the WITHIN phrase for a sum is the same as computing PARTITION_AGGR on the
WITHIN sort field from B (beginning of sort break) to E (end of sort break) using SUM, as in
the following example.

106
4. Simplified Analytic Functions

TABLE FILE WF_RETAIL_LITE


SUM COGS_US WITHIN TIME_QTR AS 'WITHIN Qtr'
COMPUTE PART_WITHIN_QTR/D12.2M = PARTITION_AGGR(COGS_US, TIME_QTR, B, E,
SUM);
BY BUSINESS_REGION AS Region
BY TIME_QTR
BY TIME_MTH
WHERE BUSINESS_REGION EQ 'North America' OR 'South America'
ON TABLE SET PAGE NOPAGE
ON TABLE SET STYLE *
GRID=OFF,$
ENDSTYLE
END

The output is shown in the following image.

Using Functions 107


PARTITION_AGGR: Creating Rolling Calculations

With other types of calculations, the results are not the same. For example, the following
request calculates the average within quarter using the WITHIN phrase and the average
within quarter using PARTITION_AGGR.
TABLE FILE WF_RETAIL_LITE
SUM COGS_US AS Cost
CNT.COGS_US AS Count AVE.COGS_US WITHIN TIME_QTR AS 'Ave Within'
COMPUTE PART_WITHIN_QTR/D12.2M = PARTITION_AGGR(COGS_US, TIME_QTR, B, E,
AVE);
BY BUSINESS_REGION AS Region
BY TIME_QTR
ON TIME_QTR SUBTOTAL COGS_US CNT.COGS_US
BY TIME_MTH
WHERE BUSINESS_REGION EQ 'North America'
ON TABLE SET PAGE NOPAGE
ON TABLE SET STYLE *
GRID=OFF,$
ENDSTYLE
END

108
4. Simplified Analytic Functions

The output is shown in the following image. The average using the WITHIN phrase divides
the total cost for the quarter by the total count of instances for the quarter (for example,
$76,829,105.00/ 252850 = $303.85), while PARTITION_AGGR divides the total cost for
the quarter by the number of report rows in the quarter (for example, $76,829,105.00/3 =
$25,609,701.67).

If you use PARTITION_AGGR to perform operations for specific time periods using an offset,
for example, an operation on the quarters for different years, you must make sure that
every quarter is represented. If some quarters are missing for some years, the offset will
not access the correct data. In this case, generate a HOLD file that has every quarter
represented for every year (you can use BY QUARTER ROWS OVER 1 OVER 2 OVER 3 OVER
4) and use PARTITION_AGGR on the HOLD file.

Using Functions 109


PARTITION_REF: Using Prior or Subsequent Field Values in Calculations

PARTITION_REF: Using Prior or Subsequent Field Values in Calculations


Use of LAST in a calculation retrieves the LAST value of the specified field the last time this
calculation was performed. In contrast, the PARTITION_REF function enables you to specify
both how many rows back or forward to go in the output in order to retrieve a value, and a sort
break within which the retrieval will be contained.

Syntax: How to Retrieve Prior or Subsequent Field Values for Use in a Calculation

PARTITION_REF([prefix.]field, reset_key, offset)

where:
prefix
Is optional. If used, it can be one of the following aggregation operators:

AVE. Average

MAX. Maximum

MIN. Minimum

CNT. Count

SUM. Sum
field
Is the field whose value is to be retrieved.
reset_key
Identifies the point at which the retrieval break restarts. Valid values are:

The name of a sort field in the request.

PRESET, which uses the value of the PARTITION_ON parameter, as described in How to
Specify the Partition Size for Simplified Statistical Functions on page 589.

TABLE, which indicates that there is no break on a sort field.

The sort field may use BY HIGHEST to indicate a HIGH-TO-LOW sort. ACROSS COLUMNS
AND is supported. BY ROWS OVER and FOR are not supported.

Note: The values used in the retrieval depend on the sort sequence (ascending or
descending) specified in the request. Be aware that displaying a date or time dimension in
descending order may produce different results than those you may expect.

110
4. Simplified Analytic Functions

offset
Is the integer number of records to go forward (for a positive offset) or backward (for a
negative offset) to retrieve the value.

If the offset is prior to the partition boundary sort value, the return will be the default value
for the field. The calculation is performed prior to any WHERE TOTAL tests, but after
WHERE_GROUPED tests.

Example: Retrieving a Previous Record With PARTITION_REF


The following request retrieves the previous record within the sort field PRODUCT_CATEGORY.
TABLE FILE WF_RETAIL_LITE
SUM DAYSDELAYED
COMPUTE NEWDAYS/I5=PARTITION_REF(DAYSDELAYED, PRODUCT_CATEGORY, -1);
BY PRODUCT_CATEGORY
BY PRODUCT_SUBCATEG
ON TABLE SET PAGE NOPAGE
ON TABLE SET STYLE *
GRID=OFF,$
ENDSTYLE
END

Using Functions 111


PARTITION_REF: Using Prior or Subsequent Field Values in Calculations

The output is shown in the following image. The first value within each sort break is zero
because there is no prior record to retrieve.

The following request retrieves the average cost of goods from two records prior to the current
record within the PRODUCT_CATEGORY sort field.
TABLE FILE WF_RETAIL_LITE
SUM COGS_US AVE.COGS_US AS Average
COMPUTE PartitionAve/D12.2M=PARTITION_REF(AVE.COGS_US, PRODUCT_CATEGORY,
-2);
BY PRODUCT_CATEGORY
BY PRODUCT_SUBCATEG
ON TABLE SET PAGE NOPAGE
ON TABLE SET STYLE *
GRID=OFF,$
ENDSTYLE
END

112
4. Simplified Analytic Functions

The output is shown in the following image.

Using Functions 113


INCREASE: Calculating the Difference Between the Current and a Prior Value of a Field

Replacing the function call with the following syntax changes the partition boundary to TABLE.
COMPUTE PartitionAve/D12.2M=PARTITION_REF(AVE.COGS_US, TABLE, -2);

The output is shown in the following image.

Reference: Usage Notes for PARTITION_REF


Fields referenced in the PARTITION_REF parameters but not previously mentioned in the
request, will not be counted in column notation or propagated to HOLD files.

INCREASE: Calculating the Difference Between the Current and a Prior Value of a Field
Given an aggregated input field and a negative offset, INCREASE calculates the difference
between the value in the current row of the report output and one or more prior rows, within a
sort break or the entire table. The reset point for the calculation is determined by the value of
the PARTITION_ON parameter described in How to Specify the Partition Size for Simplified
Statistical Functions on page 589.

114
4. Simplified Analytic Functions

Note: The values used in the calculations depend on the sort sequence (ascending or
descending) specified in the request. Be aware that displaying a date or time dimension in
descending order may produce different results than those you may expect.

Syntax: How to Calculate the Difference Between the Current and a Prior Value of a Field

INCREASE([prefix.]field, offset)

where:
prefix
Is one of the following optional aggregation operators to apply to the field before using it in
the calculation:

SUM. which calculates the sum of the field values. SUM is the default value.

CNT. which calculates a count of the field values.

AVE. which calculates the average of the field values.

MIN. which calculates the minimum of the field values.

MAX. which calculates the maximum of the field values.

FST. which retrieves the first value of the field.

LST. which retrieves the last value of the field.


field
Numeric

Is the field to be used in the calculation.


offset
Numeric

Is a negative number indicating the number of rows back from the current row to use for
the calculation.

Using Functions 115


INCREASE: Calculating the Difference Between the Current and a Prior Value of a Field

Example: Calculating the Increase Between the Current and a Prior Value of a Field
The following request uses the default value of SET PARTITION_ON (PENULTIMATE) to calculate
the increase within the PRODUCT_CATEGORY sort field between the current row and the
previous row.
SET PARTITION_ON=PENULTIMATE
TABLE FILE wf_retail_lite
SUM QUANTITY_SOLD
COMPUTE INC = INCREASE(QUANTITY_SOLD,-1);
BY PRODUCT_CATEGORY
BY PRODUCT_SUBCATEG
ON TABLE SET PAGE NOLEAD
ON TABLE SET STYLE *
GRID=OFF,$
ENDSTYLE
END

116
4. Simplified Analytic Functions

The output is shown in the following image. The first value for INC is the value in the
Accessories category for Quantity Sold, as there is no prior value. The second value for INC is
the difference between the values for Headphones and Charger, the third is the difference
between Universal Remote Controls and Headphones. Then, the calculations start over for
Camcorder, which is the reset point.

Using Functions 117


PCT_INCREASE: Calculating the Percentage Difference Between the Current and a Prior Value of a Field

PCT_INCREASE: Calculating the Percentage Difference Between the Current and a Prior
Value of a Field
Given an aggregated input field and a negative offset, PCT_INCREASE calculates the
percentage difference between the value in the current row of the report output and one or
more prior rows, within a sort break or the entire table. The reset point for the calculation is
determined by the value of the PARTITION_ON parameter described in How to Specify the
Partition Size for Simplified Statistical Functions on page 589.

The percentage increase is calculated using the following formula:


(current_value - prior_value) / prior_value

Note: The values used in the calculations depend on the sort sequence (ascending or
descending) specified in the request. Be aware that displaying a date or time dimension in
descending order may produce different results than those you may expect.

Syntax: How to Calculate the Percentage Difference Between the Current and a Prior Value of
a Field

PCT_INCREASE([prefix.]field, offset)

where:
prefix
Is one of the following optional aggregation operators to apply to the field before using it in
the calculation:

SUM. which calculates the sum of the field values. SUM is the default value.

CNT. which calculates a count of the field values.

AVE. which calculates the average of the field values.

MIN. which calculates the minimum of the field values.

MAX. which calculates the maximum of the field values.

FST. which retrieves the first value of the field.

LST. which retrieves the last value of the field.


field
Numeric

The field to be used in the calculation.

118
4. Simplified Analytic Functions

offset
Numeric

Is a negative number indicating the number of rows back from the current row to use for
the calculation.

Example: PCT_INCREASE: Calculating the Percent Increase Between the Current and a Prior
Value of a Field
The following request uses the default value of SET PARTITION_ON (PENULTIMATE) to calculate
the percent increase within the PRODUCT_CATEGORY sort field between the current row and
the previous row.
SET PARTITION_ON=PENULTIMATE
TABLE FILE wf_retail_lite
SUM QUANTITY_SOLD
COMPUTE PCTINC/D8.2p = PCT_INCREASE(QUANTITY_SOLD,-1);
BY PRODUCT_CATEGORY
BY PRODUCT_SUBCATEG
ON TABLE SET PAGE NOLEAD
ON TABLE SET STYLE *
GRID=OFF,$
ENDSTYLE
END

Using Functions 119


PCT_INCREASE: Calculating the Percentage Difference Between the Current and a Prior Value of a Field

The output is shown in the following image. The first value for PCTINC is zero percent, as there
is no prior value. The second value for PCTINC is the percent difference between the values for
Headphones and Charger, the third is the percent difference between Universal Remote
Controls and Headphones. Then, the calculations start over for Camcorder, which is the reset
point.

120
4. Simplified Analytic Functions

PREVIOUS: Retrieving a Prior Value of a Field


Given an aggregated input field and a negative offset, PREVIOUS retrieves the value in a prior
row, within a sort break or the entire table. The reset point for the calculation is determined by
the value of the PARTITION_ON parameter described in How to Specify the Partition Size for
Simplified Statistical Functions on page 589.

Note: The values used in the retrieval depend on the sort sequence (ascending or descending)
specified in the request. Be aware that displaying a date or time dimension in descending
order may produce different results than those you may expect.

Syntax: How to Retrieve a Prior Value of a Field

PREVIOUS([prefix.]field, offset)

where:
prefix
Is one of the following optional aggregation operators to apply to the field before using it in
the calculation:

SUM. which calculates the sum of the field values. SUM is the default value.

CNT. which calculates a count of the field values.

AVE. which calculates the average of the field values.

MIN. which calculates the minimum of the field values.

MAX. which calculates the maximum of the field values.

FST. which retrieves the first value of the field.

LST. which retrieves the last value of the field.


field
Numeric or an alphanumeric field that contains all numeric digits.

The field to be used in the calculation.


offset
Numeric

Is a negative number indicating the number of rows back from the current row to use for
the retrieval.

Using Functions 121


PREVIOUS: Retrieving a Prior Value of a Field

Example: Retrieving a Prior Value of a Field


The following request sets the PARITITON_ON parameter to TABLE and retrieves the value of
the QUANTITIY_SOLD field two rows back from the current row.
SET PARTITION_ON=TABLE
TABLE FILE wf_retail_lite
SUM QUANTITY_SOLD
COMPUTE PREV = PREVIOUS(QUANTITY_SOLD,-2);
BY PRODUCT_CATEGORY
BY PRODUCT_SUBCATEG
ON TABLE SET PAGE NOLEAD
ON TABLE SET STYLE *
GRID=OFF,$
ENDSTYLE
END

122
4. Simplified Analytic Functions

The output is shown in the following image. The value of PREV in the first two rows is zero, as
there are no prior rows for retrieval. From then on, each value of PREV is from the
QUANTITY_SOLD value from two rows prior, with no reset points.

RUNNING_AVE: Calculating an Average Over a Group of Rows


Given an aggregated input field and a negative offset, RUNNING_AVE calculates the average of
the values between the current row of the report output and one or more prior rows, within a
sort break or the entire table. The reset point for the calculation is determined by the sort field
specified, the entire table, or the value of the PARTITION_ON parameter described in How to
Specify the Partition Size for Simplified Statistical Functions on page 589.

Using Functions 123


RUNNING_AVE: Calculating an Average Over a Group of Rows

Syntax: How to Calculate Running Average Between the Current and a Prior Value of a Field

RUNNING_AVE(field, reset_key, lower)

where:
field
Numeric

The field to be used in the calculation.


reset_key
Identifies the point at which the running average restarts. Valid values are:

The name of a sort field in the request.

PRESET, which uses the value of the PARTITION_ON parameter, as described in How to
Specify the Partition Size for Simplified Statistical Functions on page 589.

TABLE, which indicates that there is no break on a sort field.

Note: The values used in the calculations depend on the sort sequence (ascending or
descending) specified in the request. Be aware that displaying a date or time dimension in
descending order may produce different results than those you may expect.
lower
Is the starting point in the partition for the running average. Valid values are:

A negative number, which identifies the offset from the current row.

B, which specifies the beginning of the sort group.

Example: Calculating a Running Average


The following request calculates a running average of QUANTITY_SOLD within the
PRODUCT_CATEGORY sort field, always starting from the beginning of the sort break.
TABLE FILE wf_retail_lite
SUM QUANTITY_SOLD
COMPUTE RAVE = RUNNING_AVE(QUANTITY_SOLD,PRODUCT_CATEGORY,B);
BY PRODUCT_CATEGORY
BY PRODUCT_SUBCATEG
ON TABLE SET PAGE NOLEAD
ON TABLE SET STYLE *
GRID=OFF,$
ENDSTYLE
END

124
4. Simplified Analytic Functions

The output is shown in the following image. The first value for RAVE is the value in the
Accessories category for Quantity Sold, as there is no prior value. The second value for RAVE
is the average of the values for Headphones and Charger, the third is the average of the
values for Headphones, Charger, and Universal Remote Controls. Then, the calculations start
over for Camcorder, which is the reset point.

Using Functions 125


RUNNING_MAX: Calculating a Maximum Over a Group of Rows

RUNNING_MAX: Calculating a Maximum Over a Group of Rows


Given an aggregated input field and an offset, RUNNING_MAX calculates the maximum of the
values between the current row of the report output and one or more prior rows, within a sort
break or the entire table. The reset point for the calculation is determined by the sort field
specified, the entire table, or the value of the PARTITION_ON parameter described in How to
Specify the Partition Size for Simplified Statistical Functions on page 589.

Syntax: How to Calculate Running Maximum Between the Current and a Prior Value of a Field

RUNNING_MAX(field, reset_key, lower)

where:
field
Numeric or an alphanumeric field that contains all numeric digits.

The field to be used in the calculation.


reset_key
Identifies the point at which the running maximum restarts. Valid values are:

The name of a sort field in the request.

PRESET, which uses the value of the PARTITION_ON parameter, as described in How to
Specify the Partition Size for Simplified Statistical Functions on page 589.

TABLE, which indicates that there is no break on a sort field.

Note: The values used in the calculations depend on the sort sequence (ascending or
descending) specified in the request. Be aware that displaying a date or time dimension in
descending order may produce different results than those you may expect.
lower
Is the starting point in the partition for the running maximum. Valid values are:

A negative number, which identifies the offset from the current row.

B, which specifies the beginning of the sort group.

126
4. Simplified Analytic Functions

Example: Calculating a Running Maximum


The following request calculates a running maximum for the rows from the beginning of the
table to the current value of QUANTITY_SOLD, with no reset point.
TABLE FILE wf_retail_lite
SUM QUANTITY_SOLD
COMPUTE RMAX = RUNNING_MAX(QUANTITY_SOLD,TABLE,B);
BY PRODUCT_CATEGORY
BY PRODUCT_SUBCATEG
ON TABLE SET PAGE NOLEAD
ON TABLE SET STYLE *
GRID=OFF,$
ENDSTYLE
END

Using Functions 127


RUNNING_MAX: Calculating a Maximum Over a Group of Rows

The output is shown in the following image. The first value for RMAX is the value in the
Accessories category for Quantity Sold, as there is no prior value. The second value for RMAX
is the value for Headphones, as that value is larger. The third value for RMAX is still the value
for Headphones, as that value is larger than the Quantity Sold value in the third row. Since the
maximum value in the table occurs for Blu Ray, that value is repeated on all future rows, as
there is no reset point.

128
4. Simplified Analytic Functions

RUNNING_MIN: Calculating a Minimum Over a Group of Rows


Given an aggregated input field and an offset, RUNNING_MIN calculates the minimum of the
values between the current row of the report output and one or more prior rows, within a sort
break or the entire table. The reset point for the calculation is determined by the sort field
specified, the entire table, or the value of the PARTITION_ON parameter described in How to
Specify the Partition Size for Simplified Statistical Functions on page 589.

Syntax: How to Calculate Running Minimum Between the Current and a Prior Value of a Field

RUNNING_MIN(field, reset_key, lower)

where:
field
Numeric or an alphanumeric field that contains all numeric digits.

The field to be used in the calculation.


reset_key
Identifies the point at which the running minimum restarts. Valid values are:

The name of a sort field in the request.

PRESET, which uses the value of the PARTITION_ON parameter, as described in How to
Specify the Partition Size for Simplified Statistical Functions on page 589.

TABLE, which indicates that there is no break on a sort field.

Note: The values used in the calculations depend on the sort sequence (ascending or
descending) specified in the request. Be aware that displaying a date or time dimension in
descending order may produce different results than those you may expect.
lower
Is the starting point in the partition for the running minimum. Valid values are:

A negative number, which identifies the offset from the current row.

B, which specifies the beginning of the sort group.

Example: Calculating a Running Minimum


The following request calculates a running minimum of QUANTITY_SOLD within the
PRODUCT_CATEGORY sort field (the sort break defined by SET PARTITION_ON =
PENULTIMATE), always starting from the beginning of the sort break.

Using Functions 129


RUNNING_MIN: Calculating a Minimum Over a Group of Rows

SET PARTITION_ON=PENULTIMATE
TABLE FILE wf_retail_lite
SUM QUANTITY_SOLD
COMPUTE RMIN = RUNNING_MIN(QUANTITY_SOLD,PRESET,B);
BY PRODUCT_CATEGORY
BY PRODUCT_SUBCATEG
ON TABLE SET PAGE NOLEAD
ON TABLE SET STYLE *
GRID=OFF,$
ENDSTYLE
END

130
4. Simplified Analytic Functions

The output is shown in the following image. The first value for RMIN is the value in the
Accessories category for Quantity Sold, as there is no prior value. The second value for RMIN
is the value from the first row again (Charger), as that is smaller than the value in the second
row. The third is the same again, as it is still the smallest. Then, the calculations start over for
Camcorder, which is the reset point.

Using Functions 131


RUNNING_SUM: Calculating a Sum Over a Group of Rows

RUNNING_SUM: Calculating a Sum Over a Group of Rows


Given an aggregated input field and an offset, RUNNING_SUM calculates the sum of the values
between the current row of the report output and one or more prior rows, within a sort break or
the entire table. The reset point for the calculation is determined by the sort field specified,
the entire table, or the value of the PARTITION_ON parameter described in How to Specify the
Partition Size for Simplified Statistical Functions on page 589.

Syntax: How to Calculate Running Sum Between the Current and a Prior Value of a Field

RUNNING_SUM(field, reset_key, lower)

where:
field
Numeric

The field to be used in the calculation.


reset_key
Identifies the point at which the running sum restarts. Valid values are:

The name of a sort field in the request.

PRESET, which uses the value of the PARTITION_ON parameter, as described in How to
Specify the Partition Size for Simplified Statistical Functions on page 589.

TABLE, which indicates that there is no break on a sort field.

Note: The values used in the calculations depend on the sort sequence (ascending or
descending) specified in the request. Be aware that displaying a date or time dimension in
descending order may produce different results than those you may expect.
lower
Is the starting point in the partition for the running sum. Valid values are:

A negative number, which identifies the offset from the current row.

B, which specifies the beginning of the sort group.

132
4. Simplified Analytic Functions

Example: Calculating a Running Sum


The following request calculates a running sum of the current value and previous value of
QUANTITY_SOLD within the reset point set by the PARTITION_ON parameter, which is the sort
field PRODUCT_CATEGORY.
SET PARTITION_ON=PENULTIMATE
TABLE FILE wf_retail_lite
SUM QUANTITY_SOLD
COMPUTE RSUM = RUNNING_SUM(QUANTITY_SOLD,PRESET,-1);
BY PRODUCT_CATEGORY
BY PRODUCT_SUBCATEG
ON TABLE SET PAGE NOLEAD
ON TABLE SET STYLE *
GRID=OFF,$
ENDSTYLE
END

Using Functions 133


RUNNING_SUM: Calculating a Sum Over a Group of Rows

The output is shown in the following image. The first value for RSUM is the value in the
Accessories category for Quantity Sold, as there is no prior value. The second value for RSUM
is the sum of the values for Headphones and Charger, the third is the sum of the values for
Headphones and Universal Remote Controls. Then, the calculations start over for Camcorder,
which is the reset point.

134
Chapter 5 Simplified Character Functions
Simplified character functions have streamlined parameter lists, similar to those used by
SQL functions. In some cases, these simplified functions provide slightly different
functionality than previous versions of similar functions.

The simplified functions do not have an output argument. Each function returns a value
that has a specific data type.

When used in a request against a relational data source, these functions are optimized
(passed to the RDBMS for processing).

Note: The simplified character functions are not supported in Maintain Data.
In this chapter:

CHAR_LENGTH: Returning the Length in POSITION: Returning the First Position of


Characters of a String a Substring in a Source String

CONCAT: Concatenating Strings POSITION: Returning the Position of a


Search String in a Source String
DIFFERENCE: Measuring the Phonetic
Similarity Between Character Strings Regular Expression Functions

DIGITS: Converting a Number to a REPEAT: Repeating a String a Given


Character String Number of Times

GET_TOKEN: Extracting a Token Based REPLACE: Replacing a String


on a String of Delimiters RIGHT: Returning Characters From the
INITCAP: Capitalizing the First Letter of Right of a Character String
Each Word in a String RPAD: Right-Padding a Character String
LAST_NONBLANK: Retrieving the Last RTRIM: Removing Blanks From the Right
Field Value That is Neither Blank nor End of a String
Missing
SPACE: Returning a String With a Given
LEFT: Returning Characters From the Left Number of Spaces
of a Character String
SPLIT: Extracting an Element From a
LOWER: Returning a String With All String
Letters Lowercase
SUBSTRING: Extracting a Substring From
a Source String

Using Functions 135


CHAR_LENGTH: Returning the Length in Characters of a String

LPAD: Left-Padding a Character String TOKEN: Extracting a Token From a String

LTRIM: Removing Blanks From the Left TRIM_: Removing a Leading Character,
End of a String Trailing Character, or Both From a String

OVERLAY: Replacing Characters in a UPPER: Returning a String With All


String Letters Uppercase

PATTERNS: Returning a Pattern That


Represents the Structure of the Input
String

CHAR_LENGTH: Returning the Length in Characters of a String


The CHAR_LENGTH function returns the length, in characters, of a string. In Unicode
environments, this function uses character semantics, so that the length in characters may not
be the same as the length in bytes. If the string includes trailing blanks, these are counted in
the returned length. Therefore, if the format source string is type An, the returned value will
always be n.

Syntax: How to Return the Length of a String in Characters

CHAR_LENGTH(string)

where:
string
Alphanumeric

Is the string whose length is returned.

The data type of the returned length value is Integer.

Example: Returning the Length of a String


The following request against the EMPLOYEE data source creates a virtual field named
LASTNAME of type A15V that contains the LAST_NAME with the trailing blanks removed. It then
uses CHAR_LENGTH to return the number of characters.

136
5. Simplified Character Functions

DEFINE FILE EMPLOYEE


LASTNAME/A15V = RTRIM(LAST_NAME);
END
TABLE FILE EMPLOYEE
SUM LAST_NAME NOPRINT AND COMPUTE
NAME_LEN/I3 = CHAR_LENGTH(LASTNAME);
BY LAST_NAME
ON TABLE SET PAGE NOPAGE
END

The output is:


LAST_NAME NAME_LEN
--------- --------
BANNING 7
BLACKWOOD 9
CROSS 5
GREENSPAN 9
IRVING 6
JONES 5
MCCOY 5
MCKNIGHT 8
ROMANS 6
SMITH 5
STEVENS 7

CONCAT: Concatenating Strings


CONCAT concatenates two strings. The output is returned as variable length alphanumeric.

Syntax: How to Concatenate Strings

CONCAT(string1, string2)

where:
string2
Alphanumeric

Is a string to be concatenated.
string1
Alphanumeric

Is a string to be concatenated.

Using Functions 137


CONCAT: Concatenating Strings

Example: Concatenating Strings


The following request concatenates city names with state names. Note that the city and state
names are converted to fixed length alphanumeric fields before concatenation.
DEFINE FILE WF_RETAIL_LITE
CITY/A50 = CITY_NAME;
STATE/A50 = STATE_PROV_NAME;
CONCAT_CS/A100 = CONCAT(CITY,STATE);
END

TABLE FILE WF_RETAIL_LITE


SUM CITY AS City STATE AS State CONCAT_CS AS Concatenation
BY STATE_PROV_NAME NOPRINT
WHERE COUNTRY_NAME EQ 'United States'
WHERE STATE LE 'Louisiana'
ON TABLE SET PAGE NOLEAD
ON TABLE SET STYLE *
GRID=OFF,$
ENDSTYLE
END

138
5. Simplified Character Functions

The output is shown in the following image.

DIFFERENCE: Measuring the Phonetic Similarity Between Character Strings


DIFFERENCE returns an integer value measuring the difference between the SOUNDEX or
METAPHONE values of two character expressions.

Syntax: How to Measure the Phonetic Similarity Between Character String

DIFFERENCE(chrexp1, chrexp2)

where:
chrexp1, chrexp2
Alphanumeric

Are the character strings to be compared.

Using Functions 139


DIFFERENCE: Measuring the Phonetic Similarity Between Character Strings

Zero (0) represents the least similarity. For SOUNDEX, 4 represents the most similarity,
and for METAPHONE, 16 represents the most similarity.

The use of SOUNDEX or METAPHONE depends on the PHONETIC_ALGORITHM setting.


METAPHONE is the default algorithm.

Example: Measuring the Phonetic Similarity Between Character Strings


The following request uses DIFFERENCE with the default phonetic algorithm (METAPHONE) to
compare first names in the data source with the names JOHN and MARY.
TABLE FILE VIDEOTRK
PRINT FIRSTNAME
COMPUTE
JOHN_DIFF/I5 = DIFFERENCE(FIRSTNAME,'JOHN') ;
MARY_DIFF/I5 = DIFFERENCE(FIRSTNAME, 'MARY');
BY LASTNAME NOPRINT
WHERE RECORDLIMIT EQ 30
ON TABLE SET PAGE NOLEAD
ON TABLE SET STYLE *
GRID=OFF,$
ENDSTYLE
END

140
5. Simplified Character Functions

The output is shown in the following image. Note that the names JOANN and JOHN have the
highest scores for matching with JOHN, and that MARCIA, MICHAEL, and MARTHA have the
highest scores for matching with MARY.

Using Functions 141


DIGITS: Converting a Number to a Character String

DIGITS: Converting a Number to a Character String


Given a number, DIGITS converts it to a character string of the specified length. The format of
the field that contains the number must be Integer.

Syntax: How to Convert a Number to a Character String

DIGITS(number,length)

where:
number
Integer

Is the number to be converted, stored in a field with data type Integer.


length
Integer between 1 and 10

Is the length of the returned character string. If length is longer than the number of digits
in the number being converted, the returned value is padded on the left with zeros. If
length is shorter than the number of digits in the number being converted, the returned
value is truncated on the left.

Example: Converting a Number to a Character String


The following request against the WF_RETAIL_LITE data source converts -123.45 and
ID_PRODUCT to character strings:
DEFINE FILE WF_RETAIL_LITE
MEAS1/I8=-123.45;
DIG1/A6=DIGITS(MEAS1,6) ;
DIG2/A6=DIGITS(ID_PRODUCT,6) ;
END
TABLE FILE WF_RETAIL_LITE
PRINT MEAS1 DIG1
ID_PRODUCT DIG2
BY PRODUCT_SUBCATEG
WHERE PRODUCT_SUBCATEG EQ 'Flat Panel TV'
ON TABLE SET PAGE NOPAGE
END

142
5. Simplified Character Functions

The output is:

Using Functions 143


GET_TOKEN: Extracting a Token Based on a String of Delimiters

Reference: Usage Notes for DIGITS


Only I format numbers will be converted. D, P, and F formats generate error messages and
should be converted to I before using the DIGITS function. The limit for the number that can
be converted is 2 GB.

Negative integers are turned into positive integers.

Integer formats with decimal places are truncated.

DIGITS is not supported in Dialogue Manager.

GET_TOKEN: Extracting a Token Based on a String of Delimiters


GET_TOKEN extracts a token (substring) based on a string that can contain multiple
characters, each of which represents a single-character delimiter.

Syntax: How to Extract a Token Based on a String of Delimiters

GET_TOKEN(string, delimiter_string, occurrence)

where:
string
Alphanumeric

Is the input string from which the token will be extracted. This can be an alphanumeric
field or constant.
delimiter_string
Alphanumeric constant

Is a string that contains the list of delimiter characters. For example, '; ,' contains three
delimiter characters, semi-colon, blank space, and comma.
occurrence
Integer constant

Is a positive integer that specifies the token to be extracted. A negative integer will be
accepted in the syntax, but will not extract a token. The value zero (0) is not supported.

144
5. Simplified Character Functions

Example: Extracting a Token Based on a String of Delimiters


The following request defines an input string and two tokens based on a list of delimiters that
contains the characters comma (,), semicolon (;), and slash (/).
DEFINE FILE EMPLOYEE
InputString/A20 = 'ABC,DEF;GHI/JKL';
FirstToken/A20 WITH DEPARTMENT = GET_TOKEN(InputString, ',;/', 1);
FourthToken/A20 WITH DEPARTMENT = GET_TOKEN(InputString, ',;/', 4);
END
TABLE FILE EMPLOYEE
PRINT InputString FirstToken FourthToken
WHERE READLIMIT EQ 1
ON TABLE SET PAGE NOLEAD
ON TABLE SET STYLE *
GRID = OFF,$
END

The output is shown in the following image. The first token was extracted using the comma (,)
as the delimiter. The fourth token was extracted using the slash (/) as the delimiter.

INITCAP: Capitalizing the First Letter of Each Word in a String


INITCAP capitalizes the first letter of each word in an input string and makes all other letters
lowercase. A word starts at the beginning of the string, after a blank space, or after a special
character.

Syntax: How to Capitalize the First Letter of Each Word in a String

INITCAP(input_string)

where:
input_string
Alphanumeric

Is the string to capitalize.

Using Functions 145


LAST_NONBLANK: Retrieving the Last Field Value That is Neither Blank nor Missing

Example: Capitalizing the First Letter of Each Word in a String


The following request changes the last names in the EMPLOYEE data source to initial caps and
capitalizes the first letter after each blank or special character in the NewName field.
TABLE FILE EMPLOYEE
PRINT LAST_NAME AND COMPUTE
Caps1/A30 = INITCAP(LAST_NAME);
NewName/A30 = 'abc,def!ghi'jKL MNO';
Caps2/A30 = INITCAP(NewName);
ON TABLE SET PAGE NOLEAD
ON TABLE SET STYLE *
GRID=OFF,$
ENDSTYLE
END

The output is shown in the following image.

LAST_NONBLANK: Retrieving the Last Field Value That is Neither Blank nor Missing
LAST_NONBLANK retrieves the last field value that is neither blank nor missing. If all previous
values are either blank or missing, LAST_NONBLANK returns a missing value.

Syntax: How to Return the Last Value That is Neither Blank nor Missing

LAST_NONBLANK(field)

146
5. Simplified Character Functions

where:
field
Is the field name whose last non-blank value is to be retrieved. If the current value is not
blank or missing, the current value is returned.

Note: LAST_NONBLANK cannot be used in a compound expression, for example, as part of an


IF condition.

Example: Retrieving the Last Non-Blank Value


Consider the following delimited file named input1.csv that has two fields named FIELD_1 and
FIELD_2.
,
A,
,
,
B,
C,

The input1 Master File follows.


FILENAME=INPUT1, SUFFIX=DFIX ,
DATASET=baseapp/input1.csv(LRECL 15 RECFM V, BV_NAMESPACE=OFF, $
SEGMENT=INPUT1, SEGTYPE=S0, $
FIELDNAME=FIELD_1, ALIAS=E01, USAGE=A1V, ACTUAL=A1V,
MISSING=ON, $
FIELDNAME=FIELD_2, ALIAS=E02, USAGE=A1V, ACTUAL=A1V,
MISSING=ON, $

The input1 Access File follows.


SEGNAME=INPUT1,
DELIMITER=',',
HEADER=NO,
PRESERVESPACE=NO,
CDN=COMMAS_DOT,
CONNECTION=<local>, $

The following request displays the FIELD_1 values and computes the last non-blank value for
each FIELD_1 value.
TABLE FILE baseapp/INPUT1
PRINT FIELD_1 AS Input
COMPUTE
Last_NonBlank/A1 MISSING ON = LAST_NONBLANK(FIELD_1);
ON TABLE SET PAGE NOLEAD
ON TABLE SET STYLE *
GRID=OFF,$
ENDSTYLE
END

Using Functions 147


LEFT: Returning Characters From the Left of a Character String

The output is shown in the following image.

LEFT: Returning Characters From the Left of a Character String


Given a source character string, or an expression that can be converted to varchar (variable-
length alphanumeric), and an integer number, LEFT returns that number of characters from the
left end of the string.

Syntax: How to Return Characters From the Left of a Character String

LEFT(chr_exp, int_exp)

where:
chr_exp
Alphanumeric or an expression that can be converted to variable-length alphanumeric.

Is the source character string.


int_exp
Integer

Is the number of characters to be returned.

148
5. Simplified Character Functions

Example: Returning Characters From the Left of a Character String


The following request computes the length of the first name in the FULLNAME field and returns
that number of characters to FIRST.
TABLE FILE WF_RETAIL_EMPLOYEE
PRINT FULLNAME AND
COMPUTE LEN/I5 = ARGLEN(54, GET_TOKEN(FULLNAME, ' ', 1), LEN); NOPRINT
COMPUTE FIRST/A20 = LEFT(FULLNAME, LEN);
WHERE RECORDLIMIT EQ 20
ON TABLE SET PAGE NOLEAD
ON TABLE SET STYLE *
GRID=OFF,$
ENDSTYLE
END

Using Functions 149


LOWER: Returning a String With All Letters Lowercase

The output is shown in the following image.

LOWER: Returning a String With All Letters Lowercase


The LOWER function takes a source string and returns a string of the same data type with all
letters translated to lowercase.

150
5. Simplified Character Functions

Syntax: How to Return a String With All Letters Lowercase

LOWER(string)

where:
string
Alphanumeric

Is the string to convert to lowercase.

The returned string is the same data type and length as the source string.

Example: Converting a String to Lowercase


In the following request against the EMPLOYEE data source, LOWER converts the LAST_NAME
field to lowercase and stores the result in LOWER_NAME:

TABLE FILE EMPLOYEE


PRINT LAST_NAME AND COMPUTE
LOWER_NAME/A15 = LOWER(LAST_NAME);
ON TABLE SET PAGE NOPAGE
END

The output is:

LAST_NAME LOWER_NAME
--------- ----------
STEVENS stevens
SMITH smith
JONES jones
SMITH smith
BANNING banning
IRVING irving
ROMANS romans
MCCOY mccoy
BLACKWOOD blackwood
MCKNIGHT mcknight
GREENSPAN greenspan
CROSS cross

LPAD: Left-Padding a Character String


LPAD uses a specified character and output length to return a character string padded on the
left with that character.

Syntax: How to Pad a Character String on the Left

LPAD(string, out_length, pad_character)

Using Functions 151


LPAD: Left-Padding a Character String

where:
string
Fixed length alphanumeric

Is a string to pad on the left side.


out_length
Integer

Is the length of the output string after padding.


pad_character
Fixed length alphanumeric

Is a single character to use for padding.

Example: Left-Padding a String


In the following request against the WF_RETAIL data source, LPAD left-pads the
PRODUCT_CATEGORY column with @ symbols:
DEFINE FILE WF_RETAIL
LPAD1/A25 = LPAD(PRODUCT_CATEGORY,25,'@');
DIG1/A4 = DIGITS(ID_PRODUCT,4);
END
TABLE FILE WF_RETAIL
SUM DIG1 LPAD1
BY PRODUCT_CATEGORY
ON TABLE SET PAGE NOPAGE
ON TABLE SET STYLE *
TYPE=DATA,FONT=COURIER,SIZE=11,COLOR=BLUE,$
END

152
5. Simplified Character Functions

The output is:

Reference: Usage Notes for LPAD


To use the single quotation mark (') as the padding character, you must double it and
enclose the two single quotation marks within single quotation marks (LPAD(COUNTRY,
20,''''). You can use an amper variable in quotation marks for this parameter, but you
cannot use a field, virtual or real.

Input can be fixed or variable length alphanumeric.

Output, when optimized to SQL, will always be data type VARCHAR.

If the output is specified as shorter than the original input, the original data will be
truncated, leaving only the padding characters. The output length can be specified as a
positive integer or an unquoted &variable (indicating a numeric).

LTRIM: Removing Blanks From the Left End of a String


The LTRIM function removes all blanks from the left end of a string.

Using Functions 153


OVERLAY: Replacing Characters in a String

Syntax: How to Remove Blanks From the Left End of a String

LTRIM(string)

where:
string
Alphanumeric

Is the string to trim on the left.

The data type of the returned string is AnV, with the same maximum length as the source
string.

Example: Removing Blanks From the Left End of a String


In the following request against the MOVIES data source, the DIRECTOR field is right-justified
and stored in the RDIRECTOR virtual field. Then LTRIM removes leading blanks from the
RDIRECTOR field:
DEFINE FILE MOVIES
RDIRECTOR/A17 = RJUST(17, DIRECTOR, 'A17');
END
TABLE FILE MOVIES
PRINT RDIRECTOR AND
COMPUTE
TRIMDIR/A17 = LTRIM(RDIRECTOR);
WHERE DIRECTOR CONTAINS 'BR'
ON TABLE SET PAGE NOPAGE
END

The output is:


RDIRECTOR TRIMDIR
--------- -------
ABRAHAMS J. ABRAHAMS J.
BROOKS R. BROOKS R.
BROOKS J.L. BROOKS J.L.

OVERLAY: Replacing Characters in a String


Given a starting position, length, source string, and insertion string, OVERLAY replaces the
number of characters defined by length in the source string with the insertion string, starting
from the starting position.

Syntax: How to Replace Characters in a String

OVERLAY(src, ins, start, len)

154
5. Simplified Character Functions

where:
src
Alphanumeric

Is the source string whose characters will be replaced.


ins
Alphanumeric

Is the insertion string with the replacement characters.


start
Numeric

Is the starting position for the replacement in the source string.


len
Numeric

Is the number of characters to replace in the source string with the entire insertion string.

Example: Replacing Characters in a String


The following request replaces the first three characters in the last name with the first four
characters of the first name.
TABLE FILE EMPLOYEE
PRINT
COMPUTE FIRST4/A4 = LEFT(FIRST_NAME,4);
NEWNAME/A20 = OVERLAY(LAST_NAME, FIRST4, 1, 3);
BY LAST_NAME
BY FIRST_NAME
ON TABLE SET PAGE NOLEAD
ON TABLE SET STYLE *
GRID=OFF,$
ENDSTYLE
END

Using Functions 155


PATTERNS: Returning a Pattern That Represents the Structure of the Input String

The output is shown in the following image.

PATTERNS: Returning a Pattern That Represents the Structure of the Input String
PATTERNS returns a string that represents the structure of the input argument. The returned
pattern includes the following characters:

A is returned for any position in the input string that has an uppercase letter.

a is returned for any position in the input string that has a lowercase letter.

9 is returned for any position in the input string that has a digit.

Note that special characters (for example, +-/=%) are returned exactly as they were in the input
string.

The output is returned as variable length alphanumeric.

Syntax: How to Return a String That Represents the Pattern Profile of the Input Argument

PATTERNS(string)

where:
string
Alphanumeric

Is a string whose pattern will be returned.

156
5. Simplified Character Functions

Example: Returning a Pattern Representing an Input String


The following request returns patterns that represent customer addresses.
DEFINE FILE WF_RETAIL_LITE
Address_Pattern/A40V = PATTERNS(ADDRESS_LINE_1);
END

TABLE FILE WF_RETAIL_LITE


PRINT FST.ADDRESS_LINE_1 OVER
Address_Pattern
BY ADDRESS_LINE_1 NOPRINT SKIP-LINE
WHERE COUNTRY_NAME EQ 'United States'
WHERE CITY_NAME EQ 'Houston' OR 'Indianapolis' OR 'Chapel Hill' OR 'Bronx'
ON TABLE SET PAGE NOLEAD
ON TABLE SET STYLE *
GRID=OFF,$
ENDSTYLE
END

Using Functions 157


POSITION: Returning the First Position of a Substring in a Source String

The partial output is shown in the following image. Note that the special characters (#-,) in an
address are represented in the pattern as is.

POSITION: Returning the First Position of a Substring in a Source String


The POSITION function returns the first position (in characters) of a substring in a source
string.

158
5. Simplified Character Functions

Syntax: How to Return the First Position of a Substring in a Source String

POSITION(pattern, string)

where:
pattern
Alphanumeric

Is the substring whose position you want to locate. The string can be as short as a single
character, including a single blank.
string
Alphanumeric

Is the string in which to find the pattern.

The data type of the returned value is Integer.

Example: Returning the First Position of a Substring


In the following request against the EMPLOYEE data source, POSITION determines the position
of the first capital letter I in LAST_NAME and stores the result in I_IN_NAME:
TABLE FILE EMPLOYEE
PRINT LAST_NAME AND COMPUTE
I_IN_NAME/I2 = POSITION('I', LAST_NAME);
ON TABLE SET PAGE NOPAGE
END

The output is:


LAST_NAME I_IN_NAME
--------- ---------
STEVENS 0
SMITH 3
JONES 0
SMITH 3
BANNING 5
IRVING 1
ROMANS 0
MCCOY 0
BLACKWOOD 0
MCKNIGHT 5
GREENSPAN 0
CROSS 0

Using Functions 159


POSITION: Returning the Position of a Search String in a Source String

POSITION: Returning the Position of a Search String in a Source String


Given a search string, a source string, and a starting position, POSITION returns the position of
the search string within the source string. The search starts at the given starting position and
searches from left to right. If the string is not found, POSITION returns zero (0). The search is
case sensitive.

Syntax: How to Return the Position of a Search String in a Source String

POSITION(search, source, start)

where:
search
Alphanumeric

Is the search string.


source
Alphanumeric

Is the source string.


start
Numeric

Is the starting position in the source string for the search.

Example: Returning the Position of a Search String in a Source String


The following request uses POSITION twice, once to find the first occurrence of the character A
after starting position 3 in FULLNAME, and once to find the first occurrence of the character a
after starting position 3 in FULLNAME.
TABLE FILE WF_RETAIL_CUSTOMER
PRINT FULLNAME
COMPUTE POS1/I5 = POSITION('A', FULLNAME, 3) ;
POS2/I5 = POSITION('a', FULLNAME, 3) ;
WHERE RECORDLIMIT EQ 5
ON TABLE SET PAGE NOLEAD
ON TABLE SET STYLE *
GRID=OFF,$
ENDSTYLE
END

160
5. Simplified Character Functions

The output is shown in the following image.

Regular Expression Functions


A regular expression is a sequence of special meta-characters and literal characters that you
can combine to form a search pattern.

Note: You can search online for information about the symbols used to create a regular
expression pattern. For example, Wikipedia has a good introduction at:

https://en.wikipedia.org/wiki/Regular_expression

The following list summarizes common meta-characters used in regular expressions.

. represents any single character

* represents zero or more occurrences

+ represents one or more occurrences

? represents zero or one occurrence

^ represents beginning of line

$ represents end of line

[] represents any one character in the set listed within the brackets

[^] represents any one character not in the set listed within the brackets

| represents the Or operator

\ is the Escape Special Character

() contains a character sequence

Using Functions 161


Regular Expression Functions

For example, the regular expression '^Ste(v|ph)en$' matches values starting with Ste followed
by either ph or v, and ending with en.

Using Regular Expressions on z/OS


On z/OS, depending on the code page you are using, some of the meta-characters used to
create a regular expression may not be interpreted correctly when inserted directly from a
Windows keyboard.

If you are using the Unicode code page 65002, the meta-characters will be interpreted
correctly. In this environment, you need to be sure the files you are referencing, such as
FOCUS data sources, have been built using this code page.

If you are not using a Unicode code page, you can use the CHAR function to return the correct
meta-characters, based on the decimal code for the EBCDIC character. For example, to insert:

The circumflex (^) meta-character, use CHAR(95).

The left bracket ([) meta-character, use CHAR(173).

The right bracket (]) meta-character, use CHAR(189).

The left brace ({) meta-character, use CHAR(192).

The right brace (}) meta-character, use CHAR(208).

Create a Dialogue Manager variable that contains the pattern. To insert the meta-characters,
use the CHAR function, and then use that variable as the argument in the regular expression
function. For example, to generate the regular expression '[AEIOUaeiou]', which matches all
uppercase and lowercase vowels, issue a -SET command similar to the following, which
creates a variable named &VCWSTRING:
-SET &VCWSTRING=CHAR(173) || 'AEIOUaeiou' || CHAR(189);

Then use the &VCWSTRING variable as the regular expression argument in the function call.
For example:
VowelCnt/I5=REGEXP_COUNT(PRODUCT, '&VCWSTRING');

REGEX: Matching a String to a Regular Expression


The REGEX function matches a string to a regular expression and returns true (1) if it matches
and false (0) if it does not match.

A regular expression is a sequence of special characters and literal characters that you can
combine to form a search pattern.

Many references for regular expressions exist on the web.

162
5. Simplified Character Functions

For a basic summary, see the section Summary of Regular Expressions in Chapter 2, Security,
of the Server Administration manual.

Syntax: How to Match a String to a Regular Expression

REGEX(string, regular_expression)

where:
string
Alphanumeric

Is the character string to match.


regular_expression
Alphanumeric

Is a regular expression, enclosed in single quotation marks, constructed using literals and
meta-characters. The following meta-characters are supported

. represents any single character

* represents zero or more occurrences

+ represents one or more occurrences

? represents zero or one occurrence

^ represents beginning of line

$ represents end of line

[] represents any one character in the set listed within the brackets

[^] represents any one character not in the set listed within the brackets

| represents the Or operator

\ is the Escape Special Character

() contains a character sequence

For example, the regular expression '^Ste(v|ph)en$' matches values starting with Ste
followed by either ph or v, and ending with en.

Note: The output value is numeric.

Using Functions 163


Regular Expression Functions

Example: Matching a String Against a Regular Expression


The following request matches the FIRSTNAME field against the regular expression
'PATRIC[(I?)K]', which matches PATRICIA or PATRICK:
DEFINE FILE VIDEOTRK
PNAME/I5=REGEX(FIRSTNAME,'PATRIC[(I?)K]');
END
TABLE FILE VIDEOTRK
PRINT FIRSTNAME PNAME
BY LASTNAME
WHERE LASTNAME GE 'M'
ON TABLE SET PAGE NOLEAD
ON TABLE SET STYLE *
GRID=OFF,$
ENDSTYLE
END

The output is shown in the following image.

The following version of the request runs on z/OS. The variable &REG1 contains the regular
expression string with the circumflex character (^) inserted as CHAR(95), the left bracket
character ([) inserted as CHAR(173), and the right bracket character (]) inserted as CHAR(189).
The other meta-characters are interpreted correctly.

164
5. Simplified Character Functions

-SET &REG1 = CHAR(95) || 'PATRIC' || CHAR(173) ||


- '(I?)K' || CHAR(189);
DEFINE FILE VIDEOTRK
PNAME/I5 = REGEX(FIRSTNAME,'&REG1') ;
END
TABLE FILE VIDEOTRK
PRINT FIRSTNAME PNAME
BY LASTNAME
WHERE LASTNAME GE 'M'
ON TABLE SET PAGE NOLEAD
END

The output follows.

LASTNAME FIRSTNAME PNAME


-------- --------- -----
MCMAHON JOHN 0
MONROE CATHERINE 0
PATRICK 1
NON-MEMBER 0
O'BRIEN DONALD 0
PARKER GLENDA 0
RICHARD 0
RATHER MICHAEL 0
RIESLER LESLIE 0
SPIVEY TOM 0
STANDLER MICHAEL 0
STEWART MAUDE 0
WHITE PATRICIA 1
WILLIAMS KENNETH 0
WILSON KELLY 0
WU MARTHA 0

REGEXP_COUNT: Counting the Number of Matches to a Pattern in a String


REGEXP_COUNT returns the integer count of matches to a specified regular expression pattern
within a source string.

Syntax: How to Count the Number of Matches to a Pattern in a String

REGEXP_COUNT(string, pattern)

where:
string
Alphanumeric

Is the input string to be searched.


pattern
Alphanumeric

Using Functions 165


Regular Expression Functions

Is a regular expression, enclosed in single quotation marks, constructed using literals and
meta-characters. The following meta-characters are supported

. represents any single character

* represents zero or more occurrences

+ represents one or more occurrences

? represents zero or one occurrence

^ represents beginning of line

$ represents end of line

[] represents any one character in the set listed within the brackets

[^] represents any one character not in the set listed within the brackets

| represents the Or operator

\ is the Escape Special Character

() contains a character sequence

Example: Counting the Number of Matches to a Pattern in a String


The following example uses the following Regular Expression symbols.

$, which searches for a specified expression that occurs at the end of a string.

^, which searches for a specified expression that occurs at the beginning of a string.

\s*, which matches any number of whitespace characters, such as blank characters.

[T,t], which matches the characters 'T' and 't'.

In the following request, REG1 is the number of occurrences of the expression 'iscotti', with
any number of following whitespace characters, that occur the end of the PRODUCT field. REG2
is the number of occurrences of the characters 'T' and 't' in the PRODUCT field.
TABLE FILE GGSALES
SUM DOLLARS AND COMPUTE
REG1/I5 = REGEXP_COUNT(PRODUCT, 'iscotti\s*$');
REG2/I5 = REGEXP_COUNT(PRODUCT, '[T,t]');
BY PRODUCT
ON TABLE SET PAGE NOLEAD
ON TABLE SET STYLE *
GRID=OFF,$
ENDSTYLE
END

166
5. Simplified Character Functions

The output is shown in the following image.

Example: Using REGEXP_COUNT on Windows and z/OS


The following request uses REGEXP_COUNT to return the number of vowels and number of
consonants in each product name, on Windows. VowelCnt is the count of vowels, and
ConsonantCnt is the count of non-vowels.
DEFINE FILE GGSALES
VowelCnt/I5=REGEXP_COUNT(PRODUCT,'[AEIOUaeiou]');
ConsonantCnt/I5=REGEXP_COUNT(PRODUCT,'[^AEIOUaeiou]');
END
TABLE FILE GGSALES
SUM MAX.VowelCnt AS 'Vowels'
MAX.ConsonantCnt AS 'Consonants'
BY PRODUCT
END

Note:

Brackets are used to enclose a list of characters that will match the regular expression
pattern.

When the circumflex character (^) prefaces the list of characters within the brackets, the
regular expression matches any character not on the list.

Using Functions 167


Regular Expression Functions

The output is shown in the following image.

The following version of the request uses REGEXP_COUNT to return the number of vowels and
number of consonants in each product name, on z/OS. The -SET commands create the regular
expressions by using the CHAR function to insert the meta-characters into the expressions.
VowelCnt is the count of vowels, and ConsonantCnt is the count of non-vowels.
-SET &VCWSTRING=CHAR(173) || 'AEIOUaeiou' || CHAR(189);
-SET &CONSTRING=CHAR(173) || CHAR(95) || 'AEIOU aeiou' || CHAR(189);
DEFINE FILE GGSALES
VowelCnt/I5=REGEXP_COUNT(PRODUCT, '&VCWSTRING');
ConsonantCnt/I5=REGEXP_COUNT(PRODUCT,'&CONSTRING');
END
TABLE FILE GGSALES
SUM MAX.VowelCnt AS 'Vowels'
MAX.ConsonantCnt AS 'Consonants'
BY PRODUCT
ON TABLE SET PAGE NOLEAD
END

168
5. Simplified Character Functions

The output follows.

Product Vowels Consonants


------- ------ ----------
Biscotti 3 5
Capuccino 4 5
Coffee Grinder 5 8
Coffee Pot 4 5
Croissant 3 6
Espresso 3 5
Latte 2 3
Mug 1 2
Scone 2 3
Thermos 2 5

REGEXP_INSTR: Returning the First Position of a Pattern in a String


REGEXP_INSTR returns the integer position of the first match to a specified regular expression
pattern within a source string. The first character position in a string is indicated by the value
1. If there is no match within the source string, the value 0 is returned.

Syntax: How to Return the Position of a Pattern in a String

REGEXP_INSTR(string, pattern)

where:
string
Alphanumeric

Is the input string to be searched.


pattern
Alphanumeric

Is a regular expression, enclosed in single quotation marks, constructed using literals and
meta-characters. The following meta-characters are supported

. represents any single character

* represents zero or more occurrences

+ represents one or more occurrences

? represents zero or one occurrence

^ represents beginning of line

$ represents end of line

Using Functions 169


Regular Expression Functions

[] represents any one character in the set listed within the brackets

[^] represents any one character not in the set listed within the brackets

| represents the Or operator

\ is the Escape Special Character

() contains a character sequence

Example: Finding the Position of a Pattern in a String


The following example uses the following Regular Expression symbols.

$, which searches for a specified expression that occurs at the end of a string.

^, which searches for a specified expression that occurs at the beginning of a string.

\s*, which matches any number of whitespace characters, such as blank characters.

[B,C,S], which matches the uppercase letters B, C, and S.

In the following request, REG1 is the position of the expression 'iscotti', with any number of
following whitespace characters, that occur the end of the PRODUCT field value. REG2 is the
position of the characters 'B' ,C, or 'S' that occur at the beginning of the PRODUCT field value.
TABLE FILE GGSALES
SUM DOLLARS AND COMPUTE
REG1/I5 = REGEXP_INSTR(PRODUCT, 'iscotti\s*$');
REG2/I5 = REGEXP_INSTR(PRODUCT, '^[B,C,S]');
BY PRODUCT
ON TABLE SET PAGE NOLEAD
ON TABLE SET STYLE *
GRID=OFF,$
ENDSTYLE
END

170
5. Simplified Character Functions

The output is shown in the following image.

The following version of the request runs on z/OS. The first regular expression can be input
directly because the characters used are interpreted correctly. For the second regular
expression, a variable is created to contain the pattern. This variable is then used in the
function call.

-SET &REG2STR=CHAR(95) || CHAR(173) || 'B,C,S' || CHAR(189);


TABLE FILE GGSALES
SUM DOLLARS AND COMPUTE
REG1/I5 = REGEXP_INSTR(PRODUCT, 'iscotti\s*$');
REG2/I5 = REGEXP_INSTR(PRODUCT, '&REG2STR');
BY PRODUCT
ON TABLE SET PAGE NOLEAD
ON TABLE SET STYLE *
GRID=OFF,$
ENDSTYLE
END

The output follows.

Product Dollar Sales REG1 REG2


------- ------------ ---- ----
Biscotti 5263317 2 1
Capuccino 2381590 0 1
Coffee Grinder 2337567 0 1
Coffee Pot 2449585 0 1
Croissant 7749902 0 1
Espresso 3906243 0 0
Latte 10943622 0 0
Mug 4522521 0 0
Scone 4216114 0 1
Thermos 2385829 0 0

Using Functions 171


Regular Expression Functions

REGEXP_REPLACE: Replacing All Matches to a Pattern in a String


REGEXP_REPLACE returns a string generated by replacing all matches to a regular expression
pattern in the source string with the given replacement string. The replacement string can be a
null string.

Syntax: How to Replace Matches to a Pattern in a String

REGEXP_REPLACE(string, pattern, replacement)

where:
string
Alphanumeric

Is the input string to be searched.


pattern
Alphanumeric

Is a regular expression, enclosed in single quotation marks, constructed using literals and
meta-characters. The following meta-characters are supported

. represents any single character

* represents zero or more occurrences

+ represents one or more occurrences

? represents zero or one occurrence

^ represents beginning of line

$ represents end of line

[] represents any one character in the set listed within the brackets

[^] represents any one character not in the set listed within the brackets

| represents the Or operator

\ is the Escape Special Character

() contains a character sequence


replacement
Alphanumeric

Is the replacement string.

172
5. Simplified Character Functions

Example: Replacing Matches to a Pattern in a String


The following example uses the following Regular Expression symbol.

^, which searches for a specified expression that occurs at the beginning of a string.

In the following request REG1 replaces the string 'North' at the beginning of the REGION field
value with the string 'South', and REG2 replaces the string 'Mid' at the beginning of the
REGION field value with a null string.
TABLE FILE GGSALES
SUM DOLLARS NOPRINT AND COMPUTE
REG1/A25 = REGEXP_REPLACE(REGION, '^North', 'South');
REG2/A25 = REGEXP_REPLACE(REGION, '^Mid', '');
BY REGION
ON TABLE SET PAGE NOLEAD
ON TABLE SET STYLE *
GRID=OFF,$
ENDSTYLE
END

The output is shown in the following image.

The following version of the request runs on z/OS. The regular expression string is created in a
variable using a -SET command. The circumflex meta-character (^) is inserted as CHAR(95).
-SET &REGSTRING1= CHAR(95) || 'North' ;
-SET &REGSTRING2= CHAR(95) || 'Mid' ;
TABLE FILE GGSALES
SUM DOLLARS NOPRINT AND COMPUTE
REG1/A25 = REGEXP_REPLACE(REGION, '&REGSTRING1', 'South');
REG2/A25 = REGEXP_REPLACE(REGION, '&REGSTRING2', '');
BY REGION
ON TABLE SET PAGE NOLEAD
ON TABLE SET STYLE *
GRID=OFF,$
ENDSTYLE
END

Using Functions 173


Regular Expression Functions

The output follows.

Region REG1 REG2


------ ---- ----
Midwest Midwest west
Northeast Southeast Northeast
Southeast Southeast Southeast
West West West

REGEXP_SUBSTR: Returning the First Match to a Pattern in a String


REGEXP_SUBSTR returns a string that contains the first match to a specified regular
expression pattern within a source string. If there is no match within the source string, a null
string is returned.

Syntax: How to Returning the First Match to a Pattern in a String

REGEXP_SUBSTR(string, pattern)

where:
string
Alphanumeric

Is the input string to be searched.


pattern
Alphanumeric

Is a regular expression, enclosed in single quotation marks, constructed using literals and
meta-characters. The following meta-characters are supported

. represents any single character

* represents zero or more occurrences

+ represents one or more occurrences

? represents zero or one occurrence

^ represents beginning of line

$ represents end of line

[] represents any one character in the set listed within the brackets

[^] represents any one character not in the set listed within the brackets

| represents the Or operator

174
5. Simplified Character Functions

\ is the Escape Special Character

() contains a character sequence

Example: Returning the First Match of a Pattern in a String


The following example uses the following Regular Expression symbols.

[A-Z], which matches any uppercase letter.

[a-z], which matches any lowercase letter.

In the following request, REG1 contains the first instance of a string within the REGION field
value that starts with an uppercase letter, followed by any number of lowercase letters,
followed by the characters 'west'. REG2 contains the first instance of a string within the
REGION field value that starts with an uppercase letter, followed by any number of lowercase
letters, followed by the characters 'east'.
TABLE FILE GGSALES
SUM DOLLARS NOPRINT AND COMPUTE
REG1/A25 = REGEXP_SUBSTR(REGION, '[A-Z][a-z]*west');
REG2/A25 = REGEXP_SUBSTR(REGION, '[A-Z][a-z]*east');
BY REGION
ON TABLE SET PAGE NOLEAD
ON TABLE SET STYLE *
GRID=OFF,$
ENDSTYLE
END

The output is shown in the following image.

Using Functions 175


REPEAT: Repeating a String a Given Number of Times

The following version of the request runs on z/OS, where the regular expression is generated
as a variable, using the CHAR function to insert the meta-characters. Note that the asterisk
meta-character (*) needs to be represented as CHAR(92).
-SET &REG1=CHAR(173) || 'A-Z' || CHAR(189) || CHAR(173) || 'a-z'
- || CHAR(189) || CHAR(92) || 'west' ;
-SET &REG2=CHAR(173) || 'A-Z' || CHAR(189) || CHAR(173) || 'a-z'
- || CHAR(189) || '*east';
TABLE FILE GGSALES
SUM DOLLARS NOPRINT AND COMPUTE
REG1/A25 = REGEXP_SUBSTR(REGION,'&REG1');
REG2/A25 = REGEXP_SUBSTR(REGION,'&REG2');
BY REGION
ON TABLE SET PAGE NOLEAD
ON TABLE SET STYLE *
GRID=OFF,$
ENDSTYLE
END

The output follows.


Region REG1 REG2
------ ---- ----
Midwest Midwest
Northeast Northeast
Southeast Southeast
West

REPEAT: Repeating a String a Given Number of Times


Given a source string and an integer number, REPEAT returns a string with the source string
repeated that number of times. The string containing the repeated strings must be large
enough to fit the repetitions or it will contain a truncated value.

Syntax: How to Repeat a Character String a Given Number of Times

REPEAT(source_str, number)

where:
source_str
Alphanumeric

Is the source string to be repeated. If source_str is a field, the entire field, including
blanks, will be repeated.
number
Numeric

Is the number of times to repeat the source string.

176
5. Simplified Character Functions

Example: Repeating a String a Given Number of Times


The following request returns a string with FIRST_NAME repeated three times.
TABLE FILE EMPLOYEE
PRINT FIRST_NAME
COMPUTE REPEAT3/A25 = REPEAT(FIRST_NAME,3);
ON TABLE SET PAGE NOLEAD
ON TABLE PCHOLD FORMAT PDF
ON TABLE SET STYLE *
GRID=OFF,$
ENDSTYLE
END

The PDF output is shown in the following image.

REPLACE: Replacing a String


REPLACE replaces all instances of a search string in an input string with the given replacement
string. The output is always variable length alphanumeric with a length determined by the input
parameters.

Syntax: How to Replace all Instances of a String

REPLACE(input_string , search_string , replacement)

Using Functions 177


REPLACE: Replacing a String

where:
input_string
Alphanumeric or text (An, AnV, TX)

Is the input string.


search_string
Alphanumeric or text (An, AnV, TX)

Is the string to search for within the input string.


replacement
Alphanumeric or text (An, AnV, TX)

Is the replacement string to be substituted for the search string. It can be a null string ('').

Example: Replacing a String


REPLACE replaces the string 'South' in the Country Name with the string 'S.'
SET TRACEUSER = ON
SET TRACEON = STMTRACE//CLIENT
SET TRACESTAMP=OFF
DEFINE FILE WF_RETAIL_LITE
NEWNAME/A20 = REPLACE(COUNTRY_NAME, 'SOUTH', 'S.');
END
TABLE FILE WF_RETAIL_LITE
SUM COUNTRY_NAME
BY NEWNAME AS 'New,Name'
WHERE COUNTRY_NAME LIKE 'S%'
ON TABLE SET PAGE NOLEAD
END

The output is shown in the following image.

178
5. Simplified Character Functions

Example: Replacing All Instances of a String


In the following request, the virtual field DAYNAME1 is the string DAY1 with all instances of the
string 'DAY’ replaced with the string 'day'. The virtual field DAYNAME2 has all instances of the
string 'DAY’ removed.
DEFINE FILE WF_RETAIL
DAY1/A30 = 'SUNDAY MONDAY TUESDAY';
DAYNAME1/A30 = REPLACE(DAY1, 'DAY', 'day' );
DAYNAME2/A30 = REPLACE(DAY1, 'DAY', '' );
END
TABLE FILE WF_RETAIL
PRINT DAY1 OVER
DAYNAME1 OVER
DAYNAME2
WHERE EMPLOYEE_NUMBER EQ 'AH118'
ON TABLE SET PAGE NOPAGE
END

The output is:


DAY1 SUNDAY MONDAY TUESDAY
DAYNAME1 SUNday MONday TUESday
DAYNAME2 SUN MON TUES

RIGHT: Returning Characters From the Right of a Character String


Given a source character string, or an expression that can be converted to varchar (variable-
length alphanumeric), and an integer number, RIGHT returns that number of characters from
the right end of the string.

Syntax: How to Return Characters From the Right of a Character String

RIGHT(chr_exp, int_exp)

where:
chr_exp
Alphanumeric or an expression that can be converted to variable-length alphanumeric.

Is the source character string.


int_exp
Integer

Is the number of characters to be returned.

Using Functions 179


RIGHT: Returning Characters From the Right of a Character String

Example: Returning Characters From the Right of a Character String


The following request computes the length of the last name in the FULLNAME field and returns
that number of characters to LAST.
TABLE FILE WF_RETAIL_EMPLOYEE
PRINT FULLNAME AND
COMPUTE LEN/I5 = ARGLEN(54, GET_TOKEN(FULLNAME, ' ', 2), LEN); NOPRINT
COMPUTE LAST/A20 = RIGHT(FULLNAME, LEN);
WHERE RECORDLIMIT EQ 20
ON TABLE SET PAGE NOLEAD
ON TABLE SET STYLE *
GRID=OFF,$
ENDSTYLE
END

180
5. Simplified Character Functions

The output is shown in the following image.

RPAD: Right-Padding a Character String


RPAD uses a specified character and output length to return a character string padded on the
right with that character.

Syntax: How to Pad a Character String on the Right

RPAD(string, out_length, pad_character)

Using Functions 181


RPAD: Right-Padding a Character String

where:
string
Alphanumeric

Is a string to pad on the right side.


out_length
Integer

Is the length of the output string after padding.


pad_character
Alphanumeric

Is a single character to use for padding.

Example: Right-Padding a String


In the following request against the WF_RETAIL data source, RPAD right-pads the
PRODUCT_CATEGORY column with @ symbols:
DEFINE FILE WF_RETAIL
RPAD1/A25 = RPAD(PRODUCT_CATEGORY,25,'@');
DIG1/A4 = DIGITS(ID_PRODUCT,4);
END
TABLE FILE WF_RETAIL
SUM DIG1 RPAD1
BY PRODUCT_CATEGORY
ON TABLE SET PAGE NOPAGE
ON TABLE SET STYLE *
TYPE=DATA,FONT=COURIER,SIZE=11,COLOR=BLUE,$
END

182
5. Simplified Character Functions

The output is:

Reference: Usage Notes for RPAD


The input string can be data type AnV, VARCHAR, TX, and An.
Output can only be AnV or An.
When working with relational VARCHAR columns, there is no need to trim trailing spaces
from the field if they are not desired. However, with An and AnV fields derived from An
fields, the trailing spaces are part of the data and will be included in the output, with the
padding being placed to the right of these positions. You can use TRIM or TRIMV to remove
these trailing spaces prior to applying the RPAD function.

RTRIM: Removing Blanks From the Right End of a String


The RTRIM function removes all blanks from the right end of a string.

Syntax: How to Remove Blanks From the Right End of a String

RTRIM(string)

where:
string
Alphanumeric

Is the string to trim on the right.

Using Functions 183


SPACE: Returning a String With a Given Number of Spaces

The data type of the returned string is AnV, with the same maximum length as the source
string.

Example: Removing Blanks From the Right End of a String


The following request against the MOVIES data source creates the field DIRSLASH, that
contains a slash at the end of the DIRECTOR field. Then it creates the TRIMDIR field, which
trims the trailing blanks from the DIRECTOR field and places a slash at the end of that field:
TABLE FILE MOVIES
PRINT DIRECTOR NOPRINT AND
COMPUTE
DIRSLASH/A18 = DIRECTOR|'/';
TRIMDIR/A17V = RTRIM(DIRECTOR)|'/';
WHERE DIRECTOR CONTAINS 'BR'
ON TABLE SET PAGE NOPAGE
END

On the output, the slashes show that the trailing blanks in the DIRECTOR field were removed in
the TRIMDIR field:
DIRSLASH TRIMDIR
-------- -------
ABRAHAMS J. / ABRAHAMS J./
BROOKS R. / BROOKS R./
BROOKS J.L. / BROOKS J.L./

SPACE: Returning a String With a Given Number of Spaces


Given an integer count, SPACE returns a string consisting of that number of spaces.

Note: To retain the spaces in HTML report output, the SHOWBLANKS parameter must be set to
ON.

Syntax: How to Return a String With a Given Number of Spaces

SPACE(count)

where:
count
Numeric

Is the number of spaces to return.

184
5. Simplified Character Functions

Example: Returning a String With a Given Number of Spaces


The following request inserts 20 spaces between the DOLLARS and UNITS values converted to
alphanumeric values. The font used is Courier because it is monospaced and shows the 20
blanks without making them proportional.
SET SHOWBLANKS = ON
TABLE FILE GGSALES
SUM DOLLARS NOPRINT UNITS NOPRINT AND
COMPUTE ALPHADOLL/A8 = EDIT(DOLLARS); NOPRINT
COMPUTE ALPHAUNIT/A8 = EDIT(UNITS); NOPRINT
COMPUTE Dollars_And_Units_With_Spaces/A60 = ALPHADOLL | SPACE(20) |
ALPHAUNIT;
BY CATEGORY
ON TABLE SET PAGE NOLEAD
ON TABLE PCHOLD FORMAT PDF
ON TABLE SET STYLE *
GRID=OFF, FONT=COURIER,$
ENDSTYLE
END

The output is shown in the following image.

SPLIT: Extracting an Element From a String


The SPLIT function returns a specific type of element from a string. The output is returned as
variable length alphanumeric.

Syntax: How to Extract an Element From a String

SPLIT(element, string)

where:
element
Can be one of the following keywords:

EMAIL_DOMAIN. Is the domain name portion of an email address in the string.

EMAIL_USERID. Is the user ID portion of an email address in the string.

URL_PROTOCOL. Is the URL protocol for a URL in the string.

Using Functions 185


SUBSTRING: Extracting a Substring From a Source String

URL_HOST. Is the host name of the URL in the string.

URL_PORT. Is the port number of the URL in the string.

URL_PATH. Is the URL path for a URL in the string.

NAME_FIRST. Is the first token (group of characters) in the string. Tokens are delimited
by blanks.

NAME_LAST. Is the last token (group of characters) in the string. Tokens are delimited
by blanks.
string
Alphanumeric

Is the string from which the element will be extracted.

Example: Extracting an Element From a String


The following request defines strings and extracts elements from them.
DEFINE FILE WF_RETAIL_LITE
STRING1/A50 WITH COUNTRY_NAME= 'http://www.informationbuilders.com';
STRING2/A20 = '[email protected]';
STRING3/A20 = 'Louisa May Alcott';
Protocol/A20 = SPLIT(URL_PROTOCOL, STRING1);
Path/A50 = SPLIT(URL_PATH, STRING1);
Domain/A20 = SPLIT(EMAIL_DOMAIN, STRING2);
User/A20 = SPLIT(EMAIL_USERID, STRING2);
First/A10 = SPLIT(NAME_FIRST, STRING3);
Last/A10 = SPLIT(NAME_LAST, STRING3);
END
TABLE FILE WF_RETAIL_LITE
SUM Protocol Path User Domain First Last
ON TABLE SET PAGE NOLEAD
END

The output is shown in the following image.

SUBSTRING: Extracting a Substring From a Source String


The SUBSTRING function extracts a substring from a source string. If the ending position you
specify for the substring is past the end of the source string, the position of the last character
of the source string becomes the ending position of the substring.

186
5. Simplified Character Functions

Syntax: How to Extract a Substring From a Source String

SUBSTRING(string, position, length)

where:
string
Alphanumeric

Is the string from which to extract the substring. It can be a field, a literal in single
quotation marks (‘), or a variable.
position
Positive Integer

Is the starting position of the substring in string.


length
Integer

Is the limit for the length of the substring. The ending position of the substring is
calculated as position + length - 1. If the calculated position beyond the end of the source
string, the position of the last character of string becomes the ending position.

The data type of the returned substring is AnV.

Example: Extracting a Substring From a Source String


In the following request, POSITION determines the position of the first letter I in LAST_NAME
and stores the result in I_IN_NAME. SUBSTRING, then extracts three characters beginning with
the letter I from LAST_NAME and stores the results in I_SUBSTR.
TABLE FILE EMPLOYEE
PRINT
COMPUTE
I_IN_NAME/I2 = POSITION('I', LAST_NAME); AND
COMPUTE
I_SUBSTR/A3 =
SUBSTRING(LAST_NAME, I_IN_NAME, I_IN_NAME+2);
BY LAST_NAME
ON TABLE SET PAGE NOPAGE
END

Using Functions 187


TOKEN: Extracting a Token From a String

The output is:


LAST_NAME I_IN_NAME I_SUBSTR
--------- --------- --------
BANNING 5 ING
BLACKWOOD 0 BL
CROSS 0 CR
GREENSPAN 0 GR
IRVING 1 IRV
JONES 0 JO
MCCOY 0 MC
MCKNIGHT 5 IGH
ROMANS 0 RO
SMITH 3 ITH
3 ITH
STEVENS 0 ST

TOKEN: Extracting a Token From a String


The token function extracts a token (substring) from a string of characters. The tokens are
separated by a delimiter consisting of one or more characters and specified by a token number
reflecting the position of the token in the string.

Syntax: How to Extract a Token From a String

TOKEN(string, delimiter, number)

where:
string
Fixed length alphanumeric

Is the character string from which to extract the token.


delimiter
Fixed length alphanumeric

Is a delimiter consisting of one or more characters.

TOKEN can be optimized if the delimiter consists of a single character.


number
Integer

Is the token number to extract.

188
5. Simplified Character Functions

Example: Extracting a Token From a String


TOKEN extracts the second token from the PRODUCT_SUBCATEG column, where the delimiter
is the letter P:
DEFINE FILE WF_RETAIL_LITE
TOK1/A20 = TOKEN(PRODUCT_SUBCATEG,'P',2);
END
TABLE FILE WF_RETAIL_LITE
SUM TOK1 AS Token
BY PRODUCT_SUBCATEG
ON TABLE SET PAGE NOPAGE
END

Using Functions 189


TRIM_: Removing a Leading Character, Trailing Character, or Both From a String

The output is:

TRIM_: Removing a Leading Character, Trailing Character, or Both From a String


The TRIM_ function removes all occurrences of a single character from either the beginning or
end of a string, or both.

190
5. Simplified Character Functions

Note:

Leading and trailing blanks count as characters. If the character you want to remove is
preceded (for leading) or followed (for trailing) by a blank, the character will not be removed.
Alphanumeric fields that are longer than the number of characters stored within them are
padded with trailing blanks.

The function will be optimized when run against a relational DBMS that supports trimming
the character and location specified.

Syntax: How to Remove a Leading Character, Trailing Character, or Both From a String

TRIM_(where, pattern, string)

where:
where
Keyword

Defines where to trim the source string. Valid values are:

LEADING, which removes leading occurrences.

TRAILING, which removes trailing occurrences.

BOTH, which removes leading and trailing occurrences.


pattern
Alphanumeric

Is a single character, enclosed in single quotation marks ('), whose occurrences are to be
removed from string. For example, the character can be a single blank (‘ ‘).
string
Alphanumeric

Is the string to be trimmed.

The data type of the returned string is AnV.

Using Functions 191


TRIM_: Removing a Leading Character, Trailing Character, or Both From a String

Example: Trimming a Character From a String


In the following request, TRIM_ removes leading occurrences of the character ‘B’ from the
DIRECTOR field:
TABLE FILE MOVIES
PRINT DIRECTOR AND
COMPUTE
TRIMDIR/A17 = TRIM_(LEADING, 'B', DIRECTOR);
WHERE DIRECTOR CONTAINS 'BR'
ON TABLE SET PAGE NOPAGE
END

The output is:


DIRECTOR TRIMDIR
-------- -------
ABRAHAMS J. ABRAHAMS J.
BROOKS R. ROOKS R.
BROOKS J.L. ROOKS J.L.

Example: Trimming With Trailing Blanks


The following request trims a trailing period (.) from the director name. The field DIRECTOR has
format A17, so there are trailing blanks in most of the instances of the field. To create a field
(DIRECTORV) without trailing blanks, SQUEEZ converts the trailing blanks in DIRECTOR to a
single blank, then TRIMV removes the remaining trailing blank and stores it with format A17V,
so the length of the actual characters is known. Then TRIM_ is called against DIRECTOR and
DIRECTORV, creating the fields TRIMDIR (trimmed DIRECTOR) and TRIMDIRV (trimmed
DIRECTORV) :
DEFINE FILE MOVIES
DIRECTORV/A17V = TRIMV('T', SQUEEZ(17, DIRECTOR, 'A17V'), 17, ' ', 1,
DIRECTORV) ;
TRIMDIR/A17 = TRIM_(TRAILING, '.', DIRECTOR);
TRIMDIRV/A17V = TRIM_(TRAILING, '.', DIRECTORV);
END
TABLE FILE MOVIES
PRINT DIRECTOR TRIMDIR DIRECTORV TRIMDIRV
ON TABLE SET PAGE NOPAGE
END

The partial output shows that the trimmed DIRECTOR field still has the trailing periods because
the period is not the last character in the field. In the trimmed DIRECTORV field, the trailing
periods have been removed:

192
5. Simplified Character Functions

DIRECTOR TRIMDIR DIRECTORV TRIMDIRV


-------- ------- --------- --------
SPIELBERG S. SPIELBERG S. SPIELBERG S. SPIELBERG S
KAZAN E. KAZAN E. KAZAN E. KAZAN E
WELLES O. WELLES O. WELLES O. WELLES O
LUMET S. LUMET S. LUMET S. LUMET S

UPPER: Returning a String With All Letters Uppercase


The UPPER function takes a source string and returns a string of the same data type with all
letters translated to uppercase.

Syntax: How to Return a String With All Letters Uppercase

UPPER(string)

where:
string
Alphanumeric

Is the string to convert to uppercase.

The returned string is the same data type and length as the source string.

Example: Converting Letters to Uppercase


In the following request, LCWORD converts LAST_NAME to mixed case. Then UPPER converts
the LAST_NAME_MIXED field to uppercase:
DEFINE FILE EMPLOYEE
LAST_NAME_MIXED/A15=LCWORD(15, LAST_NAME, 'A15');
LAST_NAME_UPPER/A15=UPPER(LAST_NAME_MIXED) ;
END
TABLE FILE EMPLOYEE
PRINT LAST_NAME_UPPER AND FIRST_NAME
BY LAST_NAME_MIXED
WHERE CURR_JOBCODE EQ 'B02' OR 'A17' OR 'B04';
ON TABLE SET PAGE NOPAGE
END

Using Functions 193


UPPER: Returning a String With All Letters Uppercase

The output is:


LAST_NAME_MIXED LAST_NAME_UPPER FIRST_NAME
--------------- --------------- ----------
Banning BANNING JOHN
Blackwood BLACKWOOD ROSEMARIE
Cross CROSS BARBARA
Mccoy MCCOY JOHN
Mcknight MCKNIGHT ROGER
Romans ROMANS ANTHONY

194
Chapter 6 Character Functions
Character functions manipulate alphanumeric fields and character strings.
In this chapter:

Character Function Notes LOCASE: Converting Text to Lowercase

ARGLEN: Measuring the Length of a OVRLAY: Overlaying a Character String


String
PARAG: Dividing Text Into Smaller Lines
ASIS: Distinguishing Between Space and PATTERN: Generating a Pattern From a
Zero String
BITSON: Determining If a Bit Is On or Off POSIT: Finding the Beginning of a
BITVAL: Evaluating a Bit String as an Substring
Integer REVERSE: Reversing the Characters in a
String
BYTVAL: Translating a Character to
Decimal RJUST: Right-Justifying a Character
String
CHKFMT: Checking the Format of a
String SOUNDEX: Comparing Character Strings
Phonetically
CHKNUM: Checking a String for Numeric
Format SPELLNM: Spelling Out a Dollar Amount

CTRAN: Translating One Character to SQUEEZ: Reducing Multiple Spaces to a


Another Single Space

CTRFLD: Centering a Character String STRIP: Removing a Character From a


String
EDIT: Extracting or Adding Characters
STRREP: Replacing Character Strings
GETTOK: Extracting a Substring (Token)
SUBSTR: Extracting a Substring
LCWORD: Converting a String to Mixed-
TRIM: Removing Leading and Trailing
Case
Occurrences
LCWORD2: Converting a String to Mixed-
UPCASE: Converting Text to Uppercase
Case
XMLDECOD: Decoding XML-Encoded
Characters

Using Functions 195


Character Function Notes

LCWORD3: Converting a String to Mixed- XMLENCOD: XML-Encoding Characters


Case

LJUST: Left-Justifying a String

Character Function Notes


In addition to the functions discussed in this topic, there are character functions that are
available only in the Maintain language. For information on these functions, see Maintain-
specific Character Functions on page 279.

For many functions, the output argument can be supplied either as a field name or as a format
enclosed in single quotation marks. However, if a function is called from a Dialogue Manager
command, this argument must always be supplied as a format, and if a function is called from
a Maintain Data procedure, this argument must always be supplied as a field name. For
detailed information about calling a function and supplying arguments, see Accessing and
Calling a Function on page 59.

ARGLEN: Measuring the Length of a String


Available Languages: reporting, Maintain

The ARGLEN function measures the length of a character string within a field, excluding trailing
spaces. The field format in a Master File specifies the length of a field, including trailing
spaces.

In Dialogue Manager, you can measure the length of a supplied character string using
the .LENGTH suffix.

Syntax: How to Measure the Length of a Character String

ARGLEN(length, source_string, output)

where:
length
Integer

Is the length of the field containing the character string, or a field that contains the length.
source_string
Alphanumeric

Is the name of the field containing the character string.

196
6. Character Functions

output
Integer

Is the field that contains the result, or the format of the output value enclosed in single
quotation marks.

Example: Measuring the Length of a Character String


ARGLEN determines the length of the character string in LAST_NAME and stores the result in
NAME_LEN:
TABLE FILE EMPLOYEE
PRINT LAST_NAME AND COMPUTE
NAME_LEN/I3 = ARGLEN(15, LAST_NAME, NAME_LEN);
WHERE DEPARTMENT EQ 'MIS';
END

The output is:

LAST_NAME NAME_LEN
--------- --------
SMITH 5
JONES 5
MCCOY 5
BLACKWOOD 9
GREENSPAN 9
CROSS 5

ASIS: Distinguishing Between Space and Zero


Available Languages: reporting

The ASIS function distinguishes between a space and a zero in Dialogue Manager. It
differentiates between a numeric string, a constant or variable defined as a numeric string
(number within single quotation marks), and a field defined simply as numeric. ASIS forces a
variable to be evaluated as it is entered rather than be converted to a number. It is used in
Dialogue Manager equality expressions only.

Using Functions 197


ASIS: Distinguishing Between Space and Zero

Syntax: How to Distinguish Between a Space and a Zero

ASIS(argument)

where:
argument
Alphanumeric

Is the value to be evaluated. Supply the actual value, the name of a field that contains the
value, or an expression that returns the value. An expression can call a function.

If you specify an alphanumeric literal, enclose it in single quotation marks. If you specify
an expression, use parentheses, as needed, to ensure the correct order of evaluation.

Example: Distinguishing Between a Space and a Zero


The first request does not use ASIS. No difference is detected between variables defined as a
space and 0.
-SET &VAR1 = ' ';
-SET &VAR2 = 0;
-IF &VAR2 EQ &VAR1 GOTO ONE;
-TYPE VAR1 &VAR1 EQ VAR2 &VAR2 NOT TRUE
-QUIT
-ONE
-TYPE VAR1 &VAR1 EQ VAR2 &VAR2 TRUE

The output is:


VAR1 EQ VAR2 0 TRUE
The next request uses ASIS to distinguish between the two variables.
-SET &VAR1 = ' ';
-SET &VAR2 = 0;
-IF &VAR2 EQ ASIS(&VAR1) GOTO ONE;
-TYPE VAR1 &VAR1 EQ VAR2 &VAR2 NOT TRUE
-QUIT
-ONE
-TYPE VAR1 &VAR1 EQ VAR2 &VAR2 TRUE

The output is:


VAR1 EQ VAR2 0 NOT TRUE

198
6. Character Functions

Reference: Usage Notes for ASIS


In general, Dialogue Manager variables are treated as alphanumeric values. However, a
Dialogue Manager variable with the value of '.' may be treated as an alphanumeric value ('.') or
a number (0) depending on the context used.

If the Dialogue Manager variable '.' is used in a mathematical expression, its value will be
treated as a number. For example, in the following request, &DMVAR1 is used in an
arithmetic expression and is evaluated as zero (0).

-SET &DMVAR1='.';
-SET &DMVAR2=10 + &DMVAR1;
-TYPE DMVAR2 = &DMVAR2

The output is;

DMVAR2 = 10

If the Dialogue Manager variable value '.' is used in an IF test and is compared to the
values ' ', '0', or '.', the result will be TRUE even if ASIS is used, as shown in the following
example. The following IF tests all evaluate to TRUE.

-SET &DMVAR1='.';
-SET &DMVAR2=IF &DMVAR1 EQ ' ' THEN 'TRUE' ELSE 'FALSE';
-SET &DMVAR3=IF &DMVAR1 EQ '.' THEN 'TRUE' ELSE 'FALSE';
-SET &DMVAR4=IF &DMVAR1 EQ '0' THEN 'TRUE' ELSE 'FALSE';

If the Dialogue Manager variable is used with ASIS, the result of the ASIS function will be
always be considered alphanumeric and will distinguish between the space (‘ ‘), zero (‘0’),
or period (‘.’), as in the following example. The following IF tests all evaluate to TRUE.

-SET &DMVAR2=IF ASIS('.') EQ '.' THEN 'TRUE' ELSE 'FALSE';


-SET &DMVAR3=IF ASIS(' ') EQ ' ' THEN 'TRUE' ELSE 'FALSE';
-SET &DMVAR4=IF ASIS('0') EQ '0' THEN 'TRUE' ELSE 'FALSE';

Comparing ASIS('0') to ' ' and ASIS(' ') to '0' always evaluates to FALSE.

BITSON: Determining If a Bit Is On or Off


Available Languages: reporting, Maintain

The BITSON function evaluates an individual bit within a character string to determine whether
it is on or off. If the bit is on, BITSON returns a value of 1. If the bit is off, it returns a value of
0. This function is useful in interpreting multi-punch data, where each punch conveys an item
of information.

Using Functions 199


BITSON: Determining If a Bit Is On or Off

Syntax: How to Determine If a Bit Is On or Off

BITSON(bitnumber, source_string, output)

where:
bitnumber
Integer

Is the number of the bit to be evaluated, counted from the left-most bit in the character
string.
source_string
Alphanumeric

Is the character string to be evaluated, enclosed in single quotation marks, or a field or


variable that contains the character string. The character string is in multiple eight-bit
blocks.
output
Integer

Is the name of the field that contains the result, or the format of the output value enclosed
in single quotation marks.

Example: Evaluating a Bit in a Field


BITSON evaluates the 24th bit of LAST_NAME and stores the result in BIT_24:
TABLE FILE EMPLOYEE
PRINT LAST_NAME AND COMPUTE
BIT_24/I1 = BITSON(24, LAST_NAME, BIT_24);
WHERE DEPARTMENT EQ 'MIS';
END

The output is:


LAST_NAME BIT_24
--------- ------
SMITH 1
JONES 1
MCCOY 1
BLACKWOOD 1
GREENSPAN 1
CROSS 0

200
6. Character Functions

BITVAL: Evaluating a Bit String as an Integer


Available Languages: reporting, Maintain

The BITVAL function evaluates a string of bits within a character string. The bit string can be
any group of bits within the character string and can cross byte and word boundaries. The
function evaluates the subset of bits in the string as an integer value.

If the number of bits is:

Less than 1, the returned value is 0.

Between 1 and 31 (the recommended range), the returned value is a zero or positive
number representing the bits specified, extended with high-order zeroes for a total of 32
bits.

Exactly 32, the returned value is the positive, zero, or the complement value of negative
two, of the specified 32 bits.

Greater than 32 (33 or more), the returned value is the positive, zero, or the complement
value of negative two, of the rightmost 32 bits specified.

Syntax: How to Evaluate a Bit String

BITVAL(source_string, startbit, number, output)

where:
source_string
Alphanumeric

Is the character string to be evaluated, enclosed in single quotation marks, or a field or


variable that contains the character string.
startbit
Integer

Is the number of the first bit in the bit string, counting from the left-most bit in the
character string. If this argument is less than or equal to 0, the function returns a value of
zero.
number
Integer

Is the number of bits in the subset of bits. If this argument is less than or equal to 0, the
function returns a value of zero.

Using Functions 201


BYTVAL: Translating a Character to Decimal

output
Integer

Is the name of the field that contains the binary integer equivalent, or the format of the
output value enclosed in single quotation marks.

Example: Evaluating a Bit String


BITVAL evaluates the bits 12 through 20 of LAST_NAME and stores the result in a field with
the format I5:
TABLE FILE EMPLOYEE
PRINT LAST_NAME AND COMPUTE
STRING_VAL/I5 = BITVAL(LAST_NAME, 12, 9, 'I5');
WHERE DEPARTMENT EQ 'MIS';
END

The output is:


LAST_NAME STRING_VAL
--------- ----------
SMITH 332
JONES 365
MCCOY 60
BLACKWOOD 316
GREENSPAN 412
CROSS 413

BYTVAL: Translating a Character to Decimal


Available Languages: reporting, Maintain

The BYTVAL function translates a character to the ASCII, EBCDIC, or Unicode decimal value
that represents it, depending on the operating system.

Syntax: How to Translate a Character

BYTVAL(character, output)

where:
character
Alphanumeric

Is the character to be translated. You can specify a field or variable that contains the
character, or the character itself enclosed in single quotation marks. If you supply more
than one character, the function evaluates the first.

202
6. Character Functions

output
Integer

Is the name of the field that contains the corresponding decimal value, or the format of the
output value enclosed in single quotation marks.

Example: Translating the First Character of a Field


BYTVAL translates the first character of LAST_NAME into its ASCII or EBCDIC decimal value
and stores the result in LAST_INIT_CODE. Since the input string has more than one character,
BYTVAL evaluates the first one.
TABLE FILE EMPLOYEE
PRINT LAST_NAME AND
COMPUTE LAST_INIT_CODE/I3 = BYTVAL(LAST_NAME, 'I3');
WHERE DEPARTMENT EQ 'MIS';
END

The output on an ASCII platform is:


LAST_NAME LAST_INIT_CODE
--------- --------------
SMITH 83
JONES 74
MCCOY 77
BLACKWOOD 66
GREENSPAN 71
CROSS 67
The output on an EBCDIC platform is:
LAST_NAME LAST_INIT_CODE
--------- --------------
SMITH 226
JONES 209
MCCOY 212
BLACKWOOD 194
GREENSPAN 199
CROSS 195

Using Functions 203


CHKFMT: Checking the Format of a String

Example: Returning the EBCDIC Value With Dialogue Manager


This Dialogue Manager request prompts for a character, then returns the corresponding
number. The following reflects the results on the Windows platform.
-SET &CODE = BYTVAL(&CHAR, 'I3');
-HTMLFORM BEGIN
<HTML>
<BODY>
THE EQUIVALENT VALUE IS &CODE
</BODY>
</HTML>
-HTMLFORM END

Assume the value entered for &CHAR is an exclamation point (!). The output is:
THE EQUIVALENT VALUE IS 33

CHKFMT: Checking the Format of a String


Available Languages: reporting, Maintain

The CHKFMT function checks a character string for incorrect characters or character types. It
compares each character string to a second string, called a mask, by comparing each
character in the first string to the corresponding character in the mask. If all characters in the
character string match the characters or character types in the mask, CHKFMT returns the
value 0. Otherwise, CHKFMT returns a value equal to the position of the first character in the
character string not matching the mask.

If the mask is shorter than the character string, the function checks only the portion of the
character string corresponding to the mask. For example, if you are using a four-character
mask to test a nine-character string, only the first four characters in the string are checked; the
rest are returned as a no match with CHKFMT giving the first non-matching position as the
result.

Syntax: How to Check the Format of a Character String

CHKFMT(numchar, source_string, 'mask', output)

where:
numchar
Integer

Is the number of characters being compared to the mask.

204
6. Character Functions

string
Alphanumeric

Is the character string to be checked enclosed in single quotation marks, or a field or


variable that contains the character string.
'mask'

Alphanumeric

Is the mask, which contains the comparison characters enclosed in single quotation
marks.

Some characters in the mask are generic and represent character types. If a character in
the string is compared to one of these characters and is the same type, it matches.
Generic characters are:

A is any letter between A and Z (uppercase or lowercase).

9 is any digit between 0–9.

X is any letter between A–Z or any digit between 0-9.

$ is any character.

Any other character in the mask represents only that character. For example, if the third
character in the mask is B, the third character in the string must be B to match.
output
Integer

Is the name of the field that contains the result, or the format of the output value enclosed
in single quotation marks.

Using Functions 205


CHKNUM: Checking a String for Numeric Format

Example: Checking the Format of a Field


CHKFMT examines EMP_ID for nine numeric characters starting with 11 and stores the result
in CHK_ID:
TABLE FILE EMPLOYEE
PRINT EMP_ID AND LAST_NAME AND
COMPUTE CHK_ID/I3 = CHKFMT(9, EMP_ID, '119999999', CHK_ID);
WHERE DEPARTMENT EQ 'PRODUCTION';
END

The output is:


EMP_ID LAST_NAME CHK_ID
------ --------- ------
071382660 STEVENS 1
119265415 SMITH 0
119329144 BANNING 0
123764317 IRVING 2
126724188 ROMANS 2
451123478 MCKNIGHT 1

CHKNUM: Checking a String for Numeric Format


The CHKNUM function checks a character string for numeric format. If the string contains a
valid numeric format, CHKNUM returns the value 1. If the string contains characters that are
not valid in a number, CHKNUM returns zero (0).

Syntax: How to Check the Format of a Character String

CHKNUM(numchar, source_string, output)

where:
numchar
Integer

Is the number of characters in the string.


string
Alphanumeric

Is the character string to be checked enclosed in single quotation marks, or a field or


variable that contains the character string.
output
Numeric

Is the name of the field that contains the result, or the format of the output value enclosed
in single quotation marks.

206
6. Character Functions

Example: Checking a String for Numeric Format


CHKNUM examines the strings STR1, STR2, and STR3 for numeric format.
DEFINE FILE WF_RETAIL_LITE
STR1/A8 = '12345E01';
STR2/A8 = 'ABCDEFG';
STR3/A8 = '1234.567';
CHK1/I1= CHKNUM(8,STR1,CHK1);
CHK2/I1= CHKNUM(8,STR2,CHK2);
CHK3/I1= CHKNUM(8,STR3,CHK3);
END
TABLE FILE WF_RETAIL_LITE
PRINT STR1 IN 20 CHK1 STR2 CHK2 STR3 CHK3
BY PRODUCT_CATEGORY
WHERE PRODUCT_CATEGORY EQ 'Video Production'
ON TABLE SET PAGE NOPAGE
ON TABLE PCHOLD FORMAT WP
END

The output is:

Product

Category STR1 CHK1 STR2 CHK2 STR3


CHK3
-------- ---- ---- ---- ---- ----
----
Video Production 12345E01 1 ABCDEFG 0 1234.567 1
12345E01 1 ABCDEFG 0 1234.567 1
12345E01 1 ABCDEFG 0 1234.567 1
12345E01 1 ABCDEFG 0 1234.567 1
12345E01 1 ABCDEFG 0 1234.567 1
12345E01 1 ABCDEFG 0 1234.567 1

CTRAN: Translating One Character to Another


Available Languages: reporting, Maintain

The CTRAN function translates a character within a character string to another character based
on its decimal value. This function is especially useful for changing replacement characters to
unavailable characters, or to characters that are difficult to input or unavailable on your
keyboard. It can also be used for inputting characters that are difficult to enter when
responding to a Dialogue Manager -PROMPT command, such as a comma or apostrophe. It
eliminates the need to enclose entries in single quotation marks (').

To use CTRAN, you must know the decimal equivalent of the characters in internal machine
representation. Note that the coding chart for conversion is platform dependent, hence your
platform and configuration option determines whether ASCII, EBCDIC, or Unicode coding is
used. Printable EBCDIC or ASCII characters and their decimal equivalents are listed in .

Using Functions 207


CTRAN: Translating One Character to Another

In Unicode configurations, this function uses values in the range:

0 to 255 for 1-byte characters.

256 to 65535 for 2-byte characters.

65536 to 16777215 for 3-byte characters.

16777216 to 4294967295 for 4-byte characters (primarily for EBCDIC).

Syntax: How to Translate One Character to Another

CTRAN(length, source_string, decimal, decvalue, output)

where:
length
Integer

Is the number of characters in the source string, or a field that contains the length.
source_string
Alphanumeric

Is the character string to be translated enclosed in single quotation marks ('), or the field
or variable that contains the character string.
decimal
Integer

Is the ASCII or EBCDIC decimal value of the character to be translated.


decvalue
Integer

Is the ASCII or EBCDIC decimal value of the character to be used as a substitute for
decimal.
output
Alphanumeric

Is the name of the field that contains the result, or the format of the output value enclosed
in single quotation marks.

208
6. Character Functions

Example: Translating Spaces to Underscores on an ASCII Platform


CTRAN translates the spaces in ADDRESS_LN3 (ASCII decimal value 32) to underscores (ASCII
decimal value 95), and stores the result in ALT_ADDR:
TABLE FILE EMPLOYEE
PRINT ADDRESS_LN3 AND COMPUTE
ALT_ADDR/A20 = CTRAN(20, ADDRESS_LN3, 32, 95, ALT_ADDR);
BY EMP_ID
WHERE TYPE EQ 'HSM';
END

The output is:


EMP_ID ADDRESS_LN3 ALT_ADDR
------ ----------- --------
117593129 RUTHERFORD NJ 07073 RUTHERFORD_NJ_07073_
119265415 NEW YORK NY 10039 NEW_YORK_NY_10039___
119329144 FREEPORT NY 11520 FREEPORT_NY_11520___
123764317 NEW YORK NY 10001 NEW_YORK_NY_10001___
126724188 FREEPORT NY 11520 FREEPORT_NY_11520___
451123478 ROSELAND NJ 07068 ROSELAND_NJ_07068___
543729165 JERSEY CITY NJ 07300 JERSEY_CITY_NJ_07300
818692173 FLUSHING NY 11354 FLUSHING_NY_11354

Example: Translating Spaces to Underscores on an EBCDIC Platform


CTRAN translates the spaces in ADDRESS_LN3 (EBCDIC decimal value 64) to underscores
(EBCDIC decimal value 109) and stores the result in ALT_ADDR:
TABLE FILE EMPLOYEE
PRINT ADDRESS_LN3 AND COMPUTE
ALT_ADDR/A20 = CTRAN(20, ADDRESS_LN3, 64, 109, ALT_ADDR);
BY EMP_ID
WHERE TYPE EQ 'HSM'
END

The output is:


EMP_ID ADDRESS_LN3 ALT_ADDR
------ ----------- --------
117593129 RUTHERFORD NJ 07073 RUTHERFORD_NJ_07073_
119265415 NEW YORK NY 10039 NEW_YORK_NY_10039___
119329144 FREEPORT NY 11520 FREEPORT_NY_11520___
123764317 NEW YORK NY 10001 NEW_YORK_NY_10001___
126724188 FREEPORT NY 11520 FREEPORT_NY_11520___
451123478 ROSELAND NJ 07068 ROSELAND_NJ_07068___
543729165 JERSEY CITY NJ 07300 JERSEY_CITY_NJ_07300
818692173 FLUSHING NY 11354 FLUSHING_NY_11354___

Using Functions 209


CTRFLD: Centering a Character String

CTRFLD: Centering a Character String


Available Languages: reporting, Maintain

The CTRFLD function centers a character string within a field. The number of leading spaces is
equal to or one less than the number of trailing spaces.

CTRFLD is useful for centering the contents of a field and its report column, or a heading that
consists only of an embedded field. HEADING CENTER centers each field value including
trailing spaces. To center the field value without the trailing spaces, first center the value
within the field using CTRFLD.

Limit: Using CTRFLD in a styled report (StyleSheets feature) generally negates the effect of
CTRFLD unless the item is also styled as a centered element. Also, if you are using CTRFLD on
a platform for which the default font is proportional, either use a non-proportional font, or issue
SET STYLE=OFF before running the request.

Syntax: How to Center a Character String

CTRFLD(source_string, length, output)

where:
source_string
Alphanumeric

Is the character string enclosed in single quotation marks, or a field or variable that
contains the character string.
length
Integer

Is the number of characters in source_string and output, or a field that contains the length.
This argument must be greater than 0. A length less than 0 can cause unpredictable
results.
output
Alphanumeric

Is the name of the field that contains the result, or the format of the output value enclosed
in single quotation marks.

210
6. Character Functions

Example: Centering a Field


CTRFLD centers LAST_NAME and stores the result in CENTER_NAME:
SET STYLE=OFF
TABLE FILE EMPLOYEE
PRINT LAST_NAME AND COMPUTE
CENTER_NAME/A12 = CTRFLD(LAST_NAME, 12, 'A12');
WHERE DEPARTMENT EQ 'MIS'
END

The output is:


LAST_NAME CENTER_NAME
--------- -----------
SMITH SMITH
JONES JONES
MCCOY MCCOY
BLACKWOOD BLACKWOOD
GREENSPAN GREENSPAN
CROSS CROSS

EDIT: Extracting or Adding Characters


Available Languages: reporting

The EDIT function extracts characters from the source string and adds characters to the output
string, according to the mask. It can extract a substring from different parts of the source
string. It can also insert characters from the source string into an output string. For example, it
can extract the first two characters and the last two characters of a string to form a single
output string.

EDIT compares the characters in a mask to the characters in a source string. When it
encounters a nine (9) in the mask, EDIT copies the corresponding character from the source
field to the output string. When it encounters a dollar sign ($) in the mask, EDIT ignores the
corresponding character in the source string. When it encounters any other character in the
mask, EDIT copies that character to the corresponding position in the output string. This
process ends when the mask is exhausted.
Note:

EDIT does not require an output argument because the result is alphanumeric and its size
is determined from the mask value.

EDIT can also convert the format of a field.

Using Functions 211


EDIT: Extracting or Adding Characters

Syntax: How to Extract or Add Characters

EDIT(source_string, 'mask');

where:
source_string
Alphanumeric

Is a character string from which to pick characters. Each 9 in the mask represents one
digit, so the size of source_string must be at least as large as the number of 9's in the
mask.
mask
Alphanumeric

Is a string of mask characters enclosed in single quotation marks or a field containing the
character string enclosed in single quotation marks. The length of the mask, excluding
characters other than 9 and $, determines the length of the output field.

Example: Extracting and Adding Characters


EDIT extracts the first initial from the FIRST_NAME field and stores the result in FIRST_INIT.
EDIT also adds dashes to the EMP_ID field and stores the result in EMPIDEDIT. The mask
used to extract the first initial is stored in the virtual field named MASK1:
DEFINE FILE EMPLOYEE
MASK1/A10 = '9$$$$$$$$$'
END
TABLE FILE EMPLOYEE
PRINT LAST_NAME AND COMPUTE
FIRST_INIT/A1 = EDIT(FIRST_NAME, MASK1);
EMPIDEDIT/A11 = EDIT(EMP_ID, '999-99-9999');
WHERE DEPARTMENT EQ 'MIS';
END

The output is:


LAST_NAME FIRST_INIT EMPIDEDIT
--------- ---------- ---------
SMITH M 112-84-7612
JONES D 117-59-3129
MCCOY J 219-98-4371
BLACKWOOD R 326-17-9357
GREENSPAN M 543-72-9165
CROSS B 818-69-2173

212
6. Character Functions

GETTOK: Extracting a Substring (Token)


Available Languages: reporting, Maintain

The GETTOK function divides a character string into substrings, called tokens. The data must
have a specific character, called a delimiter, that occurs in the string and separates the string
into tokens. GETTOK returns the token specified by the token_number argument. GETTOK
ignores leading and trailing blanks in the source character string.

For example, suppose you want to extract the fourth word from a sentence. In this case, use
the space character for a delimiter and the number 4 for token_number. GETTOK divides the
sentence into words using this delimiter, then extracts the fourth word. If the string is not
divided by the delimiter, use the PARAG function for this purpose.

Syntax: How to Extract a Substring (Token)

GETTOK(source_string, inlen, token_number, 'delim', outlen, output)

where:
source_string
Alphanumeric

Is the source string from which to extract the token.


inlen
Integer

Is the number of characters in source_string. If this argument is less than or equal to 0,


the function returns spaces.
token_number
Integer

Is the number of the token to extract. If this argument is positive, the tokens are counted
from left to right. If this argument is negative, the tokens are counted from right to left. For
example, -2 extracts the second token from the right. If this argument is 0, the function
returns spaces. Leading and trailing null tokens are ignored.

Using Functions 213


GETTOK: Extracting a Substring (Token)

'delim'
Alphanumeric

Is the delimiter in the source string enclosed in single quotation marks. If you specify more
than one character, only the first character is used.

Note: In Dialogue Manager, to prevent the conversion of a delimiter space character (' ') to
a double precision zero, include a non-numeric character after the space (for example, '%').
GETTOK uses only the first character (the space) as a delimiter, while the extra character
(%) prevents conversion to double precision.
outlen
Integer

Is the size of the token extracted. If this argument is less than or equal to 0, the function
returns spaces. If the token is longer than this argument, it is truncated; if it is shorter, it
is padded with trailing spaces.
output
Alphanumeric

Is the name of the field that contains the token, or the format of the output value enclosed
in single quotation marks. The delimiter is not included in the token.

Note that the delimiter is not included in the extracted token.

214
6. Character Functions

Example: Extracting a Token


GETTOK extracts the last token from ADDRESS_LN3 and stores the result in LAST_TOKEN.

The delimiter is a space:

TABLE FILE EMPLOYEE


PRINT ADDRESS_LN3 AND COMPUTE
LAST_TOKEN/A10 = GETTOK(ADDRESS_LN3, 20, -1, ' ', 10, LAST_TOKEN);
AS 'LAST TOKEN,(ZIP CODE)'
WHERE TYPE EQ 'HSM';
END

The output is:


LAST TOKEN
ADDRESS_LN3 (ZIP CODE)
----------- ----------
RUTHERFORD NJ 07073 07073
NEW YORK NY 10039 10039
FREEPORT NY 11520 11520
NEW YORK NY 10001 10001
FREEPORT NY 11520 11520
ROSELAND NJ 07068 07068
JERSEY CITY NJ 07300 07300
FLUSHING NY 11354 11354

LCWORD: Converting a String to Mixed-Case


Available Languages: reporting, Maintain

The LCWORD function converts the letters in a character string to mixed-case. It converts every
alphanumeric character to lowercase except the first letter of each new word and the first letter
after a single or double quotation mark, which it converts to uppercase. For example,
O'CONNOR is converted to O'Connor and JACK'S to Jack'S.

LCWORD skips numeric and special characters in the source string and continues to convert
the following alphabetic characters. The result of LCWORD is a string in which the initial
uppercase characters of all words are followed by lowercase characters.

Syntax: How to Convert a Character String to Mixed-Case

LCWORD(length, source_string, output)

where:
length
Integer

Is the number of characters in source_string and output.

Using Functions 215


LCWORD2: Converting a String to Mixed-Case

string
Alphanumeric

Is the character string to be converted enclosed in single quotation marks, or a field or


variable containing the character string.
output
Alphanumeric

Is the name of the field that contains the result, or the format of the output value enclosed
in single quotation marks. The length must be greater than or equal to length.

Example: Converting a Character String to Mixed-Case


LCWORD converts the LAST_NAME field to mixed-case and stores the result in MIXED_CASE.
TABLE FILE EMPLOYEE
PRINT LAST_NAME AND COMPUTE
MIXED_CASE/A15 = LCWORD(15, LAST_NAME, MIXED_CASE);
WHERE DEPARTMENT EQ 'PRODUCTION'
END

The output is:


LAST_NAME MIXED_CASE
--------- ----------
STEVENS Stevens
SMITH Smith
BANNING Banning
IRVING Irving
ROMANS Romans
MCKNIGHT Mcknight

LCWORD2: Converting a String to Mixed-Case


Available Languages: reporting, Maintain

The LCWORD2 function converts the letters in a character string to mixed-case by converting
the first letter of each word to uppercase and converting every other letter to lowercase. In
addition, a double quotation mark or a space indicates that the next letter should be converted
to uppercase.

For example, "SMITH" would be changed to "Smith" and "JACK S" would be changed to
"Jack S".

216
6. Character Functions

Syntax: How to Convert a Character String to Mixed-Case

LCWORD2(length, string, output)

where:
length
Integer

Is the length, in characters, of the character string or field to be converted, or a field that
contains the length.
string
Alphanumeric

Is the character string to be converted, or a temporary field that contains the string.
output
Alphanumeric

Is the name of the field that contains the result, or the format of the output value enclosed
in single quotation marks. The length must be greater than or equal to length.

Example: Converting a Character String to Mixed-Case


LCWORD2 converts the string O'CONNOR’s to mixed-case:

DEFINE FILE EMPLOYEE


MYVAL1/A10='O'CONNOR'S';
LC2/A10 = LCWORD2(10, MYVAL1, 'A10');
END
TABLE FILE EMPLOYEE
SUM LAST_NAME NOPRINT MYVAL1 LC2
END

The output is:


MYVAL1 LC2
------ ---
O'CONNOR'S O'Connor's

LCWORD3: Converting a String to Mixed-Case


The LCWORD3 function converts the letters in a character string to mixed-case by converting
the first letter of each word to uppercase and converting every other letter to lowercase. In
addition, a single quotation mark indicates that the next letter should be converted to
uppercase, as long as it is neither followed by a blank nor the last character in the input string.

For example, 'SMITH' would be changed to 'Smith' and JACK'S would be changed to Jack's.

Using Functions 217


LJUST: Left-Justifying a String

Syntax: How to Convert a Character String to Mixed-Case Using LCWORD3

LCWORD3(length, string, output)

where:
length
Integer

Is the length, in characters, of the character string or field to be converted, or a field that
contains the length.
string
Alphanumeric

Is the character string to be converted, or a field that contains the string.


output
Alphanumeric

Is the name of the field that contains the result, or the format of the output value enclosed
in single quotation marks. The length must be greater than or equal to length.

Example: Converting a Character String to Mixed-Case Using LCWORD3


LCWORD3 converts the strings O'CONNOR’s and o’connor’s to mixed-case:
DEFINE FILE EMPLOYEE
MYVAL1/A10='O'CONNOR'S';
MYVAL2/A10='o'connor's';
LC1/A10 = LCWORD3(10, MYVAL1, 'A10');
LC2/A10 = LCWORD3(10, MYVAL2, 'A10');
END
TABLE FILE EMPLOYEE
SUM LAST_NAME NOPRINT MYVAL1 LC1 MYVAL2 LC2
END

On the output, the letter C after the first single quotation mark is in uppercase because it is
not followed by a blank and is not the final letter in the input string. The letter s after the
second single quotation mark (') is in lowercase because it is the last character in the input
string:
MYVAL1 LC1 MYVAL2 LC2
------ --- ------ ---
O'CONNOR'S O'Connor's o'connor's O'Connor's

LJUST: Left-Justifying a String


Available Languages: reporting

LJUST left-justifies a character string within a field. All leading spaces become trailing spaces.

218
6. Character Functions

LJUST will not have any visible effect in a report that uses StyleSheets (SET STYLE=ON) unless
you center the item.

There is a version of the LJUST function that is available only in the Maintain language. For
information on this function, see LJUST: Left-Justifying a Character String (Maintain) on page
283.

Syntax: How to Left-Justify a Character String

LJUST(length, source_string, output)

where:
length
Integer

Is the number of characters in source_string and output, or a field that contains the length.
source_string
Alphanumeric

Is the character string to be justified, or a field or variable that contains the string.
output
Alphanumeric

Is the name of the field that contains the result, or the format of the output value enclosed
in single quotation marks.

Example: Left-Justifying a String


The following request creates the XNAME field in which the last names are not left-justified.
Then, LJUST left-justifies the XNAME field and stores the result in YNAME.
SET STYLE=OFF
DEFINE FILE EMPLOYEE
XNAME/A25=IF LAST_NAME EQ 'BLACKWOOD' THEN ' '|LAST_NAME ELSE
''|LAST_NAME;
YNAME/A25=LJUST(15, XNAME, 'A25');
END

TABLE FILE EMPLOYEE


PRINT LAST_NAME XNAME YNAME
END

Using Functions 219


LOCASE: Converting Text to Lowercase

The output is:


LAST_NAME XNAME YNAME
--------- ----- -----
STEVENS STEVENS STEVENS
SMITH SMITH SMITH
JONES JONES JONES
SMITH SMITH SMITH
BANNING BANNING BANNING
IRVING IRVING IRVING
ROMANS ROMANS ROMANS
MCCOY MCCOY MCCOY
BLACKWOOD BLACKWOOD BLACKWOOD
MCKNIGHT MCKNIGHT MCKNIGHT
GREENSPAN GREENSPAN GREENSPAN
CROSS CROSS CROSS

LOCASE: Converting Text to Lowercase


Available Languages: reporting, Maintain

The LOCASE function converts alphanumeric text to lowercase.

Syntax: How to Convert Text to Lowercase

LOCASE(length, source_string, output)

where:
length
Integer

Is the number of characters in source_string and output, or a field that contains the length.
The length must be greater than 0 and the same for both arguments; otherwise, an error
occurs.
source_string
Alphanumeric

Is the character string to convert in single quotation marks, or a field or variable that
contains the string.
output
Alphanumeric

Is the name of the field in which to store the result, or the format of the output value
enclosed in single quotation marks. The field name can be the same as source_string.

220
6. Character Functions

Example: Converting a String to Lowercase


LOCASE converts the LAST_NAME field to lowercase and stores the result in LOWER_NAME:
TABLE FILE EMPLOYEE
PRINT LAST_NAME AND COMPUTE
LOWER_NAME/A15 = LOCASE(15, LAST_NAME, LOWER_NAME);
WHERE DEPARTMENT EQ 'MIS';
END

The output is:


LAST_NAME LOWER_NAME
--------- ----------
SMITH smith
JONES jones
MCCOY mccoy
BLACKWOOD blackwood
GREENSPAN greenspan
CROSS cross

OVRLAY: Overlaying a Character String


Available Languages: reporting

The OVRLAY function overlays a base character string with a substring. The function enables
you to edit part of an alphanumeric field without replacing the entire field.

There is a version of the OVRLAY function that is available only in the Maintain language. For
information on this function, see OVRLAY: Overlaying a Character String (Maintain) on page
289.

Syntax: How to Overlay a Character String

OVRLAY(source_string, length, substring, sublen, position, output)

where:
source_string
Alphanumeric

Is the base character string.


stringlen
Integer

Is the number of characters in source_string and output, or a field that contains the length.
If this argument is less than or equal to 0, unpredictable results occur.

Using Functions 221


OVRLAY: Overlaying a Character String

substring
Alphanumeric

Is the substring that will overlay source_string.


sublen
Integer

Is the number of characters in substring, or a field that contains the length. If this
argument is less than or equal to 0, the function returns spaces.
position
Integer

Is the position in source_string at which the overlay begins. If this argument is less than or
equal to 0, the function returns spaces. If this argument is larger than stringlen, the
function returns the source string.
output
Alphanumeric

Is the name of the field that contains the result, or the format of the output value enclosed
in single quotation marks. If the overlaid string is longer than the output field, the string is
truncated to fit the field.

Note that if the overlaid string is longer than the output field, the string is truncated to fit
the field.

Example: Replacing Characters in a Character String


OVRLAY replaces the last three characters of EMP_ID with CURR_JOBCODE to create a new
security identification code and stores the result in NEW_ID:
TABLE FILE EMPLOYEE
PRINT EMP_ID AND CURR_JOBCODE AND COMPUTE
NEW_ID/A9 = OVRLAY(EMP_ID, 9, CURR_JOBCODE, 3, 7, NEW_ID);
BY LAST_NAME BY FIRST_NAME
WHERE DEPARTMENT EQ 'MIS';
END

The output is:


LAST_NAME FIRST_NAME EMP_ID CURR_JOBCODE NEW_ID
--------- ---------- ------ ------------ ------
BLACKWOOD ROSEMARIE 326179357 B04 326179B04
CROSS BARBARA 818692173 A17 818692A17
GREENSPAN MARY 543729165 A07 543729A07
JONES DIANE 117593129 B03 117593B03
MCCOY JOHN 219984371 B02 219984B02
SMITH MARY 112847612 B14 112847B14

222
6. Character Functions

PARAG: Dividing Text Into Smaller Lines


Available Languages: reporting, Maintain

The PARAG function divides a character string into substrings by marking them with a delimiter.
It scans a specific number of characters from the beginning of the string and replaces the last
space in the group scanned with the delimiter, thus creating a first substring, also known as a
token. It then scans the next group of characters in the line, starting from the delimiter, and
replaces its last space with a second delimiter, creating a second token. It repeats this
process until it reaches the end of the line.

Once each token is marked off by the delimiter, you can use the function GETTOK to place the
tokens into different fields. If PARAG does not find any spaces in the group it scans, it
replaces the first character after the group with the delimiter. Therefore, make sure that any
group of characters has at least one space. The number of characters scanned is provided as
the maximum token size.

For example, if you have a field called 'subtitle' which contains a large amount of text
consisting of words separated by spaces, you can cut the field into roughly equal substrings by
specifying a maximum token size to divide the field. If the field is 350 characters long, divide it
into three substrings by specifying a maximum token size of 120 characters. This technique
enables you to print lines of text in paragraph form.

Tip: If you divide the lines evenly, you may create more sub-lines than you intend. For example,
suppose you divide 120-character text lines into two lines of 60 characters maximum, but one
line is divided so that the first sub-line is 50 characters and the second is 55. This leaves
room for a third sub-line of 15 characters. To correct this, insert a space (using weak
concatenation) at the beginning of the extra sub-line, then append this sub-line (using strong
concatenation) to the end of the one before it. Note that the sub-line will be longer than 60
characters.

Syntax: How to Divide Text Into Smaller Lines

PARAG(length, source_string, 'delimiter', max_token_size, output)

where:
length
Integer

Is the number of characters in source_string and output, or a field that contains the length.

Using Functions 223


PARAG: Dividing Text Into Smaller Lines

source_string
Alphanumeric

Is a string to divide into tokens enclosed in single quotation marks, or a field or variable
that contains the text.
delimiter
Alphanumeric

Is the delimiter enclosed in single quotation marks. Choose a character that does not
appear in the text.
max_token_size
Integer

Is the upper limit for the size of each token.


output
Alphanumeric

Is the name of the field that contains the result, or the format of the output value enclosed
in single quotation marks.

Example: Dividing Text Into Smaller Lines


PARAG divides ADDRESS_LN2 into smaller lines of not more than ten characters using a
comma as the delimiter. It then stores the result in PARA_ADDR:
TABLE FILE EMPLOYEE
PRINT ADDRESS_LN2 AND COMPUTE
PARA_ADDR/A20 = PARAG(20, ADDRESS_LN2, ',', 10, PARA_ADDR);
BY LAST_NAME
WHERE TYPE EQ 'HSM';
END

The output is:


LAST_NAME ADDRESS_LN2 PARA_ADDR
--------- ----------- ---------
BANNING APT 4C APT 4C ,
CROSS 147-15 NORTHERN BLD 147-15,NORTHERN,BLD
GREENSPAN 13 LINDEN AVE. 13 LINDEN,AVE.
IRVING 123 E 32 ST. 123 E 32,ST. ,
JONES 235 MURRAY HIL PKWY 235 MURRAY,HIL PKWY
MCKNIGHT 117 HARRISON AVE. 117,HARRISON,AVE.
ROMANS 271 PRESIDENT ST. 271,PRESIDENT,ST.
SMITH 136 E 161 ST. 136 E 161,ST.

224
6. Character Functions

PATTERN: Generating a Pattern From a String


The PATTERN function examines a source string and produces a pattern that indicates the
sequence of numbers, uppercase letters, and lowercase letters in the source string. This
function is useful for examining data to make sure that it follows a standard pattern.

In the output pattern:

Any character from the input that represents a single-byte digit becomes the character 9.

Any character that represents an uppercase letter becomes A, and any character that
represents a lowercase letter becomes a. For European NLS mode (Western Europe,
Central Europe), A and a are extended to apply to accented alphabets.

For Japanese, double-byte characters and Hankaku-katakana become C (uppercase). Note


that double-byte includes Hiragana, Katakana, Kanji, full-width alphabets, full-width
numbers, and full-width symbols. This means that all double-byte letters such as Chinese
and Korean are also represented as C.

Special characters remain unchanged.

An unprintable character becomes the character X.

Syntax: How to Generate a Pattern From an Input String

PATTERN (length, source_string, output)

where:
length
Numeric

Is the length of source_string.


source_string
Alphanumeric

Is the source string enclosed in single quotation marks, or a field containing the source
string.
output
Alphanumeric

Is the name of the field to contain the result or the format of the field enclosed in single
quotation marks.

Using Functions 225


PATTERN: Generating a Pattern From a String

Example: Producing a Pattern From Alphanumeric Data


The following 19 records are stored in a fixed format sequential file (with LRECL 14) named
TESTFILE:

212-736-6250
212 736 4433
123-45-6789
800-969-INFO
10121-2898
10121
2 Penn Plaza
917-339-6380
917-339-4350
(212) 736-6250
(212) 736-4433
212-736-6250
212-736-6250
212-736-6250
(212) 736 5533
(212) 736 5533
(212) 736 5533
10121 Æ
800-969-INFO

The Master File is:

FILENAME=TESTFILE, SUFFIX=FIX ,
SEGMENT=TESTFILE, SEGTYPE=S0, $
FIELDNAME=TESTFLD, USAGE=A14, ACTUAL=A14, $

The following request generates a pattern for each instance of TESTFLD and displays them by
the pattern that was generated. It shows the count of each pattern and its percentage of the
total count. The PRINT command shows which values of TESTFLD generated each pattern.

FILEDEF TESTFILE DISK testfile.ftmDEFINE FILE TESTFILE


PATTERN/A14 = PATTERN (14, TESTFLD, 'A14' ) ;
END
TABLE FILE TESTFILE
SUM CNT.PATTERN AS 'COUNT' PCT.CNT.PATTERN AS 'PERCENT'
BY PATTERN
PRINT TESTFLD
BY PATTERN
ON TABLE COLUMN-TOTAL
END

Note that the next to last line produced a pattern from an input string that contained an
unprintable character, so that character was changed to X. Otherwise, each numeric digit
generated a 9 in the output string, each uppercase letter generated the character ‘A’, and
each lowercase letter generated the character ‘a’. The output is:

226
6. Character Functions

PATTERN COUNT PERCENT TESTFLD


------- ----- ------- -------
(999) 999 9999 3 15.79 (212) 736 5533
(212) 736 5533
(212) 736 5533
(999) 999-9999 2 10.53 (212) 736-6250
(212) 736-4433
9 Aaaa Aaaaa 1 5.26 2 Penn Plaza
999 999 9999 1 5.26 212 736 4433
999-99-9999 1 5.26 123-45-6789
999-999-AAAA 2 10.53 800-969-INFO
800-969-INFO
999-999-9999 6 31.58 212-736-6250
917-339-6380
917-339-4350
212-736-6250
212-736-6250
212-736-6250
99999 1 5.26 10121
99999 X 1 5.26 10121 Æ
99999-9999 1 5.26 10121-2898
TOTAL 19 100.00

POSIT: Finding the Beginning of a Substring


Available Languages: reporting

The POSIT function finds the starting position of a substring within a source string. For
example, the starting position of the substring DUCT in the string PRODUCTION is 4. If the
substring is not in the parent string, the function returns the value 0.

There is a version of the POSIT function that is available only in the Maintain language. For
information on this function, see POSIT: Finding the Beginning of a Substring (Maintain) on page
290.

Syntax: How to Find the Beginning of a Substring

POSIT(source_string, length, substring, sublength, output)

where:
source_string
Alphanumeric

Is the string to parse enclosed in single quotation marks, or a field or variable that
contains the source character string.

Using Functions 227


POSIT: Finding the Beginning of a Substring

length
Integer

Is the number of characters in the source string, or a field that contains the length. If this
argument is less than or equal to 0, the function returns a 0.
substring
Alphanumeric

Is the substring whose position you want to find. This can be the substring enclosed in
single quotation marks, or the field that contains the string.
sublength
Integer

Is the number of characters in substring. If this argument is less than or equal to 0, or if it


is greater than length, the function returns a 0.
output
Integer

Is the name of the field that contains the result, or the format of the output value enclosed
in single quotation marks.

Example: Finding the Position of a Letter


POSIT determines the position of the first capital letter I in LAST_NAME and stores the result
in I_IN_NAME:
TABLE FILE EMPLOYEE
PRINT LAST_NAME AND COMPUTE
I_IN_NAME/I2 = POSIT(LAST_NAME, 15, 'I', 1, 'I2');
WHERE DEPARTMENT EQ 'PRODUCTION'
END

The output is:


LAST_NAME I_IN_NAME
--------- ---------
STEVENS 0
SMITH 3
BANNING 5
IRVING 1
ROMANS 0
MCKNIGHT 5

228
6. Character Functions

REVERSE: Reversing the Characters in a String


The REVERSE function reverses the characters in a string. This reversal includes all trailing
blanks, which then become leading blanks. However, in an HTML report with SET
SHOWBLANKS=OFF (the default value), the leading blanks are not visible.

Syntax: How to Reverse the Characters in a String

REVERSE(length, source_string, output)

where:
length
Integer

Is the number of characters in source_string and output, or a field that contains the length.
source_string
Alphanumeric

Is the character string to reverse enclosed in single quotation marks, or a field that
contains the character string.
output
Alphanumeric

Is the name of the field that contains the result, or the format of the output value enclosed
in single quotation marks.

Example: Reversing the Characters in a String


In the following request against the EMPLOYEE data source, the REVERSE function is used to
reverse the characters in the LAST_NAME field to produce the field named REVERSE_LAST. In
this field, the trailing blanks from LAST_NAME have become leading blanks. The TRIM function
is used to strip the leading blanks from REVERSE_LAST to produce the field named
TRIM_REVERSE:
DEFINE FILE EMPLOYEE
REVERSE_LAST/A15 = REVERSE(15, LAST_NAME, REVERSE_LAST);
TRIM_REVERSE/A15 = TRIM('L', REVERSE_LAST, 15, ' ', 1, 'A15');
END
TABLE FILE EMPLOYEE
PRINT REVERSE_LAST TRIM_REVERSE
BY LAST_NAME
END

Using Functions 229


RJUST: Right-Justifying a Character String

The output is:

LAST_NAME REVERSE_LAST TRIM_REVERSE


--------- ------------ ------------
BANNING GNINNAB GNINNAB
BLACKWOOD DOOWKCALB DOOWKCALB
CROSS SSORC SSORC
GREENSPAN NAPSNEERG NAPSNEERG
IRVING GNIVRI GNIVRI
JONES SENOJ SENOJ
MCCOY YOCCM YOCCM
MCKNIGHT THGINKCM THGINKCM
ROMANS SNAMOR SNAMOR
SMITH HTIMS HTIMS
HTIMS HTIMS
STEVENS SNEVETS SNEVETS

RJUST: Right-Justifying a Character String


Available Languages: reporting

The RJUST function right-justifies a character string. All trailing blacks become leading blanks.
This is useful when you display alphanumeric fields containing numbers.

RJUST does not have any visible effect in a report that uses StyleSheets (SET STYLE=ON)
unless you center the item. Also, if you use RJUST on a platform on which StyleSheets are
turned on by default, issue SET STYLE=OFF before running the request.

There is a version of the RJUST function that is available only in the Maintain language. For
information on this function, see RJUST: Right-Justifying a Character String (Maintain) on page
291.

Syntax: How to Right-Justify a Character String

RJUST(length, source_string, output)

where:
length
Integer
Is the number of characters in source_string and output, or a field that contains the length.
Their lengths must be the same to avoid justification problems.
source_string
Alphanumeric

Is the character string to right justify, or a field or variable that contains the character
string enclosed in single quotation marks.

230
6. Character Functions

output
Alphanumeric

Is the name of the field that contains the result, or the format of the output value enclosed
in single quotation marks.

Example: Right-Justifying a String


RJUST right-justifies the LAST_NAME field and stores the result in RIGHT_NAME:
SET STYLE=OFF
TABLE FILE EMPLOYEE
PRINT LAST_NAME AND COMPUTE
RIGHT_NAME/A15 = RJUST(15, LAST_NAME, RIGHT_NAME);
WHERE DEPARTMENT EQ 'MIS';
END

The output is:


LAST_NAME RIGHT_NAME
--------- ----------
SMITH SMITH
JONES JONES
MCCOY MCCOY
BLACKWOOD BLACKWOOD
GREENSPAN GREENSPAN
CROSS CROSS

SOUNDEX: Comparing Character Strings Phonetically


Available Languages: reporting, Maintain

The SOUNDEX function analyzes a character string phonetically, without regard to spelling. It
converts character strings to four character codes. The first character must be the first
character in the string. The last three characters represent the next three significant sounds in
the source string.

To conduct a phonetic search, do the following:


1. Use SOUNDEX to translate data values from the field you are searching for to the phonetic
codes.
2. Use SOUNDEX to translate your best guess target string to a phonetic code. Remember
that the spelling of your target string need be only approximate. However, the first letter
must be correct.
3. Use WHERE or IF criteria to compare the temporary fields created in Step 1 to the
temporary field created in Step 2.

Using Functions 231


SOUNDEX: Comparing Character Strings Phonetically

Syntax: How to Compare Character Strings Phonetically

SOUNDEX(length, source_string, output)

where:
length
Alphanumeric

Is the number of characters in source_string, or a field that contains the length. It can be a
number enclosed in single quotation marks, or a field containing the number. The number
must be from 01 to 99, expressed with two digits (for example '01'); a number larger than
99 causes the function to return asterisks (*) as output.
source_string
Alphanumeric

Is the string to analyze enclosed in single quotation marks, or a field or variable that
contains the character string.
output
Alphanumeric

Is the name of the field that contains the result, or the format of the output value enclosed
in single quotation marks.

Example: Comparing Character Strings Phonetically


The following request creates three fields:

PHON_NAME contains the phonetic code of employee last names.

PHON_COY contains the phonetic code of your guess, MICOY.

PHON_MATCH contains YES if the phonetic codes match, NO if they do not.

The WHERE criteria selects the last name that matches your best guess.

DEFINE FILE EMPLOYEE


PHON_NAME/A4 = SOUNDEX('15', LAST_NAME, PHON_NAME);
PHON_COY/A4 WITH LAST_NAME = SOUNDEX('15', 'MICOY', PHON_COY);
PHON_MATCH/A3 = IF PHON_NAME IS PHON_COY THEN 'YES' ELSE 'NO';
END

TABLE FILE EMPLOYEE


PRINT LAST_NAME
IF PHON_MATCH IS 'YES'
END

232
6. Character Functions

The output is:


LAST_NAME
---------
MCCOY

SPELLNM: Spelling Out a Dollar Amount


Available Languages: reporting, Maintain

The SPELLNM function spells out an alphanumeric string or numeric value containing two
decimal places as dollars and cents. For example, the value 32.50 is THIRTY TWO DOLLARS
AND FIFTY CENTS.

Syntax: How to Spell Out a Dollar Amount

SPELLNM(outlength, number, output)

where:
outlength
Integer

Is the number of characters in output , or a field that contains the length.

If you know the maximum value of number, use the following table to determine the value
of outlength:

If number is less than... ...outlength should be

$10 37

$100 45

$1,000 59

$10,000 74

$100,000 82

$1,000,000 96

number
Alphanumeric or Numeric (9.2)

Is the number to be spelled out. This value must contain two decimal places.

Using Functions 233


SQUEEZ: Reducing Multiple Spaces to a Single Space

output
Alphanumeric

Is the name of the field that contains the result, or the format of the output value enclosed
in single quotation marks.

Example: Spelling Out a Dollar Amount


SPELLNM spells out the values in CURR_SAL and stores the result in AMT_IN_WORDS:
TABLE FILE EMPLOYEE
PRINT CURR_SAL AND COMPUTE
AMT_IN_WORDS/A82 = SPELLNM(82, CURR_SAL, AMT_IN_WORDS);
WHERE DEPARTMENT EQ 'MIS'
END

The output is:


CURR_SAL AMT_IN_WORDS
-------- ------------
$13,200.00 THIRTEEN THOUSAND TWO HUNDRED DOLLARS AND NO CENTS
$18,480.00 EIGHTEEN THOUSAND FOUR HUNDRED EIGHTY DOLLARS AND NO CENTS
$18,480.00 EIGHTEEN THOUSAND FOUR HUNDRED EIGHTY DOLLARS AND NO CENTS
$21,780.00 TWENTY-ONE THOUSAND SEVEN HUNDRED EIGHTY DOLLARS AND NO CENTS
$9,000.00 NINE THOUSAND DOLLARS AND NO CENTS
$27,062.00 TWENTY-SEVEN THOUSAND SIXTY-TWO DOLLARS AND NO CENTS

SQUEEZ: Reducing Multiple Spaces to a Single Space


Available Languages: reporting, Maintain

The SQUEEZ function reduces multiple contiguous spaces within a character string to a single
space. The resulting character string has the same length as the original string but is padded
on the right with spaces.

Syntax: How to Reduce Multiple Spaces to a Single Space

SQUEEZ(length, source_string, output)

where:
length
Integer

Is the number of characters in source_string and output, or a field that contains the length.
source_string
Alphanumeric

Is the character string to squeeze enclosed in single quotation marks, or the field that
contains the character string.

234
6. Character Functions

output
Alphanumeric

Is the field that contains the result, or the format of the output value enclosed in single
quotation marks.

Example: Reducing Multiple Spaces to a Single Space


SQUEEZ reduces multiple spaces in the NAME field to a single blank and stores the result in a
field with the format A30:

DEFINE FILE EMPLOYEE


NAME/A30 = FIRST_NAME | LAST_NAME;
END
TABLE FILE EMPLOYEE
PRINT NAME AND COMPUTE
SQNAME/A30 = SQUEEZ(30, NAME, 'A30');
WHERE DEPARTMENT EQ 'MIS';
END

The output is:


NAME SQNAME
---- ------
MARY SMITH MARY SMITH
DIANE JONES DIANE JONES
JOHN MCCOY JOHN MCCOY
ROSEMARIE BLACKWOOD ROSEMARIE BLACKWOOD
MARY GREENSPAN MARY GREENSPAN
BARBARA CROSS BARBARA CROSS

STRIP: Removing a Character From a String


Available Languages: reporting, Maintain

The STRIP function removes all occurrences of a specific character from a string. The resulting
character string has the same length as the original string but is padded on the right with
spaces.

Syntax: How to Remove a Character From a String

STRIP(length, source_string, char, output)

where:
length
Integer

Is the number of characters in source_string and output, or a field that contains the
number.

Using Functions 235


STRIP: Removing a Character From a String

source_string
Alphanumeric

Is the string from which the character will be removed, or a field containing the string.
char
Alphanumeric

Is the character to be removed from the string. This can be an alphanumeric literal
enclosed in single quotation marks, or a field that contains the character. If more than one
character is provided, the left-most character will be used as the strip character.

Note: To remove single quotation marks, use two consecutive quotation marks. You must
then enclose this character combination in single quotation marks.
output
Alphanumeric

Is the field that contains the result, or the format of the output value enclosed in single
quotation marks.

Example: Removing Occurrences of a Character From a String


STRIP removes all occurrences of a period (.) from the DIRECTOR field and stores the result in
a field with the format A17:
TABLE FILE MOVIES
PRINT DIRECTOR AND COMPUTE
SDIR/A17 = STRIP(17, DIRECTOR, '.', 'A17');
WHERE CATEGORY EQ 'COMEDY'
END

The output is:


DIRECTORS SDIR
--------- ----
ZEMECKIS R. ZEMECKIS R
ABRAHAMS J. ABRAHAMS J
ALLEN W. ALLEN W
HALLSTROM L. HALLSTROM L
MARSHALL P. MARSHALL P
BROOKS J.L. BROOKS JL

236
6. Character Functions

Example: Removing Single Quotation Marks From a String


STRIP removes all occurrences of a single quotation mark (') from the TITLE field and stores
the result in a field with the format A39:
TABLE FILE MOVIES
PRINT TITLE AND COMPUTE
STITLE/A39 = STRIP(39, TITLE, '''', 'A39');
WHERE TITLE CONTAINS ''''
END

The output is:


TITLE STITLE
----- ------
BABETTE'S FEAST BABETTES FEAST
JANE FONDA'S COMPLETE WORKOUT JANE FONDAS COMPLETE WORKOUT
JANE FONDA'S NEW WORKOUT JANE FONDAS NEW WORKOUT
MICKEY MANTLE'S BASEBALLTIPS MICKEY MANTLES BASEBALL TIPS

Example: Removing Commas From a String (Maintain)


STRIP removes all occurrences of a comma from the TITLE field:
MAINTAIN FILE MOVIES
FOR 10 NEXT MOVIECODE INTO MOVSTK
WHERE TITLE CONTAINS ',';
COMPUTE I/I2=1;
REPEAT MOVSTK.FOCINDEX
TYPE "TITLE IS: <MOVSTK(I).TITLE"
COMPUTE NOCOMMA/A39=STRIP(39,MOVSTK().TITLE, ',',NOCOMMA);
TYPE "NEW TITLE IS: <NOCOMMA";
COMPUTE I=I+1
ENDREPEAT
END

The output is:


TITLE IS: SMURFS, THE
NEW TITLE IS: SMURFS THE

STRREP: Replacing Character Strings


The STRREP replaces all instances of a specified string within a source string. It also supports
replacement by null strings.

Using Functions 237


STRREP: Replacing Character Strings

Syntax: How to Replace Character Strings

STRREP (inlength, instring, searchlength, searchstring, replength,


repstring, outlength, output)

where:
inlength
Numeric

Is the number of characters in the source string.


instring
Alphanumeric

Is the source string.


searchlength
Numeric

Is the number of characters in the (shorter length) string to be replaced.


searchstring
Alphanumeric

Is the character string to be replaced.


replength
Numeric

Is the number of characters in the replacement string. Must be zero (0) or greater.
repstring
Alphanumeric

Is the replacement string (alphanumeric). Ignored if replength is zero (0).


outlength
Numeric

Is the number of characters in the resulting output string. Must be 1 or greater.


output
Alphanumeric

Is the resulting output string after all replacements and padding.

Reference: Usage Note for STRREP Function


The maximum string length is 4095.

238
6. Character Functions

Example: Replacing Commas and Dollar Signs


In the following example, STRREP finds and replaces commas and dollar signs that appear in
the CS_ALPHA field, first replacing commas with null strings to produce CS_NOCOMMAS
(removing the commas) and then replacing the dollar signs ($) with (USD) in the right-most
CURR_SAL column:
TABLE FILE EMPLOYEE
SUM CURR_SAL NOPRINT
COMPUTE CS_ALPHA/A15=FTOA(CURR_SAL,'(D12.2M)',CS_ALPHA);
CS_NOCOMMAS/A14=STRREP(15,CS_ALPHA,1,',',0,'X',14,CS_NOCOMMAS);
CS_USD/A17=STRREP(14,CS_NOCOMMAS,1,'$',4,'USD ',17,CS_USD);
NOPRINT
CS_USD/R AS CURR_SAL
BY LAST_NAME
END

The output is:


LAST_NAME CS_ALPHA CS_NOCOMMAS CURR_SAL
--------- -------- ----------- -----------------
BANNING $29,700.00 $29700.00 USD 29700.00
BLACKWOOD $21,780.00 $21780.00 USD 21780.00
CROSS $27,062.00 $27062.00 USD 27062.00
GREENSPAN $9,000.00 $9000.00 USD 9000.00
IRVING $26,862.00 $26862.00 USD 26862.00
JONES $18,480.00 $18480.00 USD 18480.00
MCCOY $18,480.00 $18480.00 USD 18480.00
MCKNIGHT $16,100.00 $16100.00 USD 16100.00
ROMANS $21,120.00 $21120.00 USD 21120.00
SMITH $22,700.00 $22700.00 USD 22700.00
STEVENS $11,000.00 $11000.00 USD 11000.00

SUBSTR: Extracting a Substring


Available Languages: reporting

The SUBSTR function extracts a substring based on where it begins and its length in the
source string. SUBSTR can vary the position of the substring depending on the values of other
fields.

There is a version of the SUBSTR function that is available only in the Maintain language. For
information on this function, see SUBSTR: Extracting a Substring (Maintain) on page 299.

Using Functions 239


SUBSTR: Extracting a Substring

Syntax: How to Extract a Substring

SUBSTR(length, source_string, start, end, sublength, output)

where:
length
Integer

Is the number of characters in source_string, or a field that contains the length.


source_string
Alphanumeric

Is the string from which to extract a substring enclosed in single quotation marks, or the
field containing the parent string.
start
Integer

Is the starting position of the substring in the source string. If start is less than one or
greater than length, the function returns spaces.
end
Integer

Is the ending position of the substring. If this argument is less than start or greater than
length, the function returns spaces.
sublength
Integer

Is the number of characters in the substring (normally end - start + 1). If sublength is
longer than end - start +1, the substring is padded with trailing spaces. If it is shorter, the
substring is truncated. This value should be the declared length of output. Only sublength
characters will be processed.

output
Alphanumeric

Is the field to which the result is returned, or the format of the output value enclosed in
single quotation marks.

240
6. Character Functions

Example: Extracting a String


POSIT determines the position of the first letter I in LAST_NAME and stores the result in
I_IN_NAME. SUBSTR then extracts three characters beginning with the letter I from
LAST_NAME, and stores the results in I_SUBSTR.
TABLE FILE EMPLOYEE
PRINT
COMPUTE
I_IN_NAME/I2 = POSIT(LAST_NAME, 15, 'I', 1, 'I2'); AND
COMPUTE
I_SUBSTR/A3 =
SUBSTR(15, LAST_NAME, I_IN_NAME, I_IN_NAME+2, 3, I_SUBSTR);
BY LAST_NAME
WHERE DEPARTMENT EQ 'PRODUCTION'
END

The output is:


LAST_NAME I_IN_NAME I_SUBSTR
--------- --------- --------
BANNING 5 ING
IRVING 1 IRV
MCKNIGHT 5 IGH
ROMANS 0
SMITH 3 ITH
STEVENS 0
Since Romans and Stevens have no I in their names, SUBSTR extracts a blank string.

TRIM: Removing Leading and Trailing Occurrences


Available Languages: reporting

The TRIM function removes leading and/or trailing occurrences of a pattern within a character
string.

There is a version of the TRIM function that is available only in the Maintain language. For
information on this function, see TRIM: Removing Trailing Occurrences (Maintain) on page 300.

Using Functions 241


TRIM: Removing Leading and Trailing Occurrences

Syntax: How to Remove Leading and Trailing Occurrences

TRIM(trim_where, source_string, length, pattern, sublength, output)

where:
trim_where
Alphanumeric

Is one of the following, which indicates where to remove the pattern:

'L' removes leading occurrences.

'T' removes trailing occurrences.

'B' removes both leading and trailing occurrences.


source_string
Alphanumeric

Is the string to trim enclosed in single quotation marks, or the field containing the string.
string_length
Integer

Is the number of characters in the source string.


pattern
Alphanumeric

Is the character string pattern to remove enclosed in single quotation marks.


sublength
Integer

Is the number of characters in the pattern.


output
Alphanumeric

Is the field to which the result is returned, or the format of the output value enclosed in
single quotation marks.

242
6. Character Functions

Example: Removing Leading Occurrences


TRIM removes leading occurrences of the characters BR from the DIRECTOR field and stores
the result in a field with the format A17:
TABLE FILE MOVIES
PRINT DIRECTOR AND
COMPUTE
TRIMDIR/A17 = TRIM('L', DIRECTOR, 17, 'BR', 2, 'A17');
WHERE DIRECTOR CONTAINS 'BR'
END

The output is:


DIRECTOR TRIMDIR
-------- -------
ABRAHAMS J. ABRAHAMS J.
BROOKS R. OOKS R.
BROOKS J.L. OOKS J.L.

Example: Removing Trailing Occurrences


TRIM removes trailing occurrences of the characters ER from the TITLE. In order to remove
trailing non-blank characters, trailing spaces must be removed first. The TITLE field has trailing
spaces. Therefore, TRIM does not remove the characters ER when creating field TRIMT. The
SHORT field does not have trailing spaces. Therefore, TRIM removes the trailing ER characters
when creating field TRIMS:
DEFINE FILE MOVIES
SHORT/A19 = SUBSTR(19, TITLE, 1, 19, 19, SHORT);
END
TABLE FILE MOVIES
PRINT TITLE IN 1 AS 'TITLE: '
SHORT IN 40 AS 'SHORT: ' OVER
COMPUTE
TRIMT/A39 = TRIM('T', TITLE, 39, 'ER', 2, 'A39'); IN 1 AS 'TRIMT: '
COMPUTE
TRIMS/A19 = TRIM('T', SHORT, 19, 'ER', 2, 'A19'); IN 40 AS 'TRIMS: '
WHERE TITLE LIKE '%ER'
END

The output is:


TITLE: LEARN TO SKI BETTER SHORT: LEARN TO SKI BETTER
TRIMT: LEARN TO SKI BETTER TRIMS: LEARN TO SKI BETT
TITLE: FANNY AND ALEXANDER SHORT: FANNY AND ALEXANDER
TRIMT: FANNY AND ALEXANDER TRIMS: FANNY AND ALEXAND

Using Functions 243


UPCASE: Converting Text to Uppercase

UPCASE: Converting Text to Uppercase


Available Languages: reporting

The UPCASE function converts a character string to uppercase. It is useful for sorting on a field
that contains both mixed-case and uppercase values. Sorting on a mixed-case field produces
incorrect results because the sorting sequence in EBCDIC always places lowercase letters
before uppercase letters, while the ASCII sorting sequence always places uppercase letters
before lowercase. To obtain correct results, define a new field with all of the values in
uppercase, and sort on that field.

There is a version of the UPCASE function that is available only in the Maintain language. For
information on this function, see UPCASE: Converting Text to Uppercase (Maintain) on page
301.

Syntax: How to Convert Text to Uppercase

UPCASE(length, source_string, output)

where:
length
Integer

Is the number of characters in source_string and output.


input
Alphanumeric

Is the string to convert enclosed in single quotation marks, or the field containing the
character string.
output
Alphanumeric of type AnV or An

Is the field to which the result is returned, or the format of the output value enclosed in
single quotation marks.

Example: Converting a Mixed-Case String to Uppercase


UPCASE converts the LAST_NAME_MIXED field to uppercase:
DEFINE FILE EMPLOYEE
LAST_NAME_MIXED/A15=IF DEPARTMENT EQ 'MIS' THEN LAST_NAME ELSE
LCWORD(15, LAST_NAME, 'A15');
LAST_NAME_UPPER/A15=UPCASE(15, LAST_NAME_MIXED, 'A15') ;
END

244
6. Character Functions

TABLE FILE EMPLOYEE


PRINT LAST_NAME_MIXED AND FIRST_NAME BY LAST_NAME_UPPER
WHERE CURR_JOBCODE EQ 'B02' OR 'A17' OR 'B04';
END

Now, when you execute the request, the names are sorted correctly.

The output is:


LAST_NAME_UPPER LAST_NAME_MIXED FIRST_NAME
--------------- --------------- ----------
BANNING Banning JOHN
BLACKWOOD BLACKWOOD ROSEMARIE
CROSS CROSS BARBARA
MCCOY MCCOY JOHN
MCKNIGHT Mcknight ROGER
ROMANS Romans ANTHONY
If you do not want to see the field with all uppercase values, you can NOPRINT it.

XMLDECOD: Decoding XML-Encoded Characters


The XMLDECOD function decodes the following five standard XML-encoded characters when
they are encountered in a string:

Character Name Character XML-Encoded Representation

ampersand & &amp;

greater than symbol > &gt;

less than symbol < &lt;

double quotation mark " &quot;

single quotation mark (apostrophe) ' &apos;

Syntax: How to Decode XML-Encoded Characters

XMLDECOD(inlength, source_string, outlength, output)

where:
inlength
Integer

Is the length of the field containing the source character string, or a field that contains the
length.

Using Functions 245


XMLDECOD: Decoding XML-Encoded Characters

source_string
Alphanumeric

Is the name of the field containing the source character string or the string enclosed in
single quotation marks (').
outlength
Integer

Is the length of the output character string, or a field that contains the length.
output
Integer

Is the field that contains the result, or the format of the output value enclosed in single
quotation marks.

Example: Decoding XML-Encoded Characters


The file XMLFUNCS is a .csv file that contains some unencoded characters and some XML-
encoded characters. The Master File is:
FILE = XMLFUNCS, SUFFIX=COM,$
SEGNAME = SEG01, SEGTYPE=S1,$
FIELD=INSTRING, ALIAS=CHARS, USAGE=A30,ACTUAL=A30,$

The contents of the file follow:


CHARS: & < > ,$
ENCODED: &amp; &gt; ,$
ENCODED: &quot; &apos; ,$
MIXED: &amp; < &gt; ,$

246
6. Character Functions

XMLDECOD decodes any of the supported XML-encoded characters. Note that some viewers
automatically decode the encoded values for display, so the output is produced in a plain text
format (FORMAT WP):
FILEDEF XMLFUNCS DISK xmlfuncs.csv
DEFINE FILE XMLFUNCS
OUTSTRING/A30=XMLDECOD(30,INSTRING,30,'A30');
END
TABLE FILE XMLFUNCS
PRINT INSTRING OUTSTRING
ON TABLE PCHOLD FORMAT WP
ON TABLE SET PAGE NOPAGE

In the output string, XML-encoded characters have been decoded, and characters that were
not encoded have been left as they were in the input string:
INSTRING OUTSTRING
-------- ---------
CHARS: & < > CHARS: & < >
ENCODED: &amp; &gt; ENCODED: & >
ENCODED: &quot; &apos; ENCODED: " '
MIXED: &amp; < &gt; MIXED: & < >

XMLENCOD: XML-Encoding Characters


The XMLENCOD function encodes the following five standard characters when they are
encountered in a string:

Character Name Character Encoded Representation

ampersand & &amp;

greater than symbol > &gt;

less than symbol < &lt;

double quotation mark " &quot;

single quotation mark (apostrophe) ' &apos;

Using Functions 247


XMLENCOD: XML-Encoding Characters

Syntax: How to XML-Encode Characters

XMLENCOD(inlength, source_string, option, outlength, output)

where:
inlength
Integer

Is the length of the field containing the source character string, or a field that contains the
length.
source_string
Alphanumeric

Is the name of the field containing the source character string or a string enclosed in
single quotation marks (').
option
Integer

Is a code that specifies whether to process a string that already contains XML-encoded
characters. Valid values are:

0, the default, which cancels processing of a string that already contains at least one
XML-encoded character.

1, which processes a string that contains XML-encoded characters.


outlength
Integer

Is the length of the output character string, or a field that contains the length.

Note: The output length, in the worst case, could be six times the length of the input.
output
Integer

Is the field that contains the result, or the format of the output value enclosed in single
quotation marks.

Example: XML-Encoding Characters


The file XMLFUNCS is a .csv file that contains some unencoded characters and some XML-
encoded characters. The Master File is:
FILE = XMLFUNCS, SUFFIX=COM,$
SEGNAME = SEG01, SEGTYPE=S1,$
FIELD=INSTRING, ALIAS=CHARS, USAGE=A30,ACTUAL=A30,$

248
6. Character Functions

The contents of the file follow:


CHARS: & < > ,$
ENCODED: &amp; &gt; ,$
ENCODED: &quot; &apos; ,$
MIXED: &amp; < &gt; ,$

XMLENCOD XML-encodes any of the supported characters to produce OUTSTRING1, and


processes every input string regardless of whether it already contains XML-encoded
characters. For OUTSTRING2, it only encodes those strings that do not contain any XML-
encoded characters. Note that some viewers automatically decode the encoded values for
display, so the output is produced in plain text format (FORMAT WP):
FILEDEF XMLFUNCS DISK xmlfuncs.csv
DEFINE FILE XMLFUNCS
OUTSTRING1/A30=XMLENCOD(30,INSTRING,1,30,'A30');
OUTSTRING2/A30=XMLENCOD(30,INSTRING,0,30,'A30');
END
TABLE FILE XMLFUNCS
PRINT INSTRING OUTSTRING1 IN 24 OUTSTRING2 IN 48
ON TABLE SET PAGE NOPAGE
ON TABLE PCHOLD FORMAT WP
END

In OUTSTRING1, the supported characters have been XML-encoded, and output is produced
even if the input string contains encoded characters. OUTSTRING2 is only produced when no
XML-encoded characters exist in the input string:
INSTRING OUTSTRING1 OUTSTRING2
-------- ---------- ----------
CHARS: & < > CHARS: &amp; &lt; &gt; CHARS: &amp; &lt; &gt;
ENCODED: &amp; &gt; ENCODED: &amp; &gt;
ENCODED: &quot; &apos; ENCODED: &quot; &apos;
MIXED: &amp; < &gt; MIXED: &amp; &lt; &gt;

Using Functions 249


XMLENCOD: XML-Encoding Characters

250
Chapter 7 Variable Length Character Functions
The character format AnV is supported in synonyms for FOCUS, XFOCUS, and relational
data sources. This format is used to represent the VARCHAR (variable length character)
data types supported by relational database management systems.
In this chapter:

Overview

LENV: Returning the Length of an Alphanumeric Field

LOCASV: Creating a Variable Length Lowercase String

POSITV: Finding the Beginning of a Variable Length Substring

SUBSTV: Extracting a Variable Length Substring

TRIMV: Removing Characters From a String

UPCASV: Creating a Variable Length Uppercase String

Overview
For relational data sources, AnV keeps track of the actual length of a VARCHAR column. This
information is especially valuable when the value is used to populate a VARCHAR column in a
different RDBMS. It affects whether trailing blanks are retained in string concatenation and, for
Oracle, string comparisons (the other relational engines ignore trailing blanks in string
comparisons).

In a FOCUS or XFOCUS data source, AnV does not provide true variable length character
support. It is a fixed-length character field with an extra two leading bytes to contain the actual
length of the data stored in the field. This length is stored as a short integer value occupying
two bytes. Because of the two bytes of overhead and the additional processing required to
strip them, AnV format is not recommended for use with non-relational data sources.

AnV fields can be used as arguments to all Information Builders-supplied functions that expect
alphanumeric arguments. An AnV input parameter is treated as an An parameter and is padded
with blanks to its declared size (n). If the last parameter specifies an AnV format, the function
result is converted to type AnV with actual length set equal to its size.

Using Functions 251


LENV: Returning the Length of an Alphanumeric Field

The functions described in this topic are designed to work specifically with the AnV data type
parameters.

Reference: Usage Notes for Using an AnV Field in a Function


The following affect the use of an AnV field in a function:

When using an AnV argument in a function, the input parameter is treated as an An


parameter and is padded with blanks to its declared size (n). If the last parameter specifies
an AnV format, the function result is converted to type AnV with actual length set equal to
its size.

Many functions require both an alphanumeric string and its length as input arguments. If
the supplied string is stored in an AnV field, you still must supply a length argument to
satisfy the requirements of the function. However, the length that will be used in the
function's calculations is the actual length stored as the first two bytes of the AnV field.

In general, any input argument can be a field or a literal. In most cases, numeric input
arguments are supplied to these functions as literals, and there is no reason not to supply
an integer value. However, if the value is not an integer, it is truncated to an integer value
regardless of whether it was supplied as a field or a literal.

LENV: Returning the Length of an Alphanumeric Field


Available Languages: reporting

LENV returns the actual length of an AnV field or the size of an An field.

Syntax: How to Find the Length of an Alphanumeric Field

LENV(source_string, output)

where:
source_string
Alphanumeric of type An or AnV

Is the source string or field. If it is an An format field, the function returns its size, n. For a
character string enclosed in quotation marks or a variable, the size of the string or variable
is returned. For a field of AnV format, its length, taken from the length-in-bytes of the field,
is returned.

252
7. Variable Length Character Functions

output
Integer

Is the field to which the result is returned, or the format of the output value enclosed in
single quotation marks (').

Example: Finding the Length of an AnV Field


TRIMV creates an AnV field named TITLEV by removing trailing blanks from the TITLE value.
Then LENV returns the actual length of each instance of TITLEV to the ALEN field:

TABLE FILE MOVIES


PRINT
COMPUTE TITLEV/A39V = TRIMV('T', TITLE, 39, ' ', 1, TITLEV);
ALEN/I2 = LENV(TITLEV,ALEN);
BY CATEGORY NOPRINT
WHERE CATEGORY EQ 'CHILDREN'
END

The output is:


TITLEV ALEN
------ ----
SMURFS, THE 11
SHAGGY DOG, THE 15
SCOOBY-DOO-A DOG IN THE RUFF 28
ALICE IN WONDERLAND 19
SESAME STREET-BEDTIME STORIES AND SONGS 39
ROMPER ROOM-ASK MISS MOLLY 26
SLEEPING BEAUTY 15
BAMBI 5

LOCASV: Creating a Variable Length Lowercase String


Available Languages: reporting

The LOCASV function converts alphabetic characters in the source string to lowercase and is
similar to LOCASE. LOCASV returns AnV output whose actual length is the lesser of the actual
length of the AnV source string and the value of the input parameter upper_limit.

Syntax: How to Create a Variable Length Lowercase String

LOCASV(upper_limit, source_string, output)

where:
upper_limit
Integer

Is the limit for the length of the source string.

Using Functions 253


POSITV: Finding the Beginning of a Variable Length Substring

source_string
Alphanumeric of type An or AnV

Is the string to be converted to lowercase in single quotation marks, or a field or variable


that contains the string. If it is a field, it can have An or AnV format. If it is a field of type
AnV, its length is taken from the length in bytes stored in the field. If upper_limit is smaller
than the actual length, the source string is truncated to this upper limit.
output
Alphanumeric of type An or AnV

Is the name of the field in which to store the result, or the format of the output value
enclosed in single quotation marks ('). This value can be for a field that is AnV or An
format.

If the output format is AnV, the actual length returned is equal to the smaller of the source
string length and the upper limit.

Example: Creating a Variable Length Lowercase String


In this example, LOCASV converts the LAST_NAME field to lowercase and specifies a length
limit of five characters. The results are stored in the LOWCV_NAME field:
TABLE FILE EMPLOYEE
PRINT LAST_NAME AND COMPUTE
LOWCV_NAME/A15V = LOCASV(5, LAST_NAME, LOWCV_NAME);
WHERE DEPARTMENT EQ 'MIS';
END

The output is:


LAST_NAME LOWCV_NAME
--------- ----------
SMITH smith
JONES jones
MCCOY mccoy
BLACKWOOD black
GREENSPAN green
CROSS cross

POSITV: Finding the Beginning of a Variable Length Substring


Available Languages: reporting

The POSITV function finds the starting position of a substring within a larger string. For
example, the starting position of the substring DUCT in the string PRODUCTION is 4. If the
substring is not in the parent string, the function returns the value 0. This is similar to POSIT;
however, the lengths of its AnV parameters are based on the actual lengths of those
parameters in comparison with two other parameters that specify their sizes.

254
7. Variable Length Character Functions

Syntax: How to Find the Beginning of a Variable Length Substring

POSITV(source_string, upper_limit, substring, sub_limit, output)

where:
source_string
Alphanumeric of type An or AnV

Is the source string that contains the substring whose position you want to find. It can be
the string enclosed in single quotation marks ('), or a field or variable that contains the
source string. If it is a field of AnV format, its length is taken from the length bytes stored
in the field. If upper_limit is smaller than the actual length, the source string is truncated
to this upper limit.
upper_limit
Integer

Is a limit for the length of the source string.


substring
Alphanumeric of type An or AnV

Is the substring whose position you want to find. This can be the substring enclosed in
single quotation marks ('), or the field that contains the string. If it is a field, it can have An
or AnV format. If it is a field of type AnV, its length is taken from the length bytes stored in
the field. If sub_limit is smaller than the actual length, the source string is truncated to this
limit.
sub_limit
Integer

Is the limit for the length of the substring.


output
Integer

Is the name of the field that contains the result, or the format of the output value enclosed
in single quotation marks (').

Using Functions 255


SUBSTV: Extracting a Variable Length Substring

Example: Finding the Starting Position of a Variable Length Pattern


POSITV finds the starting position of a trailing definite or indefinite article in a movie title (such
as ", THE" in SMURFS, THE). First TRIMV removes the trailing blanks from the title so that the
article will be the trailing pattern:
DEFINE FILE MOVIES
TITLEV/A39V = TRIMV('T',TITLE, 39,' ', 1, TITLEV);
PSTART/I4 = POSITV(TITLEV,LENV(TITLEV,'I4'), ',', 1,'I4');
PLEN/I4 = IF PSTART NE 0 THEN LENV(TITLEV,'I4') - PSTART +1
ELSE 0;
END
TABLE FILE MOVIES
PRINT TITLE
PSTART AS 'Pattern,Start' IN 25
PLEN AS 'Pattern,Length'
BY CATEGORY NOPRINT
WHERE PLEN NE 0
END

The output is:


Pattern Pattern
TITLE Start Length
----- ------- -------
SMURFS, THE 7 5
SHAGGY DOG, THE 11 5
MALTESE FALCON, THE 15 5
PHILADELPHIA STORY, THE 19 5
TIN DRUM, THE 9 5
FAMILY, THE 7 5
CHORUS LINE, A 12 3
MORNING AFTER, THE 14 5
BIRDS, THE 6 5
BOY AND HIS DOG, A 16 3

SUBSTV: Extracting a Variable Length Substring


Available Languages: reporting

The SUBSTV function extracts a substring from a string and is similar to SUBSTR. However, the
end position for the string is calculated from the starting position and the substring length.
Therefore, it has fewer parameters than SUBSTR. Also, the actual length of the output field, if
it is an AnV field, is determined based on the substring length.

256
7. Variable Length Character Functions

Syntax: How to Extract a Variable Length Substring

SUBSTV(upper_limit, source_string, start, sub_limit, output)

where:
upper_limit
Integer

Is the limit for the length of the source string.


source_string
Alphanumeric of type An or AnV

Is the character string that contains the substring you want to extract. It can be the string
enclosed in single quotation marks ('), or the field containing the string. If it is a field, it
can have An or AnV format. If it is a field of type AnV, its length is taken from the length
bytes stored in the field. If upper_limit is smaller than the actual length, the source string
is truncated to the upper limit. The final length value determined by this comparison is
referred to as p_length (see the description of the output parameter for related
information).
start
Integer

Is the starting position of the substring in the source string. The starting position can
exceed the source string length, which results in spaces being returned.
sub_limit
Integer

Is the length, in characters, of the substring. Note that the ending position can exceed the
input string length depending on the provided values for start and sub_limit.
output
Alphanumeric of type An or AnV

Is the field to which the result is returned, or the format of the output value enclosed in
single quotation marks ('). This field can be in An or AnV format.

If the format of output is AnV, and assuming end is the ending position of the substring,
the actual length, outlen, is computed as follows from the values for end, start, and
p_length (see the source_string parameter for related information):

If end > p_length or end < start, then outlen = 0. Otherwise, outlen = end - start + 1.

Using Functions 257


TRIMV: Removing Characters From a String

Example: Extracting a Variable Length Substring


The following request extracts a trailing definite or indefinite article from a movie title (such as
", THE" in "SMURFS, THE"). First it trims the trailing blanks so that the article is the trailing
pattern. Next it finds the starting position and length of the pattern. Then SUBSTV extracts the
pattern and TRIMV trims the pattern from the title:

DEFINE FILE MOVIES


TITLEV/A39V = TRIMV('T',TITLE, 39,' ', 1, TITLEV);
PSTART/I4 = POSITV(TITLEV,LENV(TITLEV,'I4'), ',', 1,'I4');
PLEN/I4 = IF PSTART NE 0 THEN LENV(TITLEV,'I4') - PSTART +1
ELSE 0;
PATTERN/A20V= SUBSTV(39, TITLEV, PSTART, PLEN, PATTERN);
NEWTIT/A39V = TRIMV('T',TITLEV,39,PATTERN,LENV(PATTERN,'I4'), NEWTIT);
END
TABLE FILE MOVIES
PRINT TITLE
PSTART AS 'Pattern,Start' IN 25
PLEN AS 'Pattern,Length'
NEWTIT AS 'Trimmed,Title' IN 55
BY CATEGORY NOPRINT
WHERE PLEN NE 0
END

The output is:


Pattern Pattern Trimmed
TITLE Start Length Title
----- ------ ------- -------
SMURFS, THE 7 5 SMURFS
SHAGGY DOG, THE 11 5 SHAGGY DOG
MALTESE FALCON, THE 15 5 MALTESE FALCON
PHILADELPHIA STORY, THE 19 5 PHILADELPHIA STORY
TIN DRUM, THE 9 5 TIN DRUM
FAMILY, THE 7 5 FAMILY
CHORUS LINE, A 12 3 CHORUS LINE
MORNING AFTER, THE 14 5 MORNING AFTER
BIRDS, THE 6 5 BIRDS
BOY AND HIS DOG, A 16 3 BOY AND HIS DOG

TRIMV: Removing Characters From a String


Available Languages: reporting

The TRIMV function removes leading and/or trailing occurrences of a pattern within a character
string. TRIMV is similar to TRIM. However, TRIMV allows the source string and the pattern to
be removed to have AnV format.

TRIMV is useful for converting an An field to an AnV field (with the length in bytes containing
the actual length of the data up to the last non-blank character).

258
7. Variable Length Character Functions

Syntax: How to Remove Characters From a String

TRIMV(trim_where, source_string, upper_limit, pattern, pattern_limit,


output)

where:
trim_where
Alphanumeric

Is one of the following, which indicates where to remove the pattern:

'L' removes leading occurrences.

'T' removes trailing occurrences.

'B' removes both leading and trailing occurrences.


source_string
Alphanumeric of type An or AnV

Is the source string to be trimmed. It can be the string enclosed in single quotation marks
('), or the field containing the string. If it is a field, it can have An or AnV format. If it is a
field of type AnV, its length is taken from the length in bytes stored in the field. If
upper_limit is smaller than the actual length, the source string is truncated to this upper
limit.
upper_limit
Integer

Is the upper limit for the length of the source string.


pattern
Alphanumeric of type An or AnV

Is the pattern to remove from the string, enclosed in single quotation marks ('). If it is a
field, it can have An or AnV format. If it is a field of type AnV, its length is taken from the
length in bytes stored in the field. If pattern_limit is smaller than the actual length, the
pattern is truncated to this limit.
plength_limit
Integer

Is the limit for the length of the pattern.


output
Alphanumeric of type An or AnV

Is the field to which the result is returned, or the format of the output value enclosed in
single quotation marks ('). The field can be in AnV or An format.

Using Functions 259


UPCASV: Creating a Variable Length Uppercase String

If the output format is AnV, the length is set to the number of characters left after
trimming.

Example: Creating an AnV Field by Removing Trailing Blanks


TRIMV creates an AnV field named TITLEV by removing trailing blanks from the TITLE value:
TABLE FILE MOVIES
PRINT DIRECTOR
COMPUTE TITLEV/A39V = TRIMV('T', TITLE, 39, ' ', 1, TITLEV);
BY CATEGORY
END

Here are the first 10 lines of the output:


CATEGORY DIRECTOR TITLEV
-------- -------- ------
ACTION SPIELBERG S. JAWS
VERHOVEN P. ROBOCOP
VERHOVEN P. TOTAL RECALL
SCOTT T. TOP GUN
MCDONALD P. RAMBO III
CHILDREN SMURFS, THE
BARTON C. SHAGGY DOG, THE
SCOOBY-DOO-A DOG IN THE RUFF
GEROMINI ALICE IN WONDERLAND
SESAME STREET-BEDTIME STORIES AND SONGS

UPCASV: Creating a Variable Length Uppercase String


Available Languages: reporting

UPCASV converts alphabetic characters to uppercase, and is similar to UPCASE. However,


UPCASV can return AnV output whose actual length is the lesser of the actual length of the
AnV source string and an input parameter that specifies the upper limit.

Syntax: How to Create a Variable Length Uppercase String

UPCASV(upper_limit, source_string, output)

where:
upper_limit
Integer

Is the limit for the length of the source string. It can be a positive constant or a field whose
integer portion represents the upper limit.

260
7. Variable Length Character Functions

source_string
Alphanumeric of type An or AnV

is the string to convert to uppercase. It can be the character string enclosed in single
quotation marks ('), or the field containing the character string. If it is a field, it can have
An or AnV format. If it is a field of type AnV, its length is taken from the length in bytes
stored in the field. If upper_limit is smaller than the actual length, the source string is
truncated to the upper limit.
output
Alphanumeric of type An or AnV

Is the field to which the result is returned, or the format of the output value enclosed in
single quotation marks ('). This can be a field with AnV or An format.

If the output format is AnV, the length returned is equal to the smaller of the source string
length and upper_limit.

Example: Creating a Variable Length Uppercase String


Suppose you are sorting on a field that contains both uppercase and mixed-case values. The
following request defines a field called LAST_NAME_MIXED that contains both uppercase and
mixed-case values:
DEFINE FILE EMPLOYEE
LAST_NAME_MIXED/A15=IF DEPARTMENT EQ 'MIS' THEN LAST_NAME ELSE
LCWORD(15, LAST_NAME, 'A15');
LAST_NAME_UPCASV/A15V=UPCASV(5, LAST_NAME_MIXED, 'A15') ;
END

Suppose you execute a request that sorts by this field:


TABLE FILE EMPLOYEE
PRINT LAST_NAME_MIXED AND FIRST_NAME BY LAST_NAME_UPCASV
WHERE CURR_JOBCODE EQ 'B02' OR 'A17' OR 'B04';
END

The output is:


LAST_NAME_UPCASV LAST_NAME_MIXED FIRST_NAME
---------------- --------------- ----------
BANNI Banning JOHN
BLACK BLACKWOOD ROSEMARIE
CROSS CROSS BARBARA
MCCOY MCCOY JOHN
MCKNI Mcknight ROGER
ROMAN Romans ANTHONY

Using Functions 261


UPCASV: Creating a Variable Length Uppercase String

262
Chapter 8 Character Functions for DBCS Code
Pages
The functions in this topic manipulate strings of DBCS and SBCS characters when your
configuration uses a DBCS code page.
In this chapter:

DCTRAN: Translating A Single-Byte or Double-Byte Character to Another

DEDIT: Extracting or Adding Characters

DSTRIP: Removing a Single-Byte or Double-Byte Character From a String

DSUBSTR: Extracting a Substring

JPTRANS: Converting Japanese Specific Characters

KKFCUT: Truncating a String

SFTDEL: Deleting the Shift Code From DBCS Data

SFTINS: Inserting the Shift Code Into DBCS Data

DCTRAN: Translating A Single-Byte or Double-Byte Character to Another


The DCTRAN function translates a single-byte or double-byte character within a character string
to another character based on its decimal value. To use DCTRAN, you need to know the
decimal equivalent of the characters in internal machine representation.

The DCTRAN function can translate single-byte to double-byte characters and double-byte to
single-byte characters, as well as single-byte to single-byte characters and double-byte to
double-byte characters.

Syntax: How to Translate a Single-Byte or Double-Byte Character to Another

DCTRAN(length, source_string, indecimal, outdecimal, output)

where:
length
Double

Using Functions 263


DEDIT: Extracting or Adding Characters

Is the number of characters in source_string.


source_string
Alphanumeric

Is the character string to be translated.


indecimal
Double

Is the ASCII or EBCDIC decimal value of the character to be translated.


outdecimal
Double

Is the ASCII or EBCDIC decimal value of the character to be used as a substitute for
indecimal.
output
Alphanumeric

Is the name of the field that contains the result, or the format of the output value enclosed
in single quotation marks (').

Example: Using DCTRAN to Translate Double-Byte Characters


In the following:

DEDIT: Extracting or Adding Characters


If your configuration uses a DBCS code page, you can use the DEDIT function to extract
characters from or add characters to a string.

DEDIT works by comparing the characters in a mask to the characters in a source field. When
it encounters a nine (9) in the mask, DEDIT copies the corresponding character from the
source field to the new field. When it encounters a dollar sign ($) in the mask, DEDIT ignores
the corresponding character in the source field. When it encounters any other character in the
mask, DEDIT copies that character to the corresponding position in the new field.

264
8. Character Functions for DBCS Code Pages

Syntax: How to Extract or Add DBCS or SBCS Characters

DEDIT(inlength, source_string, mask_length, mask, output)

where:
inlength
Integer

Is the number of bytes in source_string. The string can have a mixture of DBCS and SBCS
characters. Therefore, the number of bytes represents the maximum number of characters
possible in the source string.
source_string
Alphanumeric

Is the string to edit enclosed in single quotation marks ('), or the field containing the
string.
mask_length
Integer

Is the number of characters in mask.


mask
Alphanumeric

Is the string of mask characters.

Each nine (9) in the mask causes the corresponding character from the source field to be
copied to the new field.

Each dollar sign ($) in the mask causes the corresponding character in the source field to
be ignored.

Any other character in the mask is copied to the new field.


output
Alphanumeric

Is the field to which the result is returned, or the format of the output value enclosed in
single quotation marks (').

Using Functions 265


DSTRIP: Removing a Single-Byte or Double-Byte Character From a String

Example: Adding and Extracting DBCS Characters


The following example copies alternate characters from the source string to the new field,
starting with the first character in the source string, and then adds several new characters at
the end of the extracted string:

The following example copies alternate characters from the source string to the new field,
starting with the second character in the source string, and then adds several new characters
at the end of the extracted string:

DSTRIP: Removing a Single-Byte or Double-Byte Character From a String


The DSTRIP function removes all occurrences of a specific single-byte or double-byte character
from a string. The resulting character string has the same length as the original string, but is
padded on the right with spaces.

Syntax: How to Remove a Single-Byte or Double-Byte Character From a String

DSTRIP(length, source_string, char, output)

where:
length
Double

Is the number of characters in source_string and outfield.


source_string
Alphanumeric

Is the string from which the character will be removed.

266
8. Character Functions for DBCS Code Pages

char
Alphanumeric

Is the character to be removed from the string. If more than one character is provided, the
left-most character will be used as the strip character.

Note: To remove single quotation marks, use two consecutive quotation marks. You must
then enclose this character combination in single quotation marks.
output
Alphanumeric

Is the name of the field that contains the result, or the format of the output value enclosed
in single quotation marks (').

Example: Removing a Double-Byte Character From a String


In the following:

DSUBSTR: Extracting a Substring


If your configuration uses a DBCS code page, you can use the DSUBSTR function to extract a
substring based on its length and position in the source string.

Syntax: How to Extract a Substring

DSUBSTR(inlength, source_string, start, end, sublength, output)

where:
inlength
Integer

Is the length of the source string in bytes, or a field that contains the length. The string can
have a mixture of DBCS and SBCS characters. Therefore, the number of bytes represents
the maximum number of characters possible in the source string.
source_string
Alphanumeric

Is the string from which the substring will be extracted enclosed in single quotation marks
('), or the field containing the parent string.

Using Functions 267


JPTRANS: Converting Japanese Specific Characters

start
Integer

Is the starting position (in number of characters) of the substring in the source string. If
this argument is less than one or greater than end, the function returns spaces.
end
Integer

Is the ending position (in number of characters) of the substring. If this argument is less
than start or greater than inlength, the function returns spaces.
sublength
Integer

Is the length of the substring, in characters (normally end - start + 1). If sublength is longer
than end - start +1, the substring is padded with trailing spaces. If it is shorter, the
substring is truncated. This value should be the declared length of output. Only sublength
characters will be processed.
output
Alphanumeric

Is the field to which the result is returned, or the format of the output value enclosed in
single quotation marks (').

Example: Extracting a Substring


The following example extracts the 3-character substring in positions 4 through 6 from a 15-
byte string of characters:

JPTRANS: Converting Japanese Specific Characters


The JPTRANS function converts Japanese specific characters.

268
8. Character Functions for DBCS Code Pages

Syntax: How to Convert Japanese Specific Characters

JPTRANS ('type_of_conversion', length, source_string, 'output_format')

where:
type_of_conversion
Is one of the following options indicating the type of conversion you want to apply to
Japanese specific characters. The following table shows the single component input types:

Conversion Type Description

'UPCASE' Converts Zenkaku (Fullwidth) alphabets to Zenkaku uppercase.

'LOCASE' Converts Zenkaku alphabets to Zenkaku lowercase.

'HNZNALPHA' Converts alphanumerics from Hankaku (Halfwidth) to Zenkaku.

'HNZNSIGN' Converts ASCII symbols from Hankaku to Zenkaku.

'HNZNKANA' Converts Katakana from Hankaku to Zenkaku.

'HNZNSPACE' Converts space (blank) from Hankaku to Zenkaku.

'ZNHNALPHA' Converts alphanumerics from Zenkaku to Hankaku.

'ZNHNSIGN' Converts ASCII symbols from Zenkaku to Hankaku.

'ZNHNKANA' Converts Katakana from Zenkaku to Hankaku.

'ZNHNSPACE' Converts space from Zenkaku to Hankaku.

'HIRAKATA' Converts Hiragana to Zenkaku Katakana.

'KATAHIRA' Converts Zenkaku Katakana to Hiragana.

'930TO939' Converts codepage from 930 to 939.

'939TO930' Converts codepage from 939 to 930.

Using Functions 269


JPTRANS: Converting Japanese Specific Characters

length
Integer

Is the number of characters in the source_string.


source_string
Alphanumeric

Is the string to convert.


output_format
Alphanumeric

Is the name of the field that contains the output, or the format enclosed in single
quotation marks (').

Example: Using the JPTRANS Function


JPTRANS('UPCASE', 20, Alpha_DBCS_Field, 'A20')

JPTRANS('LOCASE', 20, Alpha_DBCS_Field, 'A20')

JPTRANS('HNZNALPHA', 20, Alpha_SBCS_Field, 'A20')

JPTRANS('HNZNSIGN', 20, Symbol_SBCS_Field, 'A20')

JPTRANS('HNZNKANA', 20, Hankaku_Katakana_Field, 'A20')

JPTRANS('HNZNSPACE', 20, Hankaku_Katakana_Field, 'A20')

JPTRANS('ZNHNALPHA', 20, Alpha_DBCS_Field, 'A20')

270
8. Character Functions for DBCS Code Pages

JPTRANS('ZNHNSIGN', 20, Symbol_DBCS_Field, 'A20')

JPTRANS('ZNHNKANA', 20, Zenkaku_Katakana_Field, 'A20')

JPTRANS('ZNHNSPACE', 20, Zenkaku_Katakana_Field, 'A20')

JPTRANS('HIRAKATA', 20, Hiragana_Field, 'A20')

JPTRANS('KATAHIRA', 20, Zenkaku_Katakana_Field, 'A20')

In the following, codepoints 0x62 0x63 0x64 are converted to 0x81 0x82 0x83, respectively:
JPTRANS('930TO939', 20, CP930_Field, 'A20')

In the following, codepoints 0x59 0x62 0x63 are converted to 0x81 0x82 0x83, respectively:
JPTRANS('939TO930', 20, CP939_Field, 'A20')

Reference: Usage Notes for the JPTRANS Function


HNZNSIGN and ZNHNSIGN focus on the conversion of symbols.

Many symbols have a one-to-one relation between Japanese Fullwidth characters and ASCII
symbols, whereas some characters have one-to-many relations. For example, the Japanese
punctuation character (U+3001) and Fullwidth comma , (U+FF0C) will be converted to the
same comma , (U+002C). The following EXTRA rule for those special cases is shown below:

HNZNSIGN:

Double Quote " (U+0022) -> Fullwidth Right Double Quote ” (U+201D)

Single Quote ' (U+0027) -> Fullwidth Right Single Quote ’ (U+2019)

Comma , (U+002C) -> Fullwidth Ideographic Comma (U+3001)

Using Functions 271


JPTRANS: Converting Japanese Specific Characters

Full Stop . (U+002E) -> Fullwidth Ideographic Full Stop ? (U+3002)

Backslash \ (U+005C) -> Fullwidth Backslash \ (U+FF3C)

Halfwidth Left Corner Bracket (U+FF62) -> Fullwidth Left Corner Bracket (U+300C)

Halfwidth Right Corner Bracket (U+FF63) -> Fullwidth Right Corner Bracket (U+300D)

Halfwidth Katakana Middle Dot ? (U+FF65) -> Fullwidth Middle Dot · (U+30FB)

ZNHNSIGN:

Fullwidth Right Double Quote ” (U+201D) -> Double Quote " (U+0022)

Fullwidth Left Double Quote “ (U+201C) -> Double Quote " (U+0022)

Fullwidth Quotation " (U+FF02) -> Double Quote " (U+0022)

Fullwidth Right Single Quote ’ (U+2019) -> Single Quote ' (U+0027)

Fullwidth Left Single Quote ‘ (U+2018) -> Single Quote ' (U+0027)

Fullwidth Single Quote ' (U+FF07) -> Single Quote ' (U+0027)

Fullwidth Ideographic Comma (U+3001) -> Comma , (U+002C)

Fullwidth Comma , (U+FF0C) -> Comma , (U+002C)

Fullwidth Ideographic Full Stop ? (U+3002) -> Full Stop . (U+002E)

Fullwidth Full Stop . (U+FF0E) -> Full Stop . (U+002E)

Fullwidth Yen Sign ¥ (U+FFE5) -> Yen Sign ¥ (U+00A5)

Fullwidth Backslash \ (U+FF3C) -> Backslash \ (U+005C)

Fullwidth Left Corner Bracket (U+300C) -> Halfwidth Left Corner Bracket (U+FF62)

Fullwidth Right Corner Bracket (U+300D) -> Halfwidth Right Corner Bracket (U+FF63)

Fullwidth Middle Dot · (U+30FB) -> Halfwidth Katakana Middle Dot · (U+FF65)

HNZNKANA and ZNHNKANA focus on the conversion of Katakana

They convert not only letters, but also punctuation symbols on the following list:

Fullwidth Ideographic Comma (U+3001) <-> Halfwidth Ideographic Comma (U+FF64)

272
8. Character Functions for DBCS Code Pages

Fullwidth Ideographic Full Stop (U+3002) <-> Halfwidth Ideographic Full Stop (U+FF61)

Fullwidth Left Corner Bracket (U+300C) <-> Halfwidth Left Corner Bracket (U+FF62)

Fullwidth Right Corner Bracket (U+300D) <-> Halfwidth Right Corner Bracket (U+FF63)

Fullwidth Middle Dot · (U+30FB) <-> Halfwidth Katakana Middle Dot · (U+FF65)

Fullwidth Prolonged Sound (U+30FC) <-> Halfwidth Prolonged Sound (U+FF70)

JPTRANS can be nested for multiple conversions.

For example, text data may contain fullwidth numbers and fullwidth symbols. In some
situations, they should be cleaned up for ASCII numbers and symbols.

JPTRANS('ZNHNALPHA', 20, JPTRANS('ZNHNSIGN', 20, Symbol_DBCS_Field,


'A20'), 'A20')

HNZNSPACE and ZNHNSPACE focus on the conversion of a space (blank character).

Currently only conversion between U+0020 and U+3000 is supported.

KKFCUT: Truncating a String


If your configuration uses a DBCS code page, you can use the KKFCUT function to truncate a
string.

Syntax: How to Truncate a String

KKFCUT(length, source_string, output)

where:
length
Integer

Is the length of the source string in bytes, or a field that contains the length. The string can
have a mixture of DBCS and SBCS characters. Therefore, the number of bytes represents
the maximum number of characters possible in the source string.
source_string
Alphanumeric

Is the string that will be truncated enclosed in single quotation marks ('), or the field
containing the string.

Using Functions 273


SFTDEL: Deleting the Shift Code From DBCS Data

output
Alphanumeric

Is the field to which the result is returned, or the format of the output value enclosed in
single quotation marks (').

The string will be truncated to the number of bytes in the output field.

Example: Truncating a String


In the following, KKFCUT truncates the COUNTRY field (up to 10 bytes long) to A4 format:
COUNTRY_CUT/A4 = KKFCUT(10, COUNTRY, 'A4');

The output in ASCII environments is shown in the following image:

The output in EBCDIC environments is shown in the following image:

SFTDEL: Deleting the Shift Code From DBCS Data


If your configuration uses a DBCS code page, you can use the SFTDEL function to delete the
shift code from DBCS data.

274
8. Character Functions for DBCS Code Pages

Syntax: How to Delete the Shift Code From DBCS Data

SFTDEL(source_string, length, output)

where:
source_string
Alphanumeric

Is the string from which the shift code will be deleted enclosed in single quotation marks
('), or the field containing the string.
length
Integer

Is the length of the source string in bytes, or a field that contains the length. The string can
have a mixture of DBCS and SBCS characters. Therefore, the number of bytes represents
the maximum number of characters possible in the source string.
output
Alphanumeric

Is the field to which the result is returned, or the format of the output value enclosed in
single quotation marks (').

Example: Deleting the Shift Code From a String


In the following, SFTDEL deleted the shift code from the COUNTRY field (up to 10 bytes long):
COUNTRY_DEL/A10 = SFTDEL(COUNTRY, 10, 'A10');

The output in ASCII environments is shown in the following image:

Using Functions 275


SFTINS: Inserting the Shift Code Into DBCS Data

The output in EBCDIC environments is shown in the following image:

SFTINS: Inserting the Shift Code Into DBCS Data


If your configuration uses a DBCS code page, you can use the SFTINS function to insert the
shift code into DBCS data.

Syntax: How to Insert the Shift Code Into DBCS Data

SFTINS(source_string, length, output)

where:
source_string
Alphanumeric

Is the string into which the shift code will be inserted enclosed in single quotation marks
('), or the field containing the string.
length
Integer

Is the length of the source string in bytes, or a field that contains the length. The string can
have a mixture of DBCS and SBCS characters. Therefore, the number of bytes represents
the maximum number of characters possible in the source string.
output
Alphanumeric

Is the field to which the result is returned, or the format of the output value enclosed in
single quotation marks (').

Example: SFTINS: Inserting the Shift Code Into a String


In the following example, SFTINS inserts the shift code into the COUNTRY_DEL field (which is
the COUNTRY field with the shift code deleted):
COUNTRY_INS/A10 = SFTINS(COUNTRY_DEL, 10, 'A10');

276
8. Character Functions for DBCS Code Pages

The output displays the original COUNTRY field, the COUNTRY_DEL field with the shift code
deleted, and the COUNTRY_INS field with the shift code re-inserted.

The output in ASCII environments, is shown in the following image:

The output in EBCDIC environments is shown in the following image:

Using Functions 277


SFTINS: Inserting the Shift Code Into DBCS Data

278
Chapter 9 Maintain-specific Character Functions
Character functions manipulate alphanumeric fields or character strings. The functions in
this topic are available only in the WebFOCUS Maintain language. There are additional
character functions that are available in both the reporting and Maintain languages. For
information on these functions, see Character Functions on page 195.
In this chapter:

CHAR2INT: Translating a Character Into RJUST: Right-Justifying a Character


an Integer Value String (Maintain)

INT2CHAR: Translating an Integer Value SELECTS: Decoding a Value From a


Into a Character Stack

LCWORD and LCWORD2: Converting a STRAN: Substituting One Substring for


Character String to Mixed-Case Another

LENGTH: Determining the Length of a STRCMP: Comparing Character Strings


Character String
STRICMP: Comparing Character Strings
LJUST: Left-Justifying a Character String and Ignoring Case
(Maintain)
STRNCMP: Comparing Character
LOWER: Converting a Character String to Substrings
Lowercase
STRTOKEN: Extracting a Substring Based
MASK: Extracting or Adding Characters on Delimiters

MNTGETTOK: Extracting Tokens From a SUBSTR: Extracting a Substring


String Function (Maintain)

NLSCHR: Converting Characters From TRIM: Removing Trailing Occurrences


the Native English Code Page (Maintain)

OVRLAY: Overlaying a Character String TRIMLEN: Determining the Length of a


(Maintain) String Excluding Trailing Spaces

POSIT: Finding the Beginning of a UPCASE: Converting Text to Uppercase


Substring (Maintain) (Maintain)

Using Functions 279


CHAR2INT: Translating a Character Into an Integer Value

CHAR2INT: Translating a Character Into an Integer Value


The CHAR2INT function translates an ASCII or EBCDIC character to the integer value it
represents, depending on the operating system.

Syntax: How to Translate a Character Into an Integer Value

CHAR2INT("character")

where:
character
Is the ASCII or EBCDIC character to translate into its integer value.

Example: Translating a Character Into an Integer Value


CHAR2INT translates the character X into its integer equivalent.
MAINTAIN
INT/I3=CHAR2INT("X");
type "INT IS <INT";
END

On an ASCII platform, the integer value would be 120.

On an EBCDIC platform, the integer value would be 231.

INT2CHAR: Translating an Integer Value Into a Character


The INT2CHAR function translates an integer into the equivalent ASCII or EBCDIC character,
depending on the operating system.

Syntax: How to Translate an Integer Value Into a Character

INT2CHAR(value)

where:
value
Is the integer to translate into its equivalent ASCII or EBCDIC character.

Example: Translating an Integer Value Into a Character


INT2CHAR translates the integer value 93 into its character equivalent.
MAINTAIN
CHAR/A1=INT2CHAR(93);
TYPE "CHAR IS <CHAR";
END

280
9. Maintain-specific Character Functions

On an ASCII platform, the result would be a right bracket (]). On an EBCDIC platform, the result
would be a right parenthesis.

LCWORD and LCWORD2: Converting a Character String to Mixed-Case


The LCWORD and LCWORD2 functions convert the letters in a character string to mixed-case.
These functions convert character strings in the following way:

LCWORD. Converts every alphanumeric character to lowercase except the first letter of
each new word and the first letter after a single or double quotation mark. For example,
O'CONNOR is converted to O'Connor and JACK'S to Jack'S.

If LCWORD encounters a number in the character string, it treats it as an uppercase


character and continues to convert the following alphabetic characters to lowercase.

LCWORD2. Converts every alphanumeric character to lowercase except the first letter of
each new word. LCWORD2 leaves any character after a single quotation mark as upper
case, except that when there is at least one non-blank character before the quote and just
one character followed by either the end of the string or a space immediately after the
quote, the next letter is converted to lowercase. For example, 'O’CONNOR' would be
changed to 'O’Connor,' and JACK'S would be changed to Jack's.

To use these functions, you must import the function library MNTUWS. For information on
importing this library, see Accessing and Calling a Function on page 59.

There is also an LCWORD function available for both the reporting and Maintain languages. For
information on this function, see Character Functions on page 195.

Syntax: How to Convert a Character String to Mixed-Case

{LCWORD|LCWORD2}(string)

where:
string
Alphanumeric

Is the character string to be converted, or a temporary field that contains the string.

Using Functions 281


LENGTH: Determining the Length of a Character String

Example: Converting a Character String to Mixed-Case


LCWORD and LCWORD2 convert the string O'CONNOR to mixed-case:

MAINTAIN FILE CAR


MODULE IMPORT (MNTUWS)
COMPUTE MYVAL1/A10="O'CONNOR";
COMPUTE LC1/A10 = LCWORD(MYVAL1);
COMPUTE LC2/A10 = LCWORD2(MYVAL1);
TYPE "<<MYVAL1 <<LC1 <<LC2"
END

The output is:


MYVAL1 LC1 LC2
------ --- ---
O'CONNOR O'Connor O'connor

LENGTH: Determining the Length of a Character String


The LENGTH function determines the length of a character string, including trailing spaces.

Syntax: How to Determine the Length of a Character String

LENGTH(string)

where:
string
Alphanumeric

Is the character string whose length is to be found, or a temporary field that contains the
string.

Example: Determining the Length of a Character String


LENGTH determines the length of a variable in COUNTRY:

MAINTAIN FILE CAR


MODULE IMPORT (MNTUWS)
NEXT COUNTRY INTO STK1
COMPUTE LEN/I3 = LENGTH(STK1(1).COUNTRY);
TYPE "<STK1(1).COUNTRY HAS A LENGTH OF <<LEN"
END

The result is:


ENGLAND HAS A LENGTH OF 10

282
9. Maintain-specific Character Functions

LJUST: Left-Justifying a Character String (Maintain)


The LJUST function left-justifies a character string within a field. All leading spaces are
removed.

LJUST will not have any visible effect in a report that uses StyleSheets (SET STYLE=ON) unless
you center the item.

To use this function, you must import the function library MNTUWS. For information on
importing this library see Accessing and Calling a Function on page 59.

There is also an LJUST function available for the reporting language. For information on this
function, see Character Functions on page 195.

Syntax: How to Left-Justify a Character String

LJUST(string)

where:
string
Alphanumeric

Is the character string to be justified, or a temporary field that contains the string.

LOWER: Converting a Character String to Lowercase


The LOWER function converts a character string to lowercase.

To use this function, you must import the function library MNTUWS. For more information on
importing this library see Accessing and Calling a Function on page 59.

Syntax: How to Convert a Character String to Lowercase

LOWER(string)

where:
string
Alphanumeric

Is the character string to be converted, or a temporary field that contains the string.

Using Functions 283


MASK: Extracting or Adding Characters

MASK: Extracting or Adding Characters


The MASK function extracts characters from or adds characters to an alphanumeric string. It
can extract a substring from different parts of the parent string, and can insert characters from
a parent string into another substring. For example, it can extract the first two characters and
the last two characters of a string to form a single substring.

MASK works by comparing the characters in a mask to the characters in a source field. When
it encounters a 9 in the mask, MASK copies the corresponding character from the source field
to the new field. When it encounters a dollar sign in the mask, MASK ignores the
corresponding character in the source field. When it encounters any other character in the
mask, MASK copies that character to the corresponding position in the new field.

MASK replaces the masking functionality of the EDIT function that is available in the reporting
language.

Syntax: How to Extract or Add Characters

MASK(fieldname, 'mask')

where:
fieldname
Is the source field.
mask
Is a character string enclosed in single quotation marks, or a temporary field that contains
the string.

Example: Extracting a Character From a Field


MASK extracts the first initial from the FIRST_NAME field:

MASK(FIRST_NAME, '9$$$$$$$$$')

The following are sample values for FIRST_NAME and the values for the result of the MASK
function:
FIRST_NAME MASK_FIRST_NAME
---------- ---------------
MARY M
DIANE D
JOHN J
ROSEMARIE R
MARY M
BARBARA B

284
9. Maintain-specific Character Functions

Example: Adding Dashes to a Field


MASK adds dashes to the EMP_ID field:
MASK(EMP_ID, '999-99-9999')

The following are sample values for EMP_ID and the values for the result of the MASK function:
EMP_ID MASK_EMP_ID
------ -----------
112847612 112-84-7612
117593129 117-59-3129
219984371 219-98-4371
326179357 326-17-9357
543729165 543-72-9165
818692173 818-69-2173

MNTGETTOK: Extracting Tokens From a String Function


The Maintain function MNTGETTOK divides a character string into substrings, called tokens. In
order to use MNTGETTOK, the data must have a specific character called a delimiter that
occurs in the string and separates the string into tokens. MNTGETTOK returns the token
specified by the token_number argument.

For example, you can use MNTGETTOK to extract individual values from a list separated by
semi-colons, by designating the semi-colon as the delimiter.

To use this function, you must import the function library MNTUWS.
Note:

The Maintain function called strtoken() returns only the first token from a string.

MNTGETTOK can work with variable length character strings (format A0).

Syntax: How to Extract a Substring (Token)

Module Import(mntuws)
MNTGETTOK(infield,"delim",token_number)

where:
infield
Alphanumeric

Is the field containing the original character string or a character string enclosed in single
or double quotation marks.
delim
Alphanumeric

Using Functions 285


MNTGETTOK: Extracting Tokens From a String Function

Is the delimiter in the parent string enclosed in single or double quotation marks. If you
specify more than one character, only the first character is used. The delimiter is not
included in the token.
token_number
Integer

Is the number of the token to extract. If this argument is positive, the tokens are counted
from left to right. If this argument is negative, the tokens are counted from right to left. For
example, -2 extracts the second token from the right. If this argument is 0, the function
returns spaces.

Example: Extracting Tokens From a String


MNTGETTOK extracts tokens from the variable length character string SKILLSTRING and stores
the result in the variable length character string TOKENX. The delimiter is a blank space. The
token number is based on the value of the counter variable i, which increments with each pass
through the Repeat loop:
MAINTAIN
MODULE IMPORT(MNTUWS)
SKILLSTRING/A0="Typing Steno Filing Bkkping";
COMPUTE i/i2 = 1;
TYPE "Job skills required are:"
REPEAT 6
COMPUTE TOKENX/A0=MNTGETTOK(SKILLSTRING, ' ', i );
TYPE "<<TOKENX";
COMPUTE i = i+1;
ENDREPEAT
END

The output is:


Job skills required are:
Typing
Steno
Filing
Bkkping

286
9. Maintain-specific Character Functions

Example: Extracting the Zip Code From an Address


The following procedure against the EMPLOYEE data source retrieves the EMPINFO segment
and the first instance of ADDRESS_LN3 for each employee, then extracts the last token (zip
code) from ADDRESS_LN3:
MAINTAIN FILE EMPLOYEE
MODULE IMPORT(MNTUWS)
REPEAT ALL;
NEXT EMP_ID INTO ESTACK
IF FOCFETCH NE 0 THEN GOTO EXITREPEAT;
NEXT ADDRESS_LN3 INTO ASTACK
TYPE "<<ESTACK.FIRST_NAME <<ESTACK.LAST_NAME";
TYPE "<<ASTACK.ADDRESS_LN3";
COMPUTE ZIP/A0=MNTGETTOK(ASTACK.ADDRESS_LN3, " ", -1 );
TYPE "ZIP CODE IS: <<ZIP";
TYPE " ";
ENDREPEAT
END

Using Functions 287


NLSCHR: Converting Characters From the Native English Code Page

The output is:

ALFRED STEVENS
NEW YORK NY 10001
ZIP CODE IS: 10001

MARY SMITH
NEW YORK NY 10001
ZIP CODE IS: 10001

DIANE JONES
NEW YORK NY 10001
ZIP CODE IS: 10001

RICHARD SMITH
NEW YORK NY 10001
ZIP CODE IS: 10001

JOHN BANNING
FREEPORT NY 11520
ZIP CODE IS: 11520

JOAN IRVING
NEW YORK NY 10001
ZIP CODE IS: 10001
ANTHONY ROMANS
NEW YORK NY 10001
ZIP CODE IS: 10001

JOHN MCCOY
NEW YORK NY 10001
ZIP CODE IS: 10001

ROSEMARIE BLACKWOOD
NEW YORK NY 10001
ZIP CODE IS: 10001

ROGER MCKNIGHT
NEW YORK NY 10001
ZIP CODE IS: 10001

MARY GREENSPAN
NEW YORK NY 10001
ZIP CODE IS: 10001

BARBARA CROSS
NEW YORK NY 10001
ZIP CODE IS: 10001

NLSCHR: Converting Characters From the Native English Code Page


NLSCHR converts a character from the native English code page to the running code page. This
is useful when hosting Web applications on an EBCDIC host with non-English code pages.

288
9. Maintain-specific Character Functions

Syntax: How to Convert Characters From the Native English Code Page

NLSCHR("character")

where:
character
Is the character being converted from the native English code page.

Example: Converting Characters From the Native English Code Page


NLSCHR forces the dollar sign to appear whenever the variable ADOLLAR is used, regardless
of the code page being run.

MAINTAIN
ADOLLAR/A1=NLSCHR("$");
.
.
.
END

OVRLAY: Overlaying a Character String (Maintain)


The OVRLAY function overlays a base character string with a substring.

To use this function, you must import the function library MNTUWS. For information on
importing this library, see Accessing and Calling a Function on page 59.

There is also an OVRLAY function available for the reporting language. For information on this
function, see Character Functions on page 195.

Syntax: How to Overlay a Character String

OVRLAY(string1, string2, position)

where:
string1
Alphanumeric

Is the base character string.


string2
Alphanumeric

Is the substring that will overlay string1.

Using Functions 289


POSIT: Finding the Beginning of a Substring (Maintain)

position
Integer

Is the position in the base string at which the overlay begins.

Example: Overlaying a Character String


OVRLAY replaces the letters MCA in the MOVIECODE field with MHD:
MAINTAIN FILE movies
Module Import (mntuws);
Case Top
Infer moviecode into MCASTK
Compute MCASTK.NEWCODE/A6;
For all next Moviecode into stk1
Stack copy from stk1 into MCASTK
where moviecode contains 'MCA';
Compute i/i2=1;
Type "Original Code New Code"
repeat mcastk.Foccount
Compute MCASTK(i).Newcode = OVRLAY(MCASTK(I).MOVIECODE, 'MHD', 4);
Type " <<MCASTK(i).moviecode <<MCASTK(I).NEWCODE"
Compute i=i+1;
endrepeat
EndCase
END

The following are sample values for MOVIECODE and the values for the result of the OVRLAY
function:
Original Code New Code
001MCA 001MHD
081MCA 081MHD
082MCA 082MHD
161MCA 161MHD
196MCA 196MHD
530MCA 530MHD
550MCA 550MHD
883MCA 883MHD

POSIT: Finding the Beginning of a Substring (Maintain)


The POSIT function finds the starting position of a substring within a larger string. For example,
the starting position of the substring DUCT in the string PRODUCTION is 4. If the substring is
not in the parent string, the function returns the value 0.

To use this function, you must import the function library MNTUWS. For information on
importing this library see Accessing and Calling a Function on page 59.

There is also a POSIT function available for the reporting language. For information on this
function, see POSIT: Finding the Beginning of a Substring on page 227.

290
9. Maintain-specific Character Functions

Syntax: How to Find the Beginning of a Substring

POSIT(parent, substring)

where:
parent
Alphanumeric

Is the parent string.


substring
Alphanumeric

Is the substring for which to find the position.

Example: Finding the Beginning of a Substring


POSIT displays all movie titles containing the word ROOF and the starting position of the ROOF
string:
MAINTAIN FILE movies
Module Import (mntuws);
Case Top
For all next Moviecode into stk1
Where Title Contains 'ROOF';
Compute i/i2=1;
type " Title Start Position of word ROOF"
repeat stk1.Foccount
Compute STK1(i).POS/I3 = POSIT(STK1(I).TITLE, 'ROOF');
Type " <STK1(i).Title <<STK1(I).pos"
Compute i=i+1;
endrepeat
EndCase
END

The following are sample values for MOVIECODE and values for the result of the POSIT
function:
Title Start Position of word ROOF
FIDDLER ON THE ROOF 16
CAT ON A HOT TIN ROOF 18

RJUST: Right-Justifying a Character String (Maintain)


The RJUST function right-justifies a character string. All trailing blanks become leading blanks.
This is useful when you display alphanumeric fields containing numbers.

RJUST does not have any visible effect in a report that uses StyleSheets (SET STYLE=ON)
unless you center the item. Also, if you use RJUST on a platform on which StyleSheets are
turned on by default, issue SET STYLE=OFF before running the request.

Using Functions 291


SELECTS: Decoding a Value From a Stack

There is also an RJUST function available for the reporting language. For information on this
function, see RJUST: Right-Justifying a Character String on page 230.

Syntax: How to Right-Justify a Character String

RJUST(string, length, char)

where:
string
Is the character string, or a temporary field that contains the string.
length
Is the length, in characters, of the result. If this argument is less than the length of string,
RJUST trims string from right to left. If this argument is zero, RJUST returns a variable
length string of length zero.
char
Is the character with which to pad the character string and right-justify it. RJUST uses char
only when length is greater than the length of string.

SELECTS: Decoding a Value From a Stack


The SELECTS function decodes a value from a stack.

Syntax: How to Decode a Value From a Stack

target SELECTS (code, result, code, result, ... [ELSE default])

where:
target
Is a valid expression. It can be either a field name or a variable that resolves to a single
stack cell.
code
Is the value for which SELECTS searches. Once the value is found, the input expression is
assigned the corresponding result. The comma between the code and result is optional.
result
Is the value assigned when the input expression has the corresponding code.

292
9. Maintain-specific Character Functions

default
Is the value to be assigned if the code is not found among the list of codes. If the default
is omitted, a space or zero is assigned to non-matching codes.

Example: Decoding Values With SELECTS


The following computes a user-defined field based on the values in a stack:
COMPUTE Square = Stk(Cnt).Number SELECTS (1 1, 2 4, 3 9);

Because SELECTS is a binary operator, it can also be used in an expression:


COMPUTE Square_Plus = Stk(Cnt).Number SELECTS (1 1, 2 4, 3 9) +1;

Example: Decoding a Value From a Stack


The following example uses MASK to extract the first character of the field CURR_JOBCODE in
the EMPLOYEE file. Then SELECTS creates a value for the field JOB_CATEGORY:
MAINTAIN FILE Employee
Case Top
FOR ALL NEXT EMPINFO.EMP_ID INTO EmpStack;
COMPUTE
DEPX_CODE/A1 = MASK(EmpStack().CURR_JOBCODE,'9$$');
JOB_CATEGORY/A15 = DEPX_CODE SELECTS (A 'ADMINISTRATIVE'
B 'DATA PROCESSING') ;
EndCase
END

The following table shows sample values for CURR_JOBCODE and the corresponding values for
JOB_CATEGORY:
CURR_JOBCODE JOB_CATEGORY
------------ ------------
A01 ADMINISTRATIVE
A07 ADMINISTRATIVE
A15 ADMINISTRATIVE
A17 ADMINISTRATIVE
B02 DATA PROCESSING
B03 DATA PROCESSING
B04 DATA PROCESSING
B14 DATA PROCESSING

STRAN: Substituting One Substring for Another


The STRAN function substitutes a substring for another substring in a character string. STRAN
enables you to edit part of a character string without replacing the field entirely.

To use this function, import the function library MNTUWS. For more information on importing
this library see Calling a Function on page 59.

Using Functions 293


STRAN: Substituting One Substring for Another

Syntax: How to Substitute a Substring

STRAN(string, substr1, substr2)

where:
string
Alphanumeric

Is the character string into which you want to substitute one substring for another, or a
temporary field that contains the string.
substr1
Alphanumeric

Is the substring to replace.


substr2
Alphanumeric

Is the substring to insert in place of substr1.

Example: Substituting One String for Another


STRAN replaces the word DOOR with the word Seater in the MODEL field:
MAINTAIN FILE CAR
MODULE IMPORT (MNTUWS);
FOR ALL NEXT COUNTRY CAR MODEL INTO XSTK
WHERE MODEL CONTAINS 'DOOR'
COMPUTE XSTK.NEWMOD/A24;
COMPUTE I/I2=1;
REPEAT XSTK.FOCCOUNT
COMPUTE XSTK(I).NEWMOD=STRAN(XSTK(I).MODEL,'DOOR','SEATER');
TYPE "<<XSTK(I).CAR <<XSTK(I).MODEL <<XSTK(I).NEWMOD"
COMPUTE I=I+1;
ENDREPEAT
END

294
9. Maintain-specific Character Functions

The following are sample values for MODEL and values for the result of the STRAN function:
CAR MODEL STRAN
--- ----- -----
PEUGEOT 504 4 DOOR 504 4 SEATER
ALFA ROMEO 2000 4 DOOR BERLINA 2000 4 SEATER BERLINA
MASERATI DORA 2 DOOR DORA 2 SEATER
DATSUN B210 2 DOOR AUTO B210 2 SEATER AUTO
TOYOTA COROLLA 4 DOOR DIX AUTO COROLLA 4 SEATER DIX AUT
AUDI 100 LS 2 DOOR AUTO 100 LS 2 SEATER AUTO
BMW 2002 2 DOOR 2002 2 SEATER
BMW 2002 2 DOOR AUTO 2002 2 SEATER AUTO
BMW 3.0 SI 4 DOOR 3.0 SI 4 SEATER
BMW 3.0 SI 4 DOOR AUTO 3.0 SI 4 SEATER AUTO
BMW 530I 4 DOOR 530I 4 SEATER
BMW 530I 4 DOOR AUTO 530I 4 SEATER AUTO

STRCMP: Comparing Character Strings


The STRCMP function compares two character strings using the EBCDIC or ASCII collating
sequence.

If the first string is less than the second string, STRCMP returns a negative value.

If the first string is greater than the second string, STRCMP returns a positive value.

If the first string is equal to the second string, STRCMP returns zero.

Syntax: How to Compare Character Strings

STRCMP(string1, string2)

where:
string1, string2
Alphanumeric

Are the strings to compare, or temporary fields that contain the strings.

Example: Comparing Character Strings


STRCMP compares the length of two fields:

MAINTAIN
COMPUTE STR1/A20 = 'STRING IS LONG';
STR2/A20 = 'STRING IS LONGER';
COMPUTE DIF/I3= STRCMP(STR1, STR2);
TYPE "STR1 = <<STR1"
TYPE "STR2 = <<STR2"

Using Functions 295


STRICMP: Comparing Character Strings and Ignoring Case

IF DIF LT 0 THEN TYPE "STR2 IS GREATER THAN STR1"


ELSE IF DIF GT 0 THEN TYPE "STR2 IS LESS THAN STR1"
ELSE IF DIF EQ 0 THEN TYPE "STR2 EQUALS STR1"
TYPE " "

COMPUTE STR3/A20 = 'STRING IS LONGEST';


STR4/A20 = 'STRING IS LONG';
TYPE "STR3 = <<STR3"
TYPE "STR4 = <<STR4"
COMPUTE DIF= STRCMP(STR3, STR4);
IF DIF LT 0 THEN TYPE "STR4 IS GREATER THAN STR3"
ELSE IF DIF GT 0 THEN TYPE "STR4 IS LESS THAN STR3"
ELSE IF DIF EQ 0 THEN TYPE "STR4 EQUALS STR3"
TYPE " "
COMPUTE DIF= STRCMP(STR1, STR4);
IF DIF LT 0 THEN TYPE "STR1 IS GREATER THAN STR4"
ELSE IF DIF GT 0 THEN TYPE "STR1 IS LESS THAN STR4"
ELSE IF DIF EQ 0 THEN TYPE "STR1 EQUALS STR4"
END

The result is:


STR1 = STRING IS LONG
STR2 = STRING IS LONGER
STR2 IS GREATER THAN STR1
STR3 = STRING IS LONGEST
STR4 = STRING IS LONG
STR4 IS LESS THAN STR3
STR1 EQUALS STR4

STRICMP: Comparing Character Strings and Ignoring Case


The STRICMP function compares two character strings using the EBCDIC or ASCII collating
sequence, but ignores case differences.

If the first string is less than the second string, STRICMP returns a negative value.

If the first string is greater than the second string, STRICMP returns a positive value.

If the first string is equal to the second string, STRICMP returns zero.

296
9. Maintain-specific Character Functions

Syntax: How to Compare Character Strings and Ignore Case

STRICMP(string1, string2)

where:
string1, string2
Alphanumeric

Are the strings to compare, or temporary fields that contain the strings.

STRNCMP: Comparing Character Substrings


The STRNCMP function compares a specified number of characters in two character strings
starting at the beginning of the strings using the EBCDIC or ASCII collating sequence.

If the first string is less than the second string, STRNCMP returns a negative value.

If the first string is greater than the second string, STRNCMP returns a positive value.

If the first string is equal to the second string, STRNCMP returns zero.

Syntax: How to Compare Character Substrings

STRNCMP(string1, string2, number)

where:
string1, string2
Alphanumeric

Are the strings that contain the substrings to compare.


number
Integer

Is the number of characters to compare in string1 and string2.

STRTOKEN: Extracting a Substring Based on Delimiters


The STRTOKEN function returns a substring, consisting of the characters of a string, from the
beginning of a string to a specified character, called a delimiter.

To use this function, you must import the function library MNTUWS. For more information on
importing this library see Calling a Function on page 59.

Using Functions 297


STRTOKEN: Extracting a Substring Based on Delimiters

Syntax: How to Extract a Substring

STRTOKEN(string, delimiters)

where:
string
Alphanumeric

Is the character string, or a variable that contains the string enclosed in double quotation
marks (").
delimiters
Alphanumeric

Is a character string, or variable enclosed in double quotation marks (") that contains a list
of delimiters. Separate the delimiters with semicolons.

Example: Extracting a Substring


STRTOKEN returns a substring of the first five STREET values in the VIDEOTRK data source
based on the delimiters period, space, or asterisk.
MAINTAIN FILE VIDEOTRK
MODULE IMPORT (MNTUWS);
FOR ALL NEXT CUSTID INTO CSTACK ;
COMPUTE CNT/I5 = 1;
TYPE " ";
REPEAT WHILE CNT LE 5;
COMPUTE SUBSTREET/A20 = STRTOKEN(CSTACK(CNT).STREET,".; ,*");
TYPE " STREET = <CSTACK(CNT).STREET"
TYPE " SUBSTREET = <SUBSTREET "
COMPUTE CNT = CNT +1;
ENDREPEAT
END

The output is:


STREET = 86 ELLIOTT AVE.
SUBSTREET = 86
STREET = 7 DAVENPORT LA.
SUBSTREET = 7
STREET = 8 MAGNOLIA LA.
SUBSTREET = 8
STREET = 35 POWELL ST.
SUBSTREET = 35
STREET = 10 COW LA.
SUBSTREET = 10

298
9. Maintain-specific Character Functions

SUBSTR: Extracting a Substring (Maintain)


The SUBSTR function extracts a substring based on where it begins and its length in the
parent string. SUBSTR can vary the position of the substring depending on the values of other
fields.

There is also a SUBSTR function available for the reporting language. For information on this
function, see SUBSTR: Extracting a Substring on page 239.

Syntax: How to Extract a Substring

SUBSTR(string, start, length)

where:
string
Alphanumeric

Is the parent string enclosed in single quotation marks, or a field or variable containing the
character string.
start
Integer

Is the starting position of the substring in the parent string.


length
Integer

Is the length, in characters, of the substring.

Example: Extracting the First Character of a String in Maintain


SUBSTR extracts the first letter of FIRST_NAME, combines it with LAST_NAME, and stores the
result in UID:
MAINTAIN FILE EMPLOYEE
CASE TOP
INFER EMP_ID FIRST_NAME LAST_NAME INTO ADDSTACK
COMPUTE UID/A9 = SUBSTR(ADDSTACK().FIRST_NAME,1,1) ||
ADDSTACK().LAST_NAME;
ENDCASE
END

Using Functions 299


TRIM: Removing Trailing Occurrences (Maintain)

The following table shows sample values for FIRST_NAME and LAST_NAME, and the
corresponding values for UID:
FIRST_NAME LAST_NAME UID
JOE SMITH JSMITH
SAM JONES SJONES
TERRI WHITE TWHITE

TRIM: Removing Trailing Occurrences (Maintain)


The TRIM function removes trailing occurrences of a pattern within a character string.

There is also a TRIM function available for the reporting language. For information on this
function, see TRIM: Removing Leading and Trailing Occurrences on page 241.

Syntax: How to Remove Trailing Occurrences

TRIM(string)

where:
string
Alphanumeric

Is the character string enclosed in single quotation marks, or the field containing the
string.

TRIMLEN: Determining the Length of a String Excluding Trailing Spaces


The TRIMLEN function determines the length of a character string excluding trailing spaces.

Syntax: How to Determine the Length of a String Excluding Trailing Spaces

TRIMLEN (string)

where:
string

Alphanumeric

Is the string to be measured.

300
9. Maintain-specific Character Functions

Example: Determining the Length of a String Excluding Trailing Spaces


TRIMLEN determines the length of a field in COUNTRY excluding trailing blanks:
MAINTAIN FILE CAR
MODULE IMPORT (MNTUWS)
NEXT COUNTRY INTO STK1
COMPUTE LEN/I3 = LENGTH(STK1(1).COUNTRY);
COMPUTE LEN2/I3 = TRIMLEN(STK1(1).COUNTRY);
TYPE "<STK1(1).COUNTRY HAS A LENGTH OF <LEN2 WITHOUT TRAILING BLANKS"
END

The result is:


ENGLAND HAS A LENGTH OF 7 WITHOUT TRAILING BLANKS

UPCASE: Converting Text to Uppercase (Maintain)


The UPCASE function converts a character string to uppercase. It is useful for sorting on a field
that contains both mixed-case and uppercase values. Sorting on a mixed-case field produces
incorrect results because the sorting sequence in EBCDIC always places lowercase letters
before uppercase letters, while the ASCII sorting sequence always places uppercase letters
before lowercase. To obtain correct results, define a new field with all of the values in
uppercase, and sort on that.

To use this function, you must import the function library MNTUWS. For information on
importing this library, see Calling a Function on page 59.

There is also an UPCASE function available for the reporting language. For information on this
function, see UPCASE: Converting Text to Uppercase on page 244.

Syntax: How to Convert Text to Uppercase

UPCASE(string)

where:
string
Alphanumeric

Is the character string to be converted to uppercase.

Using Functions 301


UPCASE: Converting Text to Uppercase (Maintain)

302
Chapter 10 Data Source and Decoding Functions
Data source and decoding functions search for data source records, retrieve data source
records or values, and assign values based on the value of an input field.

The result of a data source function must be stored in a field. The result cannot be
stored in a Dialogue Manager variable.

For many functions, the output argument can be supplied either as a field name or as a
format enclosed in single quotation marks (‘). However, if a function is called from a
Dialogue Manager command, this argument must always be supplied as a format. If a
function is called from a Maintain Data procedure, this argument must always be
supplied as a field name. For detailed information about calling a function and supplying
arguments, see Accessing and Calling a Function on page 59.
In this chapter:

CHECKMD5: Computing an MD5 Hash Check Value

CHECKSUM: Computing a Hash Sum

COALESCE: Returning the First Non-Missing Value

DB_EXPR: Inserting an SQL Expression Into a Request

DB_INFILE: Testing Values Against a File or an SQL Subquery

DB_LOOKUP: Retrieving Data Source Values

DECODE: Decoding Values

FIND: Verifying the Existence of a Value in a Data Source

IMPUTE: Replacing Missing Values With Aggregated Values

LAST: Retrieving the Preceding Value

LOOKUP: Retrieving a Value From a Cross-referenced Data Source

NULLIF: Returning a Null Value When Parameters Are Equal

Using Functions 303


CHECKMD5: Computing an MD5 Hash Check Value

CHECKMD5: Computing an MD5 Hash Check Value


CHECKMD5 takes an alphanumeric input value and returns a 128-bit value in a fixed length
alphanumeric string, using the MD5 hash function. A hash function is any function that can be
used to map data of arbitrary size to data of fixed size. The values returned by a hash function
are called hash values. They can be used for assuring the integrity of transmitted data.

Syntax: How to Compute an MD5 Hash Check Value

CHECKMD5(buffer)

where:
buffer
Is a data buffer whose hash value is to be calculated. It can be a set of data of different
types presented as a single field, or a group field in one of the following data type formats:
An, AnV, or TXn.

Example: Calculating an MD5 Hash Check Value


The following request calculates an MD5 hash check value and converts it to an alphanumeric
hexadecimal value for display.
DEFINE FILE WF_RETAIL_LITE
MD5/A32 = HEXTYPE(CHECKMD5(PRODUCT_CATEGORY));
END
TABLE FILE WF_RETAIL_LITE
SUM MD5
BY PRODUCT_CATEGORY
ON TABLE SET PAGE NOLEAD
ON TABLE SET STYLE *
TYPE=REPORT,FONT=COURIER,$
ENDSTYLE
END

304
10. Data Source and Decoding Functions

The output is shown in the following image. The monospaced font shows that although the
input values have varying length, the output has a fixed length.

CHECKSUM: Computing a Hash Sum


CHECKSUM computes a hash sum, called the checksum, of its input parameter, as a whole
number in format I11. This can be used for equality search of the fields. A checksum is a hash
sum used to ensure the integrity of a file after it has been transmitted from one storage device
to another.

Syntax: How to Compute a CHECKSUM Hash Value

CHECKSUM(buffer)

where:
buffer
Is a data buffer whose hash index is to be calculated. It can be a set of data of different
types presented as a single field, in one of the following data type formats: An, AnV, or
TXn.

Using Functions 305


COALESCE: Returning the First Non-Missing Value

Example: Calculating a CHECKSUM Hash Value


The following request computes a checksum hash value.
DEFINE FILE WF_RETAIL_LITE
CHKSUM/I11 = (CHECKSUM(PRODUCT_CATEGORY));
END
TABLE FILE WF_RETAIL_LITE
PRINT CHKSUM
BY PRODUCT_CATEGORY
WHERE PRODUCT_CATEGORY NE LAST PRODUCT_CATEGORY
ON TABLE SET PAGE NOLEAD
END

The output is shown in the following image.

COALESCE: Returning the First Non-Missing Value


Given a list of arguments, COALESCE returns the value of the first argument that is not
missing. If all argument values are missing, it returns a missing value if MISSING is ON.
Otherwise it returns a default value (zero or blank).

Syntax: How to Return the First Non-Missing Value

COALESCE(arg1, arg2, ...)

where:
arg1, arg2, ...
Any field, expression, or constant. The arguments should all be either numeric or
alphanumeric.

Are the input parameters that are tested for missing values.

306
10. Data Source and Decoding Functions

The output data type is the same as the input data types.

Example: Returning the First Non-Missing Value


This example uses the SALES data source with missing values added. The missing values are
added by the following procedure named SALEMISS:
MODIFY FILE SALES
FIXFORM STORE/4 DATE/5 PROD/4
FIXFORM UNIT/3 RETAIL/5 DELIVER/3
FIXFORM OPEN/3 RETURNS/C2 DAMAGED/C2
MATCH STORE
ON NOMATCH REJECT
ON MATCH CONTINUE
MATCH DATE
ON NOMATCH REJECT
ON MATCH CONTINUE
MATCH PROD_CODE
ON NOMATCH INCLUDE
ON MATCH REJECT
DATA
14Z 1017 C13 15 1.99 35 30 6
14Z 1017 C14 18 2.05 30 25 4
14Z 1017 E2 33 0.99 45 40
END

The following request uses COALESCE to return the first non-missing value:
TABLE FILE SALES
PRINT DAMAGED RETURNS RETAIL_PRICE
COMPUTE
COAL1/D12.2 MISSING ON = COALESCE(DAMAGED, RETURNS, RETAIL_PRICE);
BY STORE_CODE
ON TABLE SET PAGE NOLEAD
ON TABLE SET STYLE *
GRID=OFF,$
ENDSTYLE
END

Using Functions 307


DB_EXPR: Inserting an SQL Expression Into a Request

The output is shown in the following image. The value of DAMAGED is returned, if it is not
missing. If DAMAGED is missing, the value of RETURNS is returned, if it is not missing. If they
are both missing, the value of RETAIL_PRICE is returned.

DB_EXPR: Inserting an SQL Expression Into a Request


The DB_EXPR function inserts a native SQL expression exactly as entered into the native SQL
generated for a FOCUS or SQL language request.

308
10. Data Source and Decoding Functions

The DB_EXPR function can be used in a DEFINE command, a DEFINE in a Master File, a
WHERE clause, a FILTER FILE command, a filter in a Master File, or in an SQL statement. It
can be used in a COMPUTE command if the request is an aggregate request (uses the SUM,
WRITE, or ADD command) and has a single display command. The expression must return a
single value.

Syntax: How to Insert an SQL Expression Into a Request With DB_EXPR

DB_EXPR(native_SQL_expression)

where:
native_SQL_expression
Is a partial native SQL string that is valid to insert into the SQL generated by the request.
The SQL string must have double quotation marks (") around each field reference, unless
the function is used in a DEFINE with a WITH phrase.

Reference: Usage Notes for the DB_EXPR Function


The expression must return a single value.

Any request that includes one or more DB_EXPR functions must be for a synonym that has
a relational SUFFIX.

Field references in the native SQL expression must be within the current synonym context.

The native SQL expression must be coded inline. SQL read from a file is not supported.

Example: Inserting the DB2 BIGINT and CHAR Functions Into a TABLE Request
The following TABLE request against the WF_RETAIL data source uses the DB_EXPR function in
the COMPUTE command to call two DB2 functions. It calls the BIGINT function to convert the
squared revenue to a BIGINT data type, and then uses the CHAR function to convert that value
to alphanumeric.
TABLE FILE WF_RETAIL
SUM REVENUE NOPRINT
AND COMPUTE BIGREV/A31 = DB_EXPR(CHAR(BIGINT("REVENUE" * "REVENUE") ) ) ;
AS 'Alpha Square Revenue'
BY REGION
ON TABLE SET PAGE NOPAGE
END

WF_RETAIL is a sample data source you can create by right-clicking an application on the
Reporting Server Web Console and pointing to New and then clicking Tutorials from the context
menu.

Using Functions 309


DB_INFILE: Testing Values Against a File or an SQL Subquery

The trace shows that the expression from the DB_EXPR function was inserted into the DB2
SELECT statement:
SELECT
T11."REGION",
SUM(T1."Revenue"),
((CHAR(BIGINT( SUM(T1."Revenue") * SUM(T1."Revenue")) ) ))
FROM
wrd_fact_sales T1,
wrd_dim_customer T5,
wrd_dim_geography T11
WHERE
(T5."ID_CUSTOMER" = T1."ID_CUSTOMER") AND
(T11."ID_GEOGRAPHY" = T5."ID_GEOGRAPHY")
GROUP BY
T11."REGION "
ORDER BY
T11."REGION "
FOR FETCH ONLY;
END

The output is:

DB_INFILE: Testing Values Against a File or an SQL Subquery


The DB_INFILE function compares one or more field values in a source file to values in a target
file. The comparison can be based on one or more field values. DB_INFILE returns the value 1
(TRUE) if the set of source fields matches a set of values from the target file. Otherwise, the
function returns 0 (zero, FALSE). DB_INFILE can be used where a function is valid in a
WebFOCUS request, such as in a DEFINE or a WHERE phrase.

310
10. Data Source and Decoding Functions

The target file can be any data source that WebFOCUS can read. Depending on the data
sources accessed and the components in the request, either WebFOCUS or an RDBMS will
process the comparison of values.

If WebFOCUS processes the comparison, it reads the target data source and dynamically
creates a sequential file containing the target data values, along with a synonym describing the
data file. It then builds IF or WHERE structures in memory with all combinations of source and
target values. If the target data contains characters that WebFOCUS considers wildcard
characters, it will treat them as wildcard characters unless the command SET EQTEST = EXACT
is in effect.

The following situations exist when a relational data source is the source file:

The target values are in a relational data source from the same RDBMS and connection.
In this case, the target file referenced by DB_INFILE can be:

An SQL file containing a subquery that retrieves the target values. A synonym must exist
that describes the target SQL file. The Access File must specify the CONNECTION and
DATASET for the target file.

If the subquery results in a SELECT statement supported by the RDBMS, the relational
adapter inserts the subquery into the WHERE predicate of the generated SQL.

If the subquery does not result in a valid SELECT statement for the RDBMS, the
relational adapter retrieves the target values. It then generates a WHERE predicate, with
a list of all combinations of source and target field values.

You can create an SQL file containing a subquery and a corresponding synonym using
the HOLD FORMAT SQL_SCRIPT command. For more information, see theCreating
Reports With WebFOCUS Languagemanual.

A relational data source. A synonym must exist that describes the target data source.

If the data source contains only those fields referenced by DB_INFILE as target fields,
the relational adapter creates a subquery that retrieves the target values. If the
subquery results in a SELECT statement supported by the RDBMS, the relational
adapter inserts the subquery into the WHERE predicate of the generated SQL.

If the subquery does not result in a valid SELECT statement for the RDBMS, the
relational adapter retrieves a unique list of the target values. It then generates a
WHERE predicate with a list of all combinations of source and target field values.

Using Functions 311


DB_INFILE: Testing Values Against a File or an SQL Subquery

The target values are in a non-relational data source or a relational data source from a
different RDBMS or connection. In this case, the target values are retrieved and passed to
WebFOCUS for processing.

Syntax: How to Compare Source and Target Field Values With DB_INFILE

DB_INFILE(target_file, s1, t1, ... sn, tn)

where:
target_file
Is the synonym for the target file.
s1, ..., sn
Are fields from the source file.
t1, ..., tn
Are fields from the target file.

The function returns the value 1 if a set of target values matches the set of source values.
Otherwise, the function returns a zero (0).

Reference: Usage Notes for DB_INFILE


If both the source and target data sources have MISSING=ON for a comparison field, then
a missing value in both files is considered an equality. If MISSING=OFF in one or both files,
a missing value in one or both files results in an inequality.

Values are not padded or truncated when compared, except when comparing date and date-
time values.

If the source field is a date field and the target field is a date-time field, the time
component is removed before comparison.

If the source field is a date-time field and the target field is a date field, a zero time
component is added to the target value before comparison.

If an alphanumeric field is compared to a numeric field, an attempt will be made to convert


the alphanumeric value to a number before comparison.

If WebFOCUS processes the comparison, and the target data contains characters that
WebFOCUS considers wildcard characters, it will treat them as wildcard characters unless
the command SET EQTEST = EXACT is in effect.

312
10. Data Source and Decoding Functions

Example: Comparing Source and Target Values Using an SQL Subquery File
This example uses the WF_RETAIL DB2 data source.

WF_RETAIL is a sample data source you can create by right-clicking an application on the
Reporting Server Web Console, selecting New, and then Samples from the context menu.

The SQL file named retail_subquery.sql contains the following subquery that retrieves specified
state codes in the Central and NorthEast regions:
SELECT MAX(T11.REGION), MAX(T11.STATECODE) FROM wrd_dim_geography T11
WHERE (T11.STATECODE IN('AR', 'IA', 'KS', 'KY', 'WY', 'CT', 'MA', 'NJ',
'NY', 'RI')) AND (T11.REGION IN('Central', 'NorthEast')) GROUP BY
T11.REGION, T11.STATECODE

The retail_subquery.mas Master File follows:


FILENAME=RETAIL_SUBQUERY, SUFFIX=DB2 , $
SEGMENT=RETAIL_SUBQUERY, SEGTYPE=S0, $
FIELDNAME=REGION, ALIAS=E01, USAGE=A15V, ACTUAL=A15V,
MISSING=ON, $
FIELDNAME=STATECODE, ALIAS=E02, USAGE=A2, ACTUAL=A2,
MISSING=ON, $

The retail_subquery.acx Access File follows:


SEGNAME=RETAIL_SUBQUERY, CONNECTION=CON1, DATASET=RETAIL_SUBQUERY.SQL, $

Note: You can create an SQL subquery file, along with a corresponding synonym, using the
HOLD FORMAT SQL_SCRIPT command. For more information, see the Creating Reports With
WebFOCUS Language manual.

The following request uses the DB_INFILE function to compare region names and state codes
against the names retrieved by the subquery:
TABLE FILE WF_RETAIL
SUM REVENUE
BY REGION
BY STATECODE
WHERE DB_INFILE(RETAIL_SUBQUERY, REGION, REGION, STATECODE, STATECODE)
ON TABLE SET PAGE NOPAGE
END

Using Functions 313


DB_INFILE: Testing Values Against a File or an SQL Subquery

The trace shows that the subquery was inserted into the WHERE predicate in the generated
SQL:
SELECT
T11."REGION",
T11."STATECODE",
SUM(T1."Revenue")
FROM
wrd_fact_sales T1,
wrd_dim_customer T5,
wrd_dim_geography T11
WHERE
(T5."ID_CUSTOMER" = T1."ID_CUSTOMER") AND
(T11."ID_GEOGRAPHY" = T5."ID_GEOGRAPHY") AND
((T11."REGION", T11."STATECODE") IN (SELECT MAX(T11.REGION),
MAX(T11.STATECODE) FROM wrd_dim_geography T11 WHERE
(T11.STATECODE IN('AR', 'IA', 'KS', 'KY', 'WY', 'CT', 'MA',
'NJ', 'NY', 'RI')) AND (T11.REGION IN('Central', 'NorthEast'))
GROUP BY T11.REGION, T11.STATECODE))
GROUP BY
T11."REGION",
T11."STATECODE "
ORDER BY
T11."REGION",
T11."STATECODE "
FOR FETCH ONLY;
END

314
10. Data Source and Decoding Functions

The output is:

Example: Comparing Source and Target Values Using a Sequential File


The empvalues.ftm sequential file contains the last and first names of employees in the MIS
department:
SMITH MARY JONES DIANE MCCOY
JOHN BLACKWOOD ROSEMARIE GREENSPAN MARY
CROSS BARBARA

The empvalues.mas Master File describes the data in the empvalues.ftm file
FILENAME=EMPVALUES, SUFFIX=FIX , IOTYPE=BINARY, $
SEGMENT=EMPVALUE, SEGTYPE=S0, $
FIELDNAME=LN, ALIAS=E01, USAGE=A15, ACTUAL=A16, $
FIELDNAME=FN, ALIAS=E02, USAGE=A10, ACTUAL=A12, $

Note: You can create a sequential file, along with a corresponding synonym, using the HOLD
FORMAT SQL_SCRIPT command. For more information, see the Creating Reports With
WebFOCUS Language manual.

Using Functions 315


DB_INFILE: Testing Values Against a File or an SQL Subquery

The following request against the FOCUS EMPLOYEE data source uses the DB_INFILE function
to compare employee names against the names stored in the empvalues.ftm file:

FILEDEF EMPVALUES DISK baseapp/empvalues.ftm


TABLE FILE EMPLOYEE
SUM CURR_SAL
BY LAST_NAME BY FIRST_NAME
WHERE DB_INFILE(EMPVALUES, LAST_NAME, LN, FIRST_NAME, FN)
ON TABLE SET PAGE NOPAGE
END

The output is:

Syntax: How to Control DB_INFILE Optimization


To control whether to prevent optimization of the DB_INFILE expression, issue the following
command:

SET DB_INFILE = {DEFAULT|EXPAND_ALWAYS|EXPAND_NEVER}

In a TABLE request, issue the following command:

ON TABLE SET DB_INFILE {DEFAULT|EXPAND_ALWAYS|EXPAND_NEVER}

where:
DEFAULT

Enables DB_INFILE to create a subquery if its analysis determines that it is possible. This
is the default value.
EXPAND_ALWAYS

Prevents DB_INFILE from creating a subquery. Instead, it expands the expression into IF
and WHERE clauses in memory.

316
10. Data Source and Decoding Functions

EXPAND_NEVER

Prevents DB_INFILE from expanding the expression into IF and WHERE clauses in memory.
Instead, it attempts to create a subquery. If this is not possible, a FOC32585 message is
generated and processing halts.

DB_LOOKUP: Retrieving Data Source Values


Available Languages: reporting, MODIFY

You can use the DB_LOOKUP function to retrieve a value from one data source when running a
request against another data source, without joining or combining the two data sources.

DB_LOOKUP compares pairs of fields from the source and lookup data sources to locate
matching records and retrieve the value to return to the request. You can specify as many
pairs as needed to get to the lookup record that has the value you want to retrieve. If your field
list pairs do not lead to a unique lookup record, the first matching lookup record retrieved is
used.

DB_LOOKUP can be called in a DEFINE command, TABLE COMPUTE command, MODIFY


COMPUTE command, or DataMigrator flow.

There are no restrictions on the source file. The lookup file can be any non-FOCUS data source
that is supported as the cross referenced file in a cluster join. The lookup fields used to find
the matching record are subject to the rules regarding cross-referenced join fields for the
lookup data source. A fixed format sequential file can be the lookup file if it is sorted in the
same order as the source file.

Syntax: How to Retrieve a Value From a Lookup Data Source

DB_LOOKUP(look_mf, srcfld1, lookfld1, srcfld2, lookfld2, ..., returnfld);

where:
look_mf
Is the lookup Master File.
srcfld1, srcfld2 ...
Are fields from the source file used to locate a matching record in the lookup file.
lookfld1, lookfld2 ...
Are columns from the lookup file that share values with the source fields. Only columns in
the table or file can be used; columns created with DEFINE cannot be used. For multi-
segment synonyms, only columns in the top segment can be used.

Using Functions 317


DB_LOOKUP: Retrieving Data Source Values

returnfld
Is the name of a column in the lookup file whose value is returned from the matching
lookup record. Only columns in the table or file can be used; columns created with DEFINE
cannot be used.

Reference: Usage Notes for DB_LOOKUP


The maximum number of pairs that can be used to match records is 63.

If the lookup file is a fixed format sequential file, it must be sorted and retrieved in the
same order as the source file, unless the ENGINE INT SET CACHE=ON command is in
effect. Having this setting in effect may also improve performance if the values will be
looked up more than once. The key field of the sequential file must be the first lookup field
specified in the DB_LOOKUP request. If it is not, no records will match.

In addition, if a DB_LOOKUP request against a sequential file is issued in a DEFINE FILE


command, you must clear the DEFINE FILE command at the end of the TABLE request that
references it, or the lookup file will remain open. It will not be reusable until closed and
may cause problems when you exit. Other types of lookup files can be reused without
clearing the DEFINE. They will be cleared automatically when all DEFINE fields are cleared.

If the lookup field has the MISSING=ON attribute in its Master File and the DEFINE or
COMPUTE command specifies MISSING ON, the missing value is returned when the lookup
field is missing. Without MISSING ON in both places, the missing value is converted to a
default value (blank for an alphanumeric field, zero for a numeric field).

Source records display on the report output even if they lack a matching record in the
lookup file.

Only real fields in the lookup Master File are valid as lookup and return fields.

If there are multiple rows in the lookup table where the source field is equal to the lookup
field, the first value of the return field is returned.

318
10. Data Source and Decoding Functions

Example: Retrieving a Value From a Fixed Format Sequential File in a TABLE Request
The following procedure creates a fixed format sequential file named GSALE from the GGSALES
data source. The fields in this file are PRODUCT (product description), CATEGORY (product
category), and PCD (product code). The file is sorted on the PCD field:

SET ASNAMES = ON
TABLE FILE GGSALES
SUM PRODUCT CATEGORY
BY PCD
ON TABLE HOLD AS GSALE FORMAT ALPHA
END

The following Master File is generated as a result of the HOLD command:

FILENAME=GSALE, SUFFIX=FIX , $
SEGMENT=GSALE, SEGTYPE=S1, $
FIELDNAME=PCD, ALIAS=E01, USAGE=A04, ACTUAL=A04, $
FIELDNAME=PRODUCT, ALIAS=E02, USAGE=A16, ACTUAL=A16, $
FIELDNAME=CATEGORY, ALIAS=E03, USAGE=A11, ACTUAL=A11, $

The following TABLE request against the GGPRODS data source, sorts the report on the field
that matches the key field in the lookup file. It retrieves the value of the CATEGORY field from
the GSALE lookup file by matching on the product code and product description fields. Note
that the DEFINE FILE command is cleared at the end of the request:

DEFINE FILE GGPRODS


PCAT/A11 MISSING ON = DB_LOOKUP(GSALE, PRODUCT_ID, PCD,
PRODUCT_DESCRIPTION, PRODUCT, CATEGORY);
END
TABLE FILE GGPRODS
PRINT PRODUCT_DESCRIPTION PCAT
BY PRODUCT_ID
END
DEFINE FILE GGPRODS CLEAR
END

Because the GSALE Master File does not define the CATEGORY field with the MISSING=ON
attribute, the PCAT column displays a blank in those rows that have no matching record in the
lookup file:

Product Product PCAT


Code
------- ------- ----
B141 Hazelnut
B142 French Roast
B144 Kona

Using Functions 319


DECODE: Decoding Values

F101 Scone Food


F102 Biscotti Food
F103 Croissant Food
G100 Mug Gifts
G104 Thermos Gifts
G110 Coffee Grinder Gifts
G121 Coffee Pot Gifts

If you add the MISSING=ON attribute to the CATEGORY field in the GSALE Master File, the
PCAT column displays a missing data symbol in rows that do not have a matching record in the
lookup file:
Product
Code Product PCAT
------- ------- ----
B141 Hazelnut .
B142 French Roast .
B144 Kona .
F101 Scone Food
F102 Biscotti Food
F103 Croissant Food
G100 Mug Gifts
G104 Thermos Gifts
G110 Coffee Grinder Gifts
G121 Coffee Pot Gifts

DECODE: Decoding Values


Available Languages: reporting, Maintain

The DECODE function assigns values based on the coded value of an input field. DECODE is
useful for giving a more meaningful value to a coded value in a field. For example, the field
GENDER may have the code F for female employees and M for male employees for efficient
storage (for example, one character instead of six for female). DECODE expands (decodes)
these values to ensure correct interpretation on a report.

You can use DECODE by supplying values directly in the function or by reading values from a
separate file.

The use of DECODE with Maintain is limited. For information on decoding values with
subscripted stack values, see SELECTS: Decoding a Value From a Stack on page 292.

320
10. Data Source and Decoding Functions

Syntax: How to Supply Values in the Function

DECODE fieldname(code1 result1 code2 result2...[ELSE default ]);


DECODE fieldname(filename ...[ELSE default]);

where:
fieldname
Alphanumeric or Numeric

Is the name of the input field.


code
Alphanumeric or Numeric

Is the coded value that DECODE compares with the current value of fieldname. If the value
has embedded blanks, commas, or other special characters, it must be enclosed in single
quotation marks. When DECODE finds the specified value, it returns the corresponding
result. When the code is compared to the value of the field name, the code and field name
must be in the same format.
result
Alphanumeric or Numeric

Is the returned value that corresponds to the code. If the result has embedded blanks or
commas, or contains a negative number, it must be enclosed in single quotation marks.
Do not use double quotation marks (").

If the result is presented in alphanumeric format, it must be a non-null, non-blank string.


The format of the result must correspond to the data type of the expression.
default
Alphanumeric or Numeric

Is the value returned as a result for non-matching codes. The format must be the same as
the format of result. If you omit a default value, DECODE assigns a blank or zero to non-
matching codes.
filename
Alphanumeric

Is the name of the file in which code/result pairs are stored. Every record in the file must
contain a pair.

You can use up to 40 lines to define the code and result pairs for any given DECODE function,
or 39 lines if you also use an ELSE phrase. Use either a comma or blank to separate the code
from the result, or one pair from another.

Using Functions 321


DECODE: Decoding Values

Note: DECODE has no output argument.

Example: Supplying Values Using the DECODE Function


EDIT extracts the first character of the CURR_JOBCODE field, then DECODE returns either
ADMINISTRATIVE or DATA PROCESSING depending on the value extracted.

TABLE FILE EMPLOYEE


PRINT CURR_JOBCODE AND COMPUTE
DEPX_CODE/A1 = EDIT(CURR_JOBCODE, '9$$'); NOPRINT AND COMPUTE
JOB_CATEGORY/A15 = DECODE DEPX_CODE(A 'ADMINISTRATIVE'
B 'DATA PROCESSING');
BY LAST_NAME
WHERE DEPARTMENT EQ 'MIS';
END

The output is:


LAST_NAME CURR_JOBCODE JOB_CATEGORY
--------- ------------ ------------
BLACKWOOD B04 DATA PROCESSING
CROSS A17 ADMINISTRATIVE
GREENSPAN A07 ADMINISTRATIVE
JONES B03 DATA PROCESSING
MCCOY B02 DATA PROCESSING
SMITH B14 DATA PROCESSING

Reference: Guidelines for Reading Values From a File


Each record in the file is expected to contain pairs of elements separated by a comma or
blank.

If each record in the file consists of only one element, this element is interpreted as the
code, and the result becomes either a blank or zero, as needed.

This makes it possible to use the file to hold screening literals referenced in the screening
condition:

IF field IS (filename)

and as a file of literals for an IF criteria specified in a computational expression. For


example:

TAKE = DECODE SELECT (filename ELSE 1);


VALUE = IF TAKE IS 0 THEN... ELSE...;

TAKE is 0 for SELECT values found in the literal file and 1 in all other cases. The VALUE
computation is carried out as if the expression had been:

IF SELECT (filename) THEN... ELSE...;

322
10. Data Source and Decoding Functions

The file can contain up to 32,767 characters in the file.

All data is interpreted in ASCII format on UNIX and Windows, or in EBCDIC format on z/OS,
and converted to the USAGE format of the DECODE pairs.

Leading and trailing blanks are ignored.

The remainder of each record is ignored and can be used for comments or other data. This
convention applies in all cases, except when the file name is HOLD. In that case, the file is
presumed to have been created by the HOLD command, which writes fields in the internal
format, and the DECODE pairs are interpreted accordingly. In this case, extraneous data in
the record is ignored.

Example: Reading DECODE Values From a File


The following example has two parts. The first part creates a file with a list of IDs and reads
the EDUCFILE data source. The second part reads the EMPLOYEE data source and assigns 0
to those employees who have taken classes and 1 to those employees who have not. The
HOLD file contains only one column of values. Therefore, DECODE assigns the value 0 to an
employee whose EMP_ID appears in the file and 1 when EMP_ID does not appear in the file.
TABLE FILE EDUCFILE
PRINT EMP_ID
ON TABLE HOLD
END

TABLE FILE EMPLOYEE


PRINT EMP_ID AND LAST_NAME AND FIRST_NAME AND COMPUTE
NOT_IN_LIST/I1 = DECODE EMP_ID(HOLD ELSE 1);
WHERE DEPARTMENT EQ 'MIS';
END

The output is:


EMP_ID LAST_NAME FIRST_NAME NOT_IN_LIST
------ --------- ---------- -----------
112847612 SMITH MARY 0
117593129 JONES DIANE 0
219984371 MCCOY JOHN 1
326179357 BLACKWOOD ROSEMARIE 0
543729165 GREENSPAN MARY 1
818692173 CROSS BARBARA 0

Using Functions 323


FIND: Verifying the Existence of a Value in a Data Source

FIND: Verifying the Existence of a Value in a Data Source


Available Languages: MODIFY, Maintain

The FIND function determines if a data value is in a data source field being searched. The
function sets a temporary field to 1 (a non-zero value for MODIFY) if the data value is found in
the data source field, and to 0 if it is not. FIND does not change the searched file's current
database position. A value greater than zero confirms the presence of the data value, not the
number of instances in the data source field.

You can also use FIND in a VALIDATE command to determine if a transaction field value exists
in another FOCUS data source. If the field value is not in that data source, the function returns
a value of 0, causing the validation test to fail and the request to reject the transaction.

You can use any number of FINDs in a COMPUTE or VALIDATE command. However, more FINDs
increase processing time and require more buffer space in memory.

Limit: FIND does not work on files with different DBA passwords.

The opposite of FIND is NOT FIND. The NOT FIND function sets a temporary field to 1 if the
incoming value is not in the data source and to 0 if the incoming value is in the data source.

Syntax: How to Verify the Existence of a Value in a Data Source

FIND(fieldname [AS dbfield] IN file);

where:
fieldname
Is the name of the field that contains the incoming data value.
AS dbfield
Is the name of the data source field whose values are compared to the incoming field
values.

For Maintain - the AS field is required and the name must be qualified.
file
Is the name of the FOCUS data source.

For Maintain - the IN file is unnecessary since the AS field name is required and must be
qualified.
Note:

FIND does not use an output argument.

324
10. Data Source and Decoding Functions

Do not include a space between FIND and the left parenthesis.

Example: Verifying the Existence of a Value in Another Data Source (Maintain)


In the following example, FIND determines if a data value is found in another data source.
MAINTAIN FILE MOVIES AND VIDEOTRK
FOR ALL NEXT MOVIES.MOVIECODE INTO FILMSTK
TYPE "RC SHOULD BE 1 WHERE MOVIECODE EXISTS IN BOTH FILES";
TYPE " "
COMPUTE RC/I1;
COMPUTE I/I1=1;
REPEAT FILMSTK.FOCCOUNT
COMPUTE RC= FIND(FILMSTK(I).MOVIECODE AS VIDEOTRK.MOVIECODE)
TYPE "FOR MOVIECODE = <<FILMSTK(I).MOVIECODE , RC = <<RC"
COMPUTE I=I+1;
ENDREPEAT
END

The output is:


RC SHOULD BE 1 WHERE MOVIECODE EXISTS IN BOTH FILES
FOR MOVIECODE = 001MCA, RC = 1
.
.
.
FOR MOVIECODE = 387PLA, RC = 0
.
.
.
FOR MOVIECODE = 963CBS, RC = 1
TRANSACTIONS: COMMITS = 1 ROLLBACKS = 0
SEGMENTS : INCLUDED = 0 UPDATED = 0 DELETED = 0

Example: Verifying the Existence of a Value in the Same Data Source (Maintain)
In the following example, FIND determines if a data value is found in the same data source.
MAINTAIN FILE CAR
COMPUTE RETAIL_COST=31500;
COMPUTE CHECK/I1;
COMPUTE CHECK= FIND (RETAIL_COST);
IF CHECK = 1 THEN GOTO FOUND1
ELSE GOTO NOT1;
CASE FOUND1
TYPE "THERE IS A CAR WITH A RETAIL_COST OF <<RETAIL_COST"
-* ....
ENDCASE
CASE NOT1
TYPE "THERE IS NO CAR WITH A RETAIL_COST OF <<RETAIL_COST"
-*....
ENDCASE
-*....
END

Using Functions 325


IMPUTE: Replacing Missing Values With Aggregated Values

The output is:


THERE IS A CAR WITH A RETAIL_COST OF 31,500
TRANSACTIONS: COMMITS = 1 ROLLBACKS = 0
SEGMENTS : INCLUDED = 0 UPDATED = 0 DELETED = 0

IMPUTE: Replacing Missing Values With Aggregated Values


IMPUTE calculates a value to replace missing numeric data on report output, within a partition.

In place of eliminating data records with missing values from analysis, IMPUTE enables you to
substitute a variety of estimates for the missing values, including the mean, the median, the
mode, or a numeric constant, all calculated within the data partition specified by the reset key.
This function is designed to be used with detail level reports (PRINT or LIST commands), and
with calculated values (fields created with the COMPUTE command).

Syntax: How to Replace Missing Values With Aggregated Values

IMPUTE(field, reset_key, replacement)

where:
field
Is the name of the numeric input field that is defined with MISSING ON.
reset_key
Defines the partition for the calculation. Valid values are:

A sort field name.

PRESET, which uses the break defined by the SET PARTITION_ON command.

TABLE, which performs the calculation on the entire table.


replacement
Is a numeric constant or one of the following:

MEAN

MEDIAN

MODE

326
10. Data Source and Decoding Functions

Example: Replacing Missing Values With Aggregated Values


To run this example, the FOCUS data source SALEMISS must be created. SALEMISS is the
SALES data source with some missing values added in the RETURNS and DAMAGED fields.
The following is the SALEMISS Master File, which should be added to the IBISAMP application.
FILENAME=KSALES, SUFFIX=FOC, REMARKS='Legacy Metadata Sample: sales',$

SEGNAME=STOR_SEG, SEGTYPE=S1,
FIELDNAME=STORE_CODE, ALIAS=SNO, FORMAT=A3, $
FIELDNAME=CITY, ALIAS=CTY, FORMAT=A15, $
FIELDNAME=AREA, ALIAS=LOC, FORMAT=A1, $

SEGNAME=DATE_SEG, PARENT=STOR_SEG, SEGTYPE=SH1,


FIELDNAME=DATE, ALIAS=DTE, FORMAT=A4MD, $

SEGNAME=PRODUCT, PARENT=DATE_SEG, SEGTYPE=S1,


FIELDNAME=PROD_CODE, ALIAS=PCODE, FORMAT=A3, FIELDTYPE=I, $
FIELDNAME=UNIT_SOLD, ALIAS=SOLD, FORMAT=I5, $
FIELDNAME=RETAIL_PRICE, ALIAS=RP, FORMAT=D5.2M, $
FIELDNAME=DELIVER_AMT, ALIAS=SHIP, FORMAT=I5, $
FIELDNAME=OPENING_AMT, ALIAS=INV, FORMAT=I5, $
FIELDNAME=RETURNS, ALIAS=RTN, FORMAT=I3, MISSING=ON, $
FIELDNAME=DAMAGED, ALIAS=BAD, FORMAT=I3, MISSING=ON, $

Using Functions 327


IMPUTE: Replacing Missing Values With Aggregated Values

The following procedure creates the SALEMISS data source and then adds the missing values
to the RETURNS and DAMAGED fields:
CREATE FILE ibisamp/SALEMISS
MODIFY FILE ibisamp/SALEMISS
FIXFORM STORE_CODE/3 CITY/15 AREA/1 DATE/4 PROD_CODE/3
FIXFORM UNIT_SOLD/5 RETAIL_PRICE/5 DELIVER_AMT/5
FIXFORM OPENING_AMT/5 RETURNS/3 DAMAGED/3
MATCH STORE_CODE
ON NOMATCH INCLUDE
ON MATCH CONTINUE
MATCH DATE
ON NOMATCH INCLUDE
ON MATCH CONTINUE
MATCH PROD_CODE
ON NOMATCH INCLUDE
ON MATCH REJECT
DATA
14BSTAMFORD S1212B10 60 .95 80 65 10 6
14BSTAMFORD S1212B12 40 1.29 20 50 3 3
14BSTAMFORD S1212B17 29 1.89 30 30 2 1
14BSTAMFORD S1212C13 25 1.99 30 40 3 0
14BSTAMFORD S1212C7 45 2.39 50 49 5 4
14BSTAMFORD S1212D12 27 2.19 40 35 0 0
14BSTAMFORD S1212E2 80 .99 100 100 9 4
14BSTAMFORD S1212E3 70 1.09 80 90 8 9
14ZNEW YORK U1017B10 30 .85 30 10 2 3
14ZNEW YORK U1017B17 20 1.89 40 25 2 1
14ZNEW YORK U1017B20 15 1.99 30 5 0 1
14ZNEW YORK U1017C17 12 2.09 10 15 0 0
14ZNEW YORK U1017D12 20 2.09 30 10 3 2
14ZNEW YORK U1017E1 30 .89 25 45 4 7
14ZNEW YORK U1017E3 35 1.09 25 45 4 2
77FUNIONDALE R1018B20 25 2.09 40 25 1 1
77FUNIONDALE R1018C7 40 2.49 40 40 0 0
K1 NEWARK U1019B12 29 1.49 30 30 1 0
K1 NEWARK U1018B10 13 .99 30 15 1 1
END
-RUN

328
10. Data Source and Decoding Functions

MODIFY FILE ibisamp/SALEMISS


FIXFORM STORE_CODE/3 DATE/5 PROD_CODE/4
FIXFORM UNIT/3 RETAIL/5 DELIVER/3
FIXFORM OPEN/3 RETURNS/C3 DAMAGED/C3
MATCH STORE_CODE
ON NOMATCH INCLUDE
ON MATCH CONTINUE
MATCH DATE
ON NOMATCH INCLUDE
ON MATCH CONTINUE
MATCH PROD_CODE
ON NOMATCH INCLUDE
ON MATCH REJECT
DATA
14Z1017 C13 15 1.99 35 30 6
14Z1017 C14 18 2.05 30 25 4
14Z1017 E2 33 0.99 45 40
END
-RUN

The following request against the SALEMISS data source generates replacement values for the
missing values in the RETURNS field, using only the values within the same store.
SET PARTITION_ON=FIRST
TABLE FILE SALEMISS
PRINT RETURNS
COMPUTE MEDIAN1 = IMPUTE(RETURNS, PRESET, MEDIAN);
COMPUTE MEAN1 = IMPUTE(RETURNS, PRESET, MEAN);
COMPUTE MODE1 = IMPUTE(RETURNS, PRESET, MODE);
BY STORE_CODE
ON TABLE SET PAGE NOPAGE
ON TABLE SET STYLE *
TYPE=REPORT, GRID=OFF,$
ENDSTYLE
END

Using Functions 329


IMPUTE: Replacing Missing Values With Aggregated Values

The output is shown in the following image. The missing values occur in store 14Z, and the
replacement values are calculated using only the RETURNS values from that store because
PARTITION_ON is set to FIRST.

330
10. Data Source and Decoding Functions

Changing the PARTITION_ON setting to TABLE produces the following output, in which the
replacement values are calculated using all of the rows in the table.

LAST: Retrieving the Preceding Value


Available Languages: reporting

The LAST function retrieves the preceding value for a field.

The effect of LAST depends on whether it appears in a DEFINE or COMPUTE command:

In a DEFINE command, the LAST value applies to the previous record retrieved from the
data source before sorting takes place.

Using Functions 331


LAST: Retrieving the Preceding Value

In a COMPUTE command, the LAST value applies to the record in the previous line of the
internal matrix.

Do not use LAST with the -SET command in Dialogue Manager.

Syntax: How to Retrieve the Preceding Value

LAST fieldname

where:
fieldname
Alphanumeric or Numeric

Is the field name.

Note: LAST does not use an output argument.

Example: Retrieving the Preceding Value


LAST retrieves the previous value of the DEPARTMENT field to determine whether to restart the
running total of salaries by department. If the previous value equals the current value,
CURR_SAL is added to RUN_TOT to generate a running total of salaries within each
department.
TABLE FILE EMPLOYEE
PRINT LAST_NAME CURR_SAL AND COMPUTE
RUN_TOT/D12.2M = IF DEPARTMENT EQ LAST DEPARTMENT THEN
(RUN_TOT + CURR_SAL) ELSE CURR_SAL ;
AS 'RUNNING,TOTAL,SALARY'
BY DEPARTMENT SKIP-LINE
END

The output is:


RUNNING
TOTAL
DEPARTMENT LAST_NAME CURR_SAL SALARY
---------- --------- -------- -------
MIS SMITH $13,200.00 $13,200.00
JONES $18,480.00 $31,680.00
MCCOY $18,480.00 $50,160.00
BLACKWOOD $21,780.00 $71,940.00
GREENSPAN $9,000.00 $80,940.00
CROSS $27,062.00 $108,002.00
PRODUCTION STEVENS $11,000.00 $11,000.00
SMITH $9,500.00 $20,500.00
BANNING $29,700.00 $50,200.00
IRVING $26,862.00 $77,062.00
ROMANS $21,120.00 $98,182.00
MCKNIGHT $16,100.00 $114,282.00

332
10. Data Source and Decoding Functions

LOOKUP: Retrieving a Value From a Cross-referenced Data Source


Available Languages: MODIFY

The LOOKUP function retrieves a data value from a cross-referenced FOCUS data source in a
MODIFY request. You can retrieve data from a data source cross-referenced statically in a
Master File or a data source joined dynamically to another by the JOIN command. LOOKUP
retrieves a value, but does not activate the field. LOOKUP is required because a MODIFY
request, unlike a TABLE request, cannot read cross-referenced data sources freely.

LOOKUP allows a request to use the retrieved data in a computation or message, but it does
not allow you to modify a cross-referenced data source.

To modify more than one data source in one request, use the COMBINE command or the
Maintain Data facility.

LOOKUP can read a cross-referenced segment that is linked directly to a segment in the host
data source (the host segment). This means that the cross-referenced segment must have a
segment type of KU, KM, DKU, or DKM (but not KL or KLU) or must contain the cross-
referenced field specified by the JOIN command. Because LOOKUP retrieves a single cross-
referenced value, it is best used with unique cross-referenced segments.

The cross-referenced segment contains two fields used by LOOKUP:

The field containing the retrieved value. Alternatively, you can retrieve all the fields in a
segment at one time. The field, or your decision to retrieve all the fields, is specified in
LOOKUP.

For example, LOOKUP retrieves all the fields from the segment
RTN = LOOKUP(SEG.DATE_ATTEND);

The cross-referenced field. This field shares values with a field in the host segment called
the host field. These two fields link the host segment to the cross-referenced segment.
LOOKUP uses the cross-referenced field, which is indexed, to locate a specific segment
instance.

When using LOOKUP, the MODIFY request reads a transaction value for the host field. It then
searches the cross-referenced segment for an instance containing this value in the cross-
referenced field:

If there are no instances of the value, the function sets a return variable to 0. If you use
the field specified by LOOKUP in the request, the field assumes a value of blank if
alphanumeric and 0 if numeric.

Using Functions 333


LOOKUP: Retrieving a Value From a Cross-referenced Data Source

If there are instances of the value, the function sets the return variable to 1 and retrieves
the value of the specified field from the first instance it finds. There can be more than one
if the cross-referenced segment type is KM or DKM, or if you specified the ALL keyword in
the JOIN command.

Syntax: How to Retrieve a Value From a Cross-referenced Data Source

LOOKUP(field);

where:
field
Is the name of the field to retrieve in the cross-referenced file. If the field name also exists
in the host data source, you must qualify it here. Do not include a space between LOOKUP
and the left parenthesis.

Note: LOOKUP does not use an output argument.

Example: Using a Value in a Host Segment to Search a Data Source


You can use a field value in a host segment instance to search a cross-referenced segment.
Do the following:

In the MATCH command that selects the host segment instance, activate the host field
with the ACTIVATE command.

In the same MATCH command, code LOOKUP after the ACTIVATE command.

This request displays the employee ID, date of salary increase, employee name, and the
employee position after the raise was granted:

The employee ID and name (EMP_ID) are in the root segment.

The date of increase (DAT_INC) is in the descendant host segment.

The job position is in the cross-referenced segment.

The shared field is JOBCODE. You never enter a job code; the values are stored in the data
source.

334
10. Data Source and Decoding Functions

The request is:


MODIFY FILE EMPLOYEE
PROMPT EMP_ID DAT_INC
MATCH EMP_ID
ON NOMATCH REJECT
ON MATCH CONTINUE
MATCH DAT_INC
ON NOMATCH REJECT
ON MATCH ACTIVATE JOBCODE
ON MATCH COMPUTE
RTN = LOOKUP(JOB_DESC);
ON MATCH TYPE
"EMPLOYEE ID: <EMP_ID"
"DATE INCREASE: <DAT_INC"
"NAME: <D.FIRST_NAME <D.LAST_NAME"
"POSITION: <JOB_DESC"
DATA

A sample execution is:

1. The request prompts you for the employee ID and date of pay increase. Enter the employee
ID 071382660 and the date 820101 (January 1, 1982).
2. The request locates the instance containing the ID 071382660, then locates the child
instance containing the date of increase 820101.
3. This child instance contains the job code A07. The ACTIVATE command makes this value
available to LOOKUP.
4. LOOKUP locates the job code A07 in the cross-referenced segment. It returns a 1 the RTN
variable and retrieves the corresponding job description SECRETARY.
5. The TYPE command displays the values:
EMPLOYEE ID: 071382660
DATE INCREASE: 82/01/01
NAME: ALFRED STEVENS
POSITION: SECRETARY

Fields retrieved by LOOKUP do not require the D. prefix. FOCUS treats the field values as
transaction values.

Using Functions 335


LOOKUP: Retrieving a Value From a Cross-referenced Data Source

You may also need to activate the host field if you are using LOOKUP within a NEXT command.
This request displays the latest position held by an employee:

MODIFY FILE EMPLOYEE


PROMPT EMP_ID
MATCH EMP_ID
ON NOMATCH REJECT
ON MATCH CONTINUE
NEXT DAT_INC
ON NONEXT REJECT
ON NEXT ACTIVATE JOBCODE
ON NEXT COMPUTE
RTN = LOOKUP(JOB_DESC);
ON MATCH TYPE
"EMPLOYEE ID: <EMP_ID"
"DATE OF POSITION: <DAT_INC"
"NAME: <D.FIRST_NAME <D.LAST_NAME"
"POSITION: <JOB_DESC"
DATA

Example: Using the LOOKUP Function With a VALIDATE Command


When you use LOOKUP, reject transactions containing values for which there is no
corresponding instance in the cross-reference segment. To do this, place the function in a
VALIDATE command. If the function cannot locate the instance in the cross-referenced
segment, it sets the value of the return variable to 0, causing the request to reject the
transaction.

The following request updates an employee's classroom hours (ED_HRS). If the employee
enrolled in classes on or after January 1, 1982, the request increases the number of
classroom hours by 10%. The enrollment dates are stored in a cross-referenced segment (field
DATE_ATTEND). The shared field is the employee ID.

The request is as follows:

MODIFY FILE EMPLOYEE


PROMPT EMP_ID ED_HRS
VALIDATE
TEST_DATE = LOOKUP(DATE_ENROLL);
COMPUTE
ED_HRS = IF DATE_ENROLL GE 820101 THEN ED_HRS * 1.1
ELSE ED_HRS;
MATCH EMP_ID
ON MATCH UPDATE ED_HRS
ON NOMATCH REJECT
DATA

If an employee record is not found in the cross-referenced segment, that employee never
enrolled in a class. The transaction is rejected as an error.

336
10. Data Source and Decoding Functions

Using the Extended LOOKUP Function


If the LOOKUP function cannot locate a value of the host field in the cross-referenced segment,
use extended syntax to locate the next highest or lowest cross-referenced field value in the
cross-referenced segment.

To use this feature, create the index with the INDEX parameter set to NEW (the binary tree
scheme). To determine the type of index used by a data source, enter the FDT command.

Syntax: How to Use the Extended LOOKUP Function

COMPUTE
LOOKUP(field action);

where:
field
Is the name of the field in the cross-referenced data source, used in a MODIFY
computation. If the field name also exists in the host data source, you must qualify it here.
action
Specifies the action the request takes. Valid values are:

EQ causes LOOKUP to take no further action if an exact match is not found. If a match is
found, the value of rcode is set to 1; otherwise, it is set to 0. This is the default.

GE causes LOOKUP to locate the instance with the next highest value of the cross-
referenced field. The value of rcode is set to 2.

LE causes LOOKUP to locate the instance with the next lowest value of the cross-
referenced field. The value of rcode is set to -2.

Do not include a space between LOOKUP and the left parenthesis.

The following table shows the value of rcode, depending on which instance LOOKUP locates:

Value Action

1 Exact cross-referenced value located.

2 Next highest cross-referenced value located.

-2 Next lowest cross-referenced value located.

0 Cross-referenced value not located.

Using Functions 337


NULLIF: Returning a Null Value When Parameters Are Equal

NULLIF: Returning a Null Value When Parameters Are Equal


NULLIF returns a null (missing) value when its parameters are equal. If they are not equal, it
returns the first value. The field to which the value is returned should have MISSING ON.

Syntax: How to Return a Null Value for Equal Parameters

NULLIF(arg1,arg2)

where:
arg1,arg2
Any type of field, constant, or expression.

Are the input parameters that are tested for equality. They must either both be numeric or
both be alphanumeric.

The output data type is the same as the input data types.

Example: Testing for Equal Parameters


The following request uses NULLIF to test the DAMAGED and RETURNS field values for
equality.
DEFINE FILE SALES
NULL1/I4 MISSING ON = NULLIF(DAMAGED, RETURNS);
END
TABLE FILE SALES
PRINT DAMAGED RETURNS NULL1
BY STORE_CODE
ON TABLE SET PAGE NOLEAD
ON TABLE SET STYLE *
GRID=OFF,$
END

338
10. Data Source and Decoding Functions

The output is shown in the following image.

Using Functions 339


NULLIF: Returning a Null Value When Parameters Are Equal

340
Chapter 11 Simplified Date and Date-Time Functions
Simplified date and date-time functions have streamlined parameter lists, similar to
those used by SQL functions. In some cases, these simplified functions provide slightly
different functionality than previous versions of similar functions.

The simplified functions do not have an output argument. Each function returns a value
that has a specific data type.

When used in a request against a relational data source, these functions are optimized
(passed to the RDBMS for processing).

Standard date and date-time formats refer to YYMD and HYYMD syntax (dates that are
not stored in alphanumeric or numeric fields). Dates not in these formats must be
converted before they can be used in the simplified functions. Input date and date-time
parameters must provide full component dates. Literal date-time values can be used with
the DT function.

All arguments can be either literals, field names, or amper variables.

Note: The simplified date and date-time functions are not supported in Maintain Data.
In this chapter:

DAYNAME: Returning the Name of the Day From a Date Expression

DT_CURRENT_DATE: Returning the Current Date

DT_CURRENT_DATETIME: Returning the Current Date and Time

DT_CURRENT_TIME: Returning the Current Time

DT_TOLOCAL: Converting Universal Coordinated Time to Local Time

DT_TOUTC: Converting Local Time to Universal Coordinated Time

DTADD: Incrementing a Date or Date-Time Component

DTDIFF: Returning the Number of Component Boundaries Between Date or Date-Time


Values

DTIME: Extracting Time Components From a Date-Time Value

DTPART: Returning a Date or Date-Time Component in Integer Format

DTRUNC: Returning the Start of a Date Period for a Given Date

Using Functions 341


DAYNAME: Returning the Name of the Day From a Date Expression

MONTHNAME: Returning the Name of the Month From a Date Expression

DAYNAME: Returning the Name of the Day From a Date Expression


DAYNAME returns a character string that contains the data-source-specific name of the day for
the day part of a date expression.

Syntax: How to Return the Name of the Day From a Date Expression

DAYNAME(date_exp)

where:
date_exp
Is a date or date-time expression.

Example: Returning the Name of the Day From a Date Expression


The following request returns the name of the day from the TIME_DATE field.
TABLE FILE WF_RETAIL_TIME
PRINT TIME_DATE
COMPUTE DAYNAME1/A12 = DAYNAME(TIME_DATE);
WHERE RECORDLIMIT EQ 5
ON TABLE SET PAGE NOLEAD
ON TABLE SET STYLE *
GRID=OFF,$
ENDSTYLE
END

The output is shown in the following image.

342
11. Simplified Date and Date-Time Functions

DT_CURRENT_DATE: Returning the Current Date


The DT_CURRENT_DATE function returns the current date-time provided by the running
operating environment in date-time format. The time portion of the date-time is set to zero.

Syntax: How to Return the Current Date

DT_CURRENT_DATE()

Example: Returning the Current Date


The following request returns the current date.

DEFINE FILE WF_RETAIL_LITE


CURRDATE/YYMD WITH COUNTRY_NAME = DT_CURRENT_DATE();
END
TABLE FILE WF_RETAIL_LITE
SUM CURRDATE
ON TABLE SET PAGE NOPAGE
END

The output is shown in the following image.

DT_CURRENT_DATETIME: Returning the Current Date and Time


DT_CURRENT_DATETIME returns the current date and time provided by the running operating
environment in date-time format, with a specified time precision.

Syntax: How to Return the Current Date and Time

DT_CURRENT_DATETIME(component)

where:
component
Is one of the following time precisions.

SECOND.

MILLISECOND.

MICROSECOND.

Using Functions 343


DT_CURRENT_TIME: Returning the Current Time

Note: The field to which the value is returned must have a format that supports the time
precision requested.

Example: Returning the Current Date and Time


The following request returns the current date and time, with the time specified in
microseconds.

DEFINE FILE WF_RETAIL_LITE


CURRDATE/HYYMDm WITH COUNTRY_NAME = DT_CURRENT_DATETIME(MICROSECOND);
END
TABLE FILE WF_RETAIL_LITE
SUM CURRDATE
ON TABLE SET PAGE NOPAGE
END

The output is shown in the following image.

DT_CURRENT_TIME: Returning the Current Time


The DT_CURRENT_TIME function returns the current time provided by the running operating
environment in date-time format, with a specified time precision. The date portion of the
returned date-time value is set to zero.

Syntax: How to Return the Current Time

DT_CURRENT_TIME(component)

where:
component
Is one of the following time precisions.

SECOND.

MILLISECOND.

MICROSECOND.

Note: The field to which the value is returned must have a format that supports the time
precision requested.

344
11. Simplified Date and Date-Time Functions

Example: Returning the Current Time


The following request returns the current time, with the time precision set to milliseconds.
DEFINE FILE WF_RETAIL_LITE
CURRTIME/HHISs WITH COUNTRY_NAME = DT_CURRENT_TIME(MILLISECOND);
END
TABLE FILE WF_RETAIL_LITE
SUM CURRTIME
ON TABLE SET PAGE NOPAGE
END

The output is shown in the following image.

DT_TOLOCAL: Converting Universal Coordinated Time to Local Time


Coordinated Universal Time (UTC) is the time standard commonly used around the world. To
convert UTC time to a local time, a certain number of hours must be added to or subtracted
from the UTC time, depending on the number of time zones between the locality and
Greenwich, England (GMT).

DT_TOLOCAL converts UTC time to local time.

Converting timestamp values from different localities to a common standard time enables you
to sort events into the actual event sequence.

Using Functions 345


DT_TOLOCAL: Converting Universal Coordinated Time to Local Time

This function requires an IANA (Internet Assigned Numbers Authority) time zone database
names (expressed as 'Area/Location') as a parameter. You can find a table of IANA TZ
database names on Wikipedia at https://en.wikipedia.org/wiki/List_of_tz_database_time_zones,
as shown in the following image.

If you do not know what Area and Location corresponds to your time zone, but you do know
your offset from GMT, or your legacy time zone name (such as EST), scroll down in the table.
There are TZ database names that correspond to these time zone identifiers, as shown in the
following image.

346
11. Simplified Date and Date-Time Functions

Note: If you use a standard IANA time zone database name in the form 'Area/Location' (for
example, 'America/New_York'), automatic adjustments are made for Daylight Savings Time. If
you use a name that corresponds to an offset from GMT or to a legacy time zone name, it is
your responsibility to account for Daylight Savings Time.

Syntax: How to Convert UTC Time to Local Time

DT_TOLOCAL(datetime, timezone)

where:
datetime
Date-time

Is a date-time expression representing UTC time, containing date and time components.
timezone
Alphanumeric

Is a character expression containing the IANA time zone name of the local time, in the
form 'Area/Location' (for example, 'America/New_York').

Example: Converting UTC Time to Local Time


The following request converts the current date-time value from UTC time to local time for time
zone 'America/New_York'.
TABLE FILE GGSALES
SUM DOLLARS NOPRINT
COMPUTE UTC1/HYYMDS = DT_CURRENT_DATETIME(SECOND);
COMPUTE LOCAL1/HYYMDS = DT_TOLOCAL(UTC1, 'America/New_York');
ON TABLE SET PAGE NOLEAD
ON TABLE SET STYLE *
GRID=OFF,$
ENDSTYLE
END

The output is shown in the following image.

DT_TOUTC: Converting Local Time to Universal Coordinated Time


Coordinated Universal Time (UTC) is the time standard commonly used around the world. To
convert UTC time to a local time, a certain number of hours must be added to or subtracted
from the UTC time, depending on the number of time zones between the locality and
Greenwich, England (GMT).

Using Functions 347


DT_TOUTC: Converting Local Time to Universal Coordinated Time

DT_TOUTC converts local time to UTC time.

Converting timestamp values from different localities to a common standard time enables you
to sort events into the actual event sequence.

This function requires an IANA (Internet Assigned Numbers Authority) time zone database
names (expressed as 'Area/Location') as a parameter. You can find a table of IANA TZ
database names on Wikipedia at https://en.wikipedia.org/wiki/List_of_tz_database_time_zones,
as shown in the following image.

348
11. Simplified Date and Date-Time Functions

If you do not know what Area and Location corresponds to your time zone, but you do know
your offset from GMT, or your legacy time zone name (such as EST), scroll down in the table.
There are TZ database names that correspond to these time zone identifiers, as shown in the
following image.

Note: If you use a standard IANA time zone database name in the form 'Area/Location' (for
example, 'America/New_York'), automatic adjustments are made for Daylight Savings Time. If
you use a name that corresponds to an offset from GMT or to a legacy time zone name, it is
your responsibility to account for Daylight Savings Time.

Syntax: How to Convert Local Time to UTC Time

DT_TOUTC(datetime, timezone)

where:
datetime
Date-time

Is a date-time expression representing local time, containing date and time components.
timezone
Alphanumeric

Is a character expression containing the IANA time zone name of the local time, in the
form 'Area/Location' (for example, 'America/New_York').

Using Functions 349


DT_TOUTC: Converting Local Time to Universal Coordinated Time

Example: Converting Local Time to UTC Time


The following request converts the current local date-time value for time zone America/
New_York to UTC time.
TABLE FILE GGSALES
SUM DOLLARS NOPRINT
COMPUTE LOCAL1/HYYMDS = DT_CURRENT_DATETIME(SECOND);
COMPUTE UTC1/HYYMDS = DT_TOUTC(LOCAL1, 'America/New_York');
ON TABLE SET PAGE NOLEAD
ON TABLE SET STYLE *
GRID=OFF,$
ENDSTYLE
END

The output is shown in the following image.

Example: Sorting by UTC Time


The following request retrieves the current date and time into the field LOCALT1 and sets the
field TIMEZONE to IANA time zone database names. It then uses DT_TOUTC to convert the
same local time, with different time zones, to the UTC time that corresponds to the given time
zone, and sorts the output by the generated UTC time.
DEFINE FILE GGSALES
LOCALT1/HYYMDS=DT_CURRENT_DATETIME(SECOND);
TIMEZONE/A30=IF LAST TIMEZONE EQ ' ' THEN 'AMERICA/NEW_YORK'
ELSE IF LAST TIMEZONE EQ 'AMERICA/NEW_YORK' THEN 'AMERICA/CHICAGO'
ELSE IF LAST TIMEZONE EQ 'AMERICA/CHICAGO' THEN 'AMERICA/DENVER'
ELSE IF LAST TIMEZONE EQ 'AMERICA/DENVER' THEN 'ASIA/TOKYO'
ELSE IF LAST TIMEZONE EQ 'ASIA/TOKYO' THEN 'EUROPE/LONDON'
ELSE IF LAST TIMEZONE EQ 'EUROPE/LONDON' THEN 'AMERICA/NEW_YORK';
UTCTIME/HYYMDS=DT_TOUTC(LOCALT1,TIMEZONE);
END
TABLE FILE GGSALES
PRINT TIMEZONE LOCALT1 DOLLARS NOPRINT
BY UTCTIME
WHERE PRODUCT EQ 'Thermos'
IF RECORDLIMIT EQ 20
ON TABLE SET PAGE NOLEAD
ON TABLE SET STYLE *
GRID=OFF,$
ENDSTYLE
END

350
11. Simplified Date and Date-Time Functions

The output is shown in the following image.

DTADD: Incrementing a Date or Date-Time Component


Given a date in standard date or date-time format, DTADD returns a new date after adding the
specified number of a supported component. The returned date format is the same as the
input date format.

Syntax: How to Increment a Date or Date-Time Component

DTADD(date, component, increment)

where:
date
Date or date-time

Is the date or date-time value to be incremented, which must provide a full component
date.

Using Functions 351


DTADD: Incrementing a Date or Date-Time Component

component
Keyword

Is the component to be incremented. Valid components (and acceptable values) are:

YEAR (1-9999).

QUARTER (1-4).

MONTH (1-12).

WEEK (1-53). This is affected by the WEEKFIRST setting.

DAY (of the Month, 1-31).

HOUR (0-23).

MINUTE (0-59).

SECOND (0-59).
increment
Integer

Is the value (positive or negative) to add to the component.

Example: Incrementing the DAY Component of a Date


The following request against the WF_RETAIL data source adds three days to the employee
date of birth:
DEFINE FILE WF_RETAIL
NEWDATE/YYMD = DTADD(DATE_OF_BIRTH, DAY, 3);
MGR/A3 = DIGITS(ID_MANAGER, 3);
END
TABLE FILE WF_RETAIL
SUM MGR NOPRINT DATE_OF_BIRTH NEWDATE
BY MGR
ON TABLE SET PAGE NOPAGE
END

352
11. Simplified Date and Date-Time Functions

The output is:

Reference: Usage Notes for DTADD


Each element must be manipulated separately. Therefore, if you want to add 1 year and 1
day to a date, you need to call the function twice, once for YEAR (you need to take care of
leap years) and once for DAY. The simplified functions can be nested in a single
expression, or created and applied in separate DEFINE or COMPUTE expressions.

With respect to parameter validation, DTADD will not allow anything but a standard date or
a date-time value to be used in the first parameter.

The increment is not checked, and the user should be aware that decimal numbers are not
supported and will be truncated. Any combination of values that increases the YEAR beyond
9999 returns the input date as the value, with no message. If the user receives the input
date when expecting something else, it is possible there was an error.

Using Functions 353


DTDIFF: Returning the Number of Component Boundaries Between Date or Date-Time Values

DTDIFF: Returning the Number of Component Boundaries Between Date or Date-Time


Values
Given two dates in standard date or date-time formats, DTIFF returns the number of given
component boundaries between the two dates. The returned value has integer format for
calendar components or double precision floating point format for time components.

Syntax: How to Return the Number of Component Boundaries

DTDIFF(end_date, start_date, component)

where:
end_date
Date or date-time

Is the ending full-component date in either standard date or date-time format. If this date
is given in standard date format, all time components are assumed to be zero.
start_date
Date or date-time

Is the starting full-component date in either standard date or date-time format. If this date
is given in standard date format, all time components are assumed to be zero.
component
Keyword

Is the component on which the number of boundaries is to be calculated. For example,


QUARTER finds the difference in quarters between two dates. Valid components (and
acceptable values) are:

YEAR (1-9999).

QUARTER (1-4).

MONTH (1-12).

WEEK (1-53). This is affected by the WEEKFIRST setting.

DAY (of the Month, 1-31).

HOUR (0-23).

MINUTE (0-59).

SECOND (0-59).

354
11. Simplified Date and Date-Time Functions

Example: Returning the Number of Years Between Two Dates


The following request against the WF_RETAIL data source calculates employee age when hired:
DEFINE FILE WF_RETAIL
YEARS/I9 = DTDIFF(START_DATE, DATE_OF_BIRTH, YEAR);
END
TABLE FILE WF_RETAIL
PRINT START_DATE DATE_OF_BIRTH YEARS AS 'Hire,Age'
BY EMPLOYEE_NUMBER
WHERE EMPLOYEE_NUMBER CONTAINS 'AA'
ON TABLE SET PAGE NOPAGE
END

The output is:

DTIME: Extracting Time Components From a Date-Time Value


Given a date-time value and time component keyword as input, DTIME returns the value of all
of the time components up to and including the requested component. The remaining time
components in the value are set to zero. The field to which the time component is returned
must have a time format that supports the component being returned.

Syntax: How to Extract a Time Component From a Date-Time Value

DTIME(datetime, component)

Using Functions 355


DTIME: Extracting Time Components From a Date-Time Value

where:
datetime
Date-time

Is the date-time value from which to extract the time component. It can be a field name or
a date-time literal. It must provide a full component date.
component
Keyword

Valid values are:

TIME. The complete time portion is returned. Its smallest component depends on the
input date-time format. Nanoseconds are not supported or returned.

HOUR. The time component up to and including the hour component is extracted.

MINUTE. The time component up to and including the minute component is extracted.

SECOND. The time component up to and including the second component is extracted.

MILLISECOND. The time component up to and including the millisecond component is


extracted.

MICROSECOND. The time component up to and including the microsecond component


is extracted.

Example: Extracting Time Components


The following request defines two date-time fields:

TRANSTIME contains the extracted time components from TRANSDATE down to the minute.

TRANSTIME2 extracts all of the time components from the literal date-time value
2018/01/17 05:45:22.777888.

356
11. Simplified Date and Date-Time Functions

DEFINE FILE VIDEOTR2


TRANSTIME/HHISsm = DTIME(TRANSDATE, MINUTE);
TRANSTIME2/HHISsm = DTIME(DT(2018/01/17 05:45:22.777888), TIME);
END
TABLE FILE VIDEOTR2
SUM TRANSTIME TRANSTIME2
BY MOVIECODE
BY TRANSDATE
WHERE MOVIECODE CONTAINS 'MGM'
ON TABLE SET PAGE NOLEAD
ON TABLE SET STYLE *
GRID=OFF,$
ENDSTYLE
END

The output is shown in the following image.

DTPART: Returning a Date or Date-Time Component in Integer Format


Given a date in standard date or date-time format and a component, DTPART returns the
component value in integer format.

Syntax: How to Return a Date or Date-Time Component in Integer Format

DTPART(date, component)

Using Functions 357


DTPART: Returning a Date or Date-Time Component in Integer Format

where:
date
Date or date-time

Is the full-component date in standard date or date-time format.


component
Keyword

Is the component to extract in integer format. Valid components (and values) are:

YEAR (1-9999).

QUARTER (1-4).

MONTH (1-12).

WEEK (of the year, 1-53). This is affected by the WEEKFIRST setting.

DAY (of the Month, 1-31).

DAY_OF_YEAR (1-366).

WEEKDAY (day of the week, 1-7). This is affected by the WEEKFIRST setting.

HOUR (0-23).

MINUTE (0-59).

SECOND (0-59).

MILLISECOND (0-999).

MICROSECOND (0-999999).

Example: Extracting the Quarter Component as an Integer


The following request against the WF_RETAIL data source extracts the QUARTER component
from the employee start date:

DEFINE FILE WF_RETAIL


QTR/I2 = DTPART(START_DATE, QUARTER);
END
TABLE FILE WF_RETAIL
PRINT START_DATE QTR AS Quarter
BY EMPLOYEE_NUMBER
WHERE EMPLOYEE_NUMBER CONTAINS 'AH'
ON TABLE SET PAGE NOPAGE
END

358
11. Simplified Date and Date-Time Functions

The output is:

DTRUNC: Returning the Start of a Date Period for a Given Date


Given a date or timestamp and a component, DTRUNC returns the first date within the period
specified by that component.

Syntax: How to Return the First or Last Date of a Date Period

DTRUNC(date_or_timestamp, date_period)

where:
date_or_timestamp
Date or date-time

Is the date or timestamp of interest, which must provide a full component date.
date_period
Is the period whose starting or ending date you want to find. Can be one of the following:

DAY, returns the date that represents the input date (truncates the time portion, if
there is one).

YEAR, returns the date of the first day of the year.

MONTH, returns the date of the first day of the month.

QUARTER, returns the date of the first day in the quarter.

WEEK, returns the date that represents the first date of the given week.

By default, the first day of the week will be Sunday, but this can be changed using the
WEEKFIRST parameter.

Using Functions 359


DTRUNC: Returning the Start of a Date Period for a Given Date

YEAR_END, returns the last date of the year.

QUARTER_END, returns the last date of the quarter.

MONTH_END, returns the last date of the month.

WEEK_END, returns the last date of the week.

Example: Returning the First Date in a Date Period


In the following request against the WF_RETAIL data source, DTRUNC returns the first date of
the quarter given the start date of the employee:
DEFINE FILE WF_RETAIL
QTRSTART/YYMD = DTRUNC(START_DATE, QUARTER);
END
TABLE FILE WF_RETAIL
PRINT START_DATE QTRSTART AS 'Start,of Quarter'
BY EMPLOYEE_NUMBER
WHERE EMPLOYEE_NUMBER CONTAINS 'AH'
ON TABLE SET PAGE NOPAGE
END

The output is:

360
11. Simplified Date and Date-Time Functions

Example: Using the Start of Week Parameter for DTRUNC


The following request returns the date that is the start of the week for the start date of certain
employees:
DEFINE FILE WF_RETAIL
DAY1/WT = DTRUNC(START_DATE, DAY);
WKSTART/YYMD = DTRUNC(START_DATE, WEEK);
DAY2/WT = DTRUNC(WKSTART, DAY);
END
TABLE FILE WF_RETAIL
PRINT START_DATE
DAY1 AS 'DOW 1'
WKSTART AS 'Start,of Week'
DAY2 AS 'DOW 2'
BY EMPLOYEE_NUMBER
WHERE START_DATE GT '20130101'
WHERE EMPLOYEE_NUMBER CONTAINS 'AH'
ON TABLE SET PAGE NOPAGE
END

The output is:


Employee Start Start
Number Date DOW 1 of Week DOW 2
-------- ----- ----- ------- -----
AH118 2013/01/15 TUE 2013/01/13 SUN
AH2272 2013/01/17 THU 2013/01/13 SUN
AH288 2013/11/11 MON 2013/11/10 SUN
AH3520 2013/09/23 MON 2013/09/22 SUN
AH3591 2013/09/22 SUN 2013/09/22 SUN
AH5177 2013/07/21 SUN 2013/07/21 SUN

Example: Returning the Date of the First and Last Days of a Week
The following request returns the dates that correspond to the first day of the week and the
last day of the week for the given date.
DEFINE FILE WF_RETAIL
WEEKSTART/YYMD = DTRUNC(START_DATE, WEEK);
WEEKEND/YYMD = DTRUNC(START_DATE, WEEK_END);
END
TABLE FILE WF_RETAIL
PRINT START_DATE WEEKSTART AS 'Start,of Week'
WEEKEND AS 'End,of Week'
BY EMPLOYEE_NUMBER
WHERE EMPLOYEE_NUMBER CONTAINS 'AH1'
ON TABLE SET PAGE NOPAGE
END

Using Functions 361


MONTHNAME: Returning the Name of the Month From a Date Expression

The output is shown in the following image.

MONTHNAME: Returning the Name of the Month From a Date Expression


MONTHNAME returns a character string that contains the data-source-specific name of the
month for the month part of a date expression.

Syntax: How to Return the Name of the Month From a Date Expression

MONTHNAME(date_exp)

where:
date_exp
Is a date or date-time expression.

Example: Returning the Name of the Month From a Date Expression


The following request returns the name of the month from the TRANSDATE field.
TABLE FILE EMPLOYEE
PRINT HIRE_DATE
COMPUTE TRANSDATE/YYMD= HIRE_DATE; NOPRINT
COMPUTE MONTHNAME1/A12 = MONTHNAME(TRANSDATE);
BY TRANSDATE
ON TABLE SET PAGE NOLEAD
ON TABLE SET STYLE *
GRID=OFF,$
ENDSTYLE
END

362
11. Simplified Date and Date-Time Functions

The output is shown in the following image.

Using Functions 363


MONTHNAME: Returning the Name of the Month From a Date Expression

364
Chapter 12 Date Functions
Date functions manipulate date values. There are two types of date functions:

Standard date functions for use with non-legacy dates.

Legacy date functions for use with legacy dates.

If a date is in an alphanumeric or numeric field that contains date display options (for
example, I6YMD), you must use the legacy date functions.
In this chapter:

Overview of Date Functions TODAY: Returning the Current Date

Using Standard Date Functions Using Legacy Date Functions

DATEADD: Adding or Subtracting a Date AYM: Adding or Subtracting Months


Unit to or From a Date
AYMD: Adding or Subtracting Days
DATECVT: Converting the Format of a
CHGDAT: Changing How a Date String
Date
Displays
DATEDIF: Finding the Difference Between
DA Functions: Converting a Legacy Date
Two Dates
to an Integer
DATEMOV: Moving a Date to a
DMY, MDY, YMD: Calculating the
Significant Point
Difference Between Two Dates
DATETRAN: Formatting Dates in
DOWK and DOWKL: Finding the Day of
International Formats
the Week
DPART: Extracting a Component From a
DT Functions: Converting an Integer to a
Date
Date
FIQTR: Obtaining the Financial Quarter
GREGDT: Converting From Julian to
FIYR: Obtaining the Financial Year Gregorian Format

FIYYQ: Converting a Calendar Date to a JULDAT: Converting From Gregorian to


Financial Date Julian Format

YM: Calculating Elapsed Months

Using Functions 365


Overview of Date Functions

Overview of Date Functions


The following explains the difference between the types of date functions:

Standard date functions are for use with standard date formats, or just date formats. A
date format refers to internally stored data that is capable of holding date components,
such as century, year, quarter, month, and day. It does not include time components. A
synonym does not specify an internal data type or length for a date format. Instead, it
specifies display date components, such as D (day), M (month), Q (quarter), Y (2-digit year),
or YY (4-digit year). For example, format MDYY is a date format that has three date
components; it can be used in the USAGE attribute of a synonym. A real date value, such
as March 9, 2004, described by this format is displayed as 03/09/2004, by default. Date
formats can be full component and non-full component. Full component formats include all
three letters, for example, D, M, and Y. JUL for Julian can also be included. All other date
formats are non-full component. Some date functions require full component arguments for
date fields, while others will accept full or non-full components. A date format was formerly
called a smart date.

Legacy date functions are for use with legacy dates only. A legacy date refers to formats
with date edit options, such as I6YMD, A6MDY, I8YYMD, or A8MDYY. For example, A6MDY
is a 6-byte alphanumeric string. The suffix MDY indicates the order in which the date
components are stored in the field, and the prefix I or A indicates a numeric or
alphanumeric form of representation. For example, a value '030599' can be assigned to a
field with format A6MDY, which will be displayed as 03/05/99.

Date formats have an internal representation matching either numeric or alphanumeric format.
For example, A6MDY matches alphanumeric format, YYMD and I6DMY match numeric format.
When function output is a date in specified by output, it can be used either for assignment to
another date field of this format, or it can be used for further data manipulation in the
expression with data of matching formats. Assignment to another field of a different date
format, will yield a random result.

In addition to the functions discussed in this topic, there are date and time functions that are
available only in the Maintain language. For information on these functions, see Maintain-
specific Date and Time Functions on page 479.

For many functions, the output argument can be supplied either as a field name or as a format
enclosed in single quotation marks. However, if a function is called from a Dialogue Manager
command, this argument must always be supplied as a format, and if a function is called from
a Maintain procedure, this argument must always be supplied as a field name. For detailed
information about calling a function and supplying arguments, see Accessing and Calling a
Function on page 59.

366
12. Date Functions

Using Standard Date Functions


When using standard date functions, you need to understand the settings that alter the
behavior of these functions, as well as the acceptable formats and how to supply values in
these formats.

You can affect the behavior of date functions in the following ways:

Defining which days of the week are work days and which are not. Then, when you use a
date function involving work days, dates that are not work days are ignored. For details, see
Specifying Work Days on page 367.
Determining whether to display leading zeros when a date function in Dialogue Manager
returns a date. For details, see Enabling Leading Zeros For Date and Time Functions in
Dialogue Manager on page 373.

For detailed information on each standard date function, see:

DATEADD: Adding or Subtracting a Date Unit to or From a Date on page 375

DATECVT: Converting the Format of a Date on page 378

DATEDIF: Finding the Difference Between Two Dates on page 380

DATEMOV: Moving a Date to a Significant Point on page 383

DATETRAN: Formatting Dates in International Formats on page 390

DPART: Extracting a Component From a Date on page 406

FIYR: Obtaining the Financial Year on page 410

FIQTR: Obtaining the Financial Quarter on page 408

FIYYQ: Converting a Calendar Date to a Financial Date on page 412

TODAY: Returning the Current Date on page 415

Specifying Work Days


You can determine which days are work days and which are not. Work days affect the
DATEADD, DATEDIF, and DATEMOV functions. You identify work days as business days or
holidays.

Specifying Business Days


Business days are traditionally Monday through Friday, but not every business has this
schedule. For example, if your company does business on Sunday, Tuesday, Wednesday,
Friday, and Saturday, you can tailor business day units to reflect that schedule.

Using Functions 367


Using Standard Date Functions

Syntax: How to Set Business Days

SET BUSDAYS = smtwtfs

where:
smtwtfs
Is the seven character list of days that represents your business week. The list has a
position for each day from Sunday to Saturday:

To identify a day of the week as a business day, enter the first letter of that day in that
day's position.

To identify a non-business day, enter an underscore (_) in that day's position.

If a letter is not in its correct position, or if you replace a letter with a character other than
an underscore, you receive an error message.

Example: Setting Business Days to Reflect Your Work Week


The following designates work days as Sunday, Tuesday, Wednesday, Friday, and Saturday:

SET BUSDAYS = S_TW_FS

Syntax: How to View the Current Setting of Business Days

? SET BUSDAYS

Specifying Holidays
You can specify a list of dates that are designated as holidays in your company. These dates
are excluded when using functions that perform calculations based on working days. For
example, if Thursday in a given week is designated as a holiday, the next working day after
Wednesday is Friday.

To define a list of holidays, you must:


1. Create a holiday file using a standard text editor.
2. Select the holiday file by issuing the SET command with the HDAY parameter.

Reference: Rules for Creating a Holiday File


Dates must be in YYMD format.

Dates must be in ascending order.

368
12. Date Functions

Each date must be on its own line.

Each year for which data exists must be included or the holiday file is considered invalid.
Calling a date function with a date value outside the range of the holiday file returns a zero
for business day requests.

If you are subtracting two dates in 2005, and the latest date in the holiday file is
20041231, the subtraction will not be performed. One way to avoid invalidating the holiday
file is to put a date very far in the future in any holiday file you create (for example,
29991231), and then it will always be considered valid.

You may include an optional description of the holiday, separated from the date by a space.

By default, the holiday file has a file name of the form HDAYxxxx.err and is on your path, or on
z/OS under PDS deployment, is a member named HDAYxxxx of a PDS allocated to DDNAME
ERRORS. In your procedure or request, you must issue the SET HDAY=xxxx command to
identify the file or member name. Alternatively, you can define the file to have any name and
be stored anywhere or, on z/OS under PDS deployment, allocate the holiday file as a
sequential file of any name or as member HDAYxxxx of any PDS. For information about using
non-default holiday file names, see How to FILEDEF or DYNAM the Holiday File on page 370.

Procedure: How to Create a Holiday File


1. In a text editor, create a list of dates designated as holidays using the Rules for Creating a
Holiday File on page 368.
2. Save the file.
If you are not using the default naming convention, see How to FILEDEF or DYNAM the
Holiday File on page 370. If you are using the default naming convention, use the
following instructions:

In Windows and UNIX: The file must be HDAYxxxx.ERR

In z/OS: The file must be a member of ERRORS named HDAYxxxx.

where:
xxxx
Is a string of text four characters long.

Using Functions 369


Using Standard Date Functions

Syntax: How to Select a Holiday File

SET HDAY = xxxx

where:
xxxx
Is the part of the name of the holiday file after HDAY. This string must be four characters
long.

Example: Creating and Selecting a Holiday File


The following is the HDAYTEST file, which establishes holidays:
19910325 TEST HOLIDAY
19911225 CHRISTMAS

The following sets HDAYTEST as the holiday file:


SET BUSDAYS = SMTWTFS
SET HDAY = TEST

This request uses HDAYTEST in its calculations:


TABLE FILE MOVIES
PRINT TITLE RELDATE
COMPUTE NEXTDATE/YMD = DATEADD(RELDATE, 'BD', 1);
WHERE RELDATE GE '19910101';
END

The output is:


TITLE RELDATE NEXTDATE
----- ------- --------
TOTAL RECALL 91/03/24 91/03/26

Syntax: How to FILEDEF or DYNAM the Holiday File


In all environments except z/OS under PDS deployment, use the following syntax.

FILEDEF HDAYxxxx DISK {app/|path}/filename.ext

where:
HDAYxxxx

Is the logical name (DDNAME) for the holiday file, where xxxx is any four characters. You
establish this logical name by issuing the SET HDAY=xxxx command in your procedure or
request.

370
12. Date Functions

app
Is the name of the application in which the holiday file resides.
path
Is the path to the holiday file.
filename.ext
Is the name of the holiday file.

On z/OS under PDS deployment, use the following to allocate a sequential holiday file.
DYNAM ALLOC {DD|FILE} HDAYxxxx DA qualif.filename.suffix SHR REU

On z/OS under PDS deployment, use the following to allocate a holiday file that is a member of
a PDS.
DYNAM ALLOC {DD|FILE} HDAYxxxx DA qualif.filename.suffix(HDAYxxx) SHR REU

where:
HDAYxxxx

Is the DDNAME for the holiday file. Your FOCEXEC or request must set the HDAY
parameter to xxxx, where xxxx is any four characters you choose. If your holiday file is a
member of a PDS, HDAYxxxx must also be the member name.
qualif.filename.suffix
Is the fully-qualified name of the sequential file that contains the list of holidays or the PDS
with member HDAYxxxx that contains the list of holidays.

Example: Defining a Holiday File


The following holiday file, named holiday.data in the c:\temp directory on Windows, defines
November 3, 2011 and December 24, 2011 as holidays:
20111103
20111224

Using Functions 371


Using Standard Date Functions

The following request against the MOVIES data source uses the FILEDEF command to define
this file as the holiday file. The logical name in the FILEDEF command is HDAYMMMM, and the
procedure issues the SET HDAY=MMMM command. It then defines the date November 2,
2011 and calculates the next business day:
FILEDEF HDAYMMMM DISK c:\ibi\holiday.data
SET HDAY = MMMM
SET BUSDAYS = _MTWTF_
DEFINE FILE MOVIES
NEWDATE/YYMD = '20111102';
NEXTDATE/YYMD = DATEADD(NEWDATE, 'BD', 1);
END
TABLE FILE MOVIES
SUM COPIES NEWDATE NEXTDATE
ON TABLE SET PAGE NOPAGE
END

The output shows that the next business day after November 2 is November 4 because
November 3 is a holiday:

Example: Allocating the Holiday File to a Sequential File on z/OS Under PDS Deployment
The following sequential file, named USER1.HOLIDAY.DATA, defines November 3, 2011 and
December 24, 2011 as holidays:
20111103
20111224

The following request against the MOVIES data source uses the DYNAM command to allocate
this file as the holiday file. The DDNAME in the DYNAM command is HDAYMMMM, and the
procedure issues the SET HDAY=MMMM command. It then defines the date November 2,
2011 and calculates the next business day:
DYNAM ALLOC DD HDAYMMMM DA USER1.HOLIDAY.DATA SHR REU
SET HDAY = MMMM
SET BUSDAYS = _MTWTF_
DEFINE FILE MOVIES
NEWDATE/YYMD = '20111102';
NEXTDATE/YYMD = DATEADD(NEWDATE, 'BD', 1);
END
TABLE FILE MOVIES
SUM COPIES NEWDATE NEXTDATE
ON TABLE SET PAGE NOPAGE
END

372
12. Date Functions

The output shows that the next business day after November 2 is November 4 because
November 3 is a holiday:

COPIES NEWDATE NEXTDATE


------ ------- --------
117 2011/11/02 2011/11/04

Example: Allocating the Holiday File to a PDS Member on z/OS Under PDS Deployment
The following holiday file, member HDAYMMMM in a PDS named USER1.HOLIDAY.DATA,
defines November 3, 2011 and December 24, 2011 as holidays:

20111103
20111224

The following request against the MOVIES data source uses the DYNAM command to allocate
this file as the holiday file. The DDNAME in the DYNAM command is HDAYMMMM, the member
name is also HDAYMMMM, and the procedure issues the SET HDAY=MMMM command. It
then defines the date November 2, 2011 and calculates the next business day:

DYNAM ALLOC DD HDAYMMMM DA USER1.HOLIDAY.DATA(HDAYMMMM) SHR REU


SET HDAY = MMMM
SET BUSDAYS = _MTWTF_
DEFINE FILE MOVIES
NEWDATE/YYMD = '20111102';
NEXTDATE/YYMD = DATEADD(NEWDATE, 'BD', 1);
END
TABLE FILE MOVIES
SUM COPIES NEWDATE NEXTDATE
ON TABLE SET PAGE NOPAGE
END

The output shows that the next business day after November 2 is November 4 because
November 3 is a holiday:

COPIES NEWDATE NEXTDATE


------ ------- --------
117 2011/11/02 2011/11/04

Enabling Leading Zeros For Date and Time Functions in Dialogue Manager
If you use a date and time function in Dialogue Manager that returns a numeric integer format,
Dialogue Manager truncates any leading zeros. For example, if a function returns the value
000101 (indicating January 1, 2000), Dialogue Manager truncates the leading zeros,
producing 101, an incorrect date. To avoid this problem, use the LEADZERO parameter.

Using Functions 373


Using Standard Date Functions

LEADZERO only supports an expression that makes a direct call to a function. An expression
that has nesting or another mathematical function always truncates leading zeros. For
example,

-SET &OUT = AYM(&IN, 1, 'I4')/100;

truncates leading zeros regardless of the LEADZERO parameter setting.

Syntax: How to Set the Display of Leading Zeros

SET LEADZERO = {ON|OFF}

where:
ON

Displays leading zeros if present.


OFF

Truncates leading zeros. OFF is the default value.

Example: Displaying Leading Zeros


The AYM function adds one month to the input date of December 1999:

-SET &IN = '9912';


-RUN
-SET &OUT = AYM(&IN, 1, 'I4');
-TYPE &OUT

Using the default LEADZERO setting, this yields:

This represents the date January 2000 incorrectly. Setting the LEADZERO parameter in the
request as follows:

SET LEADZERO = ON
-SET &IN = '9912';
-SET &OUT = AYM(&IN, 1, 'I4');
-TYPE &OUT

results in the following:

0001

This correctly indicates January 2000.

374
12. Date Functions

DATEADD: Adding or Subtracting a Date Unit to or From a Date


Available Languages: reporting, Maintain

The DATEADD function adds a unit to or subtracts a unit from a full component date format. A
unit is one of the following:

Year.

Month. If the calculation using the month unit creates an invalid date, DATEADD corrects it
to the last day of the month. For example, adding one month to October 31 yields
November 30, not November 31, since November has 30 days.

Day.

Weekday. When using the weekday unit, DATEADD does not count Saturday or Sunday. For
example, if you add one day to Friday, first DATEADD moves to the next weekday, Monday,
then it adds a day. The result is Tuesday.

Business day. When using the business day unit, DATEADD uses the BUSDAYS parameter
setting and holiday file to determine which days are working days and disregards the rest. If
Monday is not a working day, then one business day past Sunday is Tuesday.

Note that when the DATEADD function calculates the next or previous business day or work
day, it always starts from a business day or work day. So if the actual day is Saturday or
Sunday, and the request wants to calculate the next business day, the function will use
Monday as the starting day, not Saturday or Sunday, and will return Tuesday as the next
business day. Similarly, when calculating the previous business day, it will use the starting day
Friday, and will return Thursday as the previous business day. You can use the DATEMOV
function to move the date to the correct type of day before using DATEADD.

DATEADD requires a date to be in date format. Since Dialogue Manager interprets a date as
alphanumeric or numeric, and DATEADD requires a standard date stored as an offset from the
base date, do not use DATEADD with Dialogue Manager unless you first convert the variable
used as the input date to an offset from the base date.

You add or subtract non day-based dates (for example, YM or YQ) directly without using
DATEADD.

DATEADD works only with full component dates.

Using Functions 375


DATEADD: Adding or Subtracting a Date Unit to or From a Date

Syntax: How to Add or Subtract a Date Unit to or From a Date

DATEADD(date, 'component', increment)

where:
date
Date

Is a full component date.


component
Alphanumeric

Is one of the following enclosed in single quotation marks:

Y indicates a year component.

M indicates a month component.

D indicates a day component.

WD indicates a weekday component.

BD indicates a business day component.

increment
Integer

Is the number of date units added to or subtracted from date. If this number is not a whole
unit, it is rounded down to the next largest integer.

Note: DATEADD does not use an output argument. It uses the format of the date argument for
the result. As long as the result is a full component date, it can be assigned only to a full
component date field or to integer field.

Example: Truncation With DATEADD


The number of units passed to DATEADD is always a whole unit. For example
DATEADD(DATE, 'M', 1.999)

adds one month because the number of units is less than two.

376
12. Date Functions

Example: Using the Weekday Unit


If you use the weekday unit and a Saturday or Sunday is the input date, DATEADD changes the
input date to Monday. The function
DATEADD('910623', 'WD', 1)

in which DATE is either Saturday or Sunday yields Tuesday; Saturday and Sunday are not
weekdays, so DATEADD begins with Monday and adds one.

Note that the single quotes around the number in the first argument, ‘910623’, causes it to
be treated as a natural date literal.

Example: Adding Weekdays to a Date (Reporting)


DATEADD adds three weekdays to NEW_DATE. In some cases, it adds more than three days
because HIRE_DATE_PLUS_THREE would otherwise be on a weekend.
TABLE FILE EMPLOYEE
PRINT FIRST_NAME AND HIRE_DATE AND COMPUTE
NEW_DATE/YYMD = HIRE_DATE;
HIRE_DATE_PLUS_THREE/YYMD = DATEADD(NEW_DATE, 'WD', 3);
BY LAST_NAME
WHERE DEPARTMENT EQ 'MIS';
END

The output is:


LAST_NAME FIRST_NAME HIRE_DATE NEW_DATE HIRE_DATE_PLUS_THREE
--------- ---------- --------- -------- --------------------
BLACKWOOD ROSEMARIE 82/04/01 1982/04/01 1982/04/06
CROSS BARBARA 81/11/02 1981/11/02 1981/11/05
GREENSPAN MARY 82/04/01 1982/04/01 1982/04/06
JONES DIANE 82/05/01 1982/05/01 1982/05/06
MCCOY JOHN 81/07/01 1981/07/01 1981/07/06
SMITH MARY 81/07/01 1981/07/01 1981/07/06

Using Functions 377


DATECVT: Converting the Format of a Date

Example: Determining If a Date Is a Work Day (Reporting)


DATEADD determines which values in the TRANSDATE field do not represent work days by
adding zero days to TRANSDATE using the business day unit. If TRANSDATE does not
represent a business day, DATEADD returns the next business day to DATEX. TRANSDATE is
then compared to DATEX, and the day of the week is printed for all dates that do not match
between the two fields, resulting in a list of all non-work days.

DEFINE FILE VIDEOTRK


DATEX/YMD = DATEADD(TRANSDATE, 'BD', 0);
DATEINT/I8YYMD = DATECVT(TRANSDATE, 'YMD','I8YYMD');
END
TABLE FILE VIDEOTRK
SUM TRANSDATE NOPRINT
COMPUTE DAYNAME/A8 = DOWKL(DATEINT, DAYNAME); AS 'Day of Week'
BY TRANSDATE AS 'Date'
WHERE TRANSDATE NE DATEX
END

The output is:


Date Day of Week
---- -----------
91/06/22 SATURDAY
91/06/23 SUNDAY
91/06/30 SUNDAY

Example: Adding Months to a Date (Maintain)


DATEADD adds months to the DATE1 field:

MAINTAIN
compute DATE1/yymd = '20000101'
compute DATE2/yymd=dateadd(date1, 'M', 2, date2);
type "DATE1 = <<DATE1 + 2 MONTHS = DATE2 = <<DATE2"
END

The result is:


DATE1 = 2000/01/01+ 2 MONTHS = DATE2 = 2000/03/01

DATECVT: Converting the Format of a Date


Available Languages: reporting, Maintain

The DATECVT function converts the field value of any standard date format or legacy date
format into a date format (offset from the base date), in the desired standard date format or
legacy date format. If you supply an invalid format, DATECVT returns a zero or a blank.

DATECVT turns off optimization and compilation.

378
12. Date Functions

Note: You can use simple assignment instead of calling this function.

Syntax: How to Convert a Date Format

DATECVT(date, 'in_format', output)

where:
date
Date

Is the date to be converted. If you supply an invalid date, DATECVT returns zero. When the
conversion is performed, a legacy date obeys any DEFCENT and YRTHRESH parameter
settings supplied for that field.
in_format
Alphanumeric

Is the format of the date enclosed in single quotation marks. It is one of the following:

A non-legacy date format (for example, YYMD, YQ, M, DMY, JUL).

A legacy date format (for example, I6YMD or A8MDYY).

A non-date format (such as I8 or A6). A non-date format in in_format functions as an


offset from the base date of a YYMD field (12/31/1900).
output
Alphanumeric

Is the output format enclosed in single quotation marks or a field containing the format. It
is one of the following:

A non-legacy date format (for example, YYMD, YQ, M, DMY, JUL).

A legacy date format (for example, I6YMD or A8MDYY).

A non-date format (such as I8 or A6). This format type causes DATECVT to convert the
date into a full component date and return it as a whole number in the format provided.

Example: Converting a YYMD Date to DMY


DATECVT converts 19991231 to 311299 and stores the result in CONV_FIELD:
CONV_FIELD/DMY = DATECVT(19991231, 'I8YYMD', 'DMY');

or

Using Functions 379


DATEDIF: Finding the Difference Between Two Dates

ONV_FIELD/DMY = DATECVT('19991231', 'A8YYMD', 'DMY');

Example: Converting a Legacy Date to Date Format (Reporting)


DATECVT converts HIRE_DATE from I6YMD legacy date format to YYMD date format:

TABLE FILE EMPLOYEE


PRINT FIRST_NAME AND HIRE_DATE AND COMPUTE
NEW_HIRE_DATE/YYMD = DATECVT(HIRE_DATE, 'I6YMD', 'YYMD');
BY LAST_NAME
WHERE DEPARTMENT EQ 'MIS';
END

The output is:


LAST_NAME FIRST_NAME HIRE_DATE NEW_HIRE_DATE
--------- ---------- --------- -------------
BLACKWOOD ROSEMARIE 82/04/01 1982/04/01
CROSS BARBARA 81/11/02 1981/11/02
GREENSPAN MARY 82/04/01 1982/04/01
JONES DIANE 82/05/01 1982/05/01
MCCOY JOHN 81/07/01 1981/07/01
SMITH MARY 81/07/01 1981/07/01

DATEDIF: Finding the Difference Between Two Dates


Available Languages: reporting, Maintain

The DATEDIF function returns the difference between two full component standard dates in
units of a specified component. A component is one of the following:

Year. Using the year unit with DATEDIF yields the inverse of DATEADD. If subtracting one
year from date X creates date Y, then the count of years between X and Y is one.
Subtracting one year from February 29 produces the date February 28.

Month. Using the month component with DATEDIF yields the inverse of DATEADD. If
subtracting one month from date X creates date Y, then the count of months between X
and Y is one. If the to-date is the end-of-month, then the month difference may be rounded
up (in absolute terms) to guarantee the inverse rule.

If one or both of the input dates is the end of the month, DATEDIF takes this into account.
This means that the difference between January 31 and April 30 is three months, not two
months.

Day.

Weekday. With the weekday unit, DATEDIF does not count Saturday or Sunday when
calculating days. This means that the difference between Friday and Monday is one day.

380
12. Date Functions

Business day. With the business day unit, DATEDIF uses the BUSDAYS parameter setting
and holiday file to determine which days are working days and disregards the rest. This
means that if Monday is not a working day, the difference between Friday and Tuesday is
one day.

DATEDIF returns a whole number. If the difference between two dates is not a whole number,
DATEDIF truncates the value to the next largest integer. For example, the number of years
between March 2, 2001, and March 1, 2002, is zero. If the end date is before the start date,
DATEDIF returns a negative number.

You can find the difference between non-day based dates (for example YM or YQ) directly
without using DATEDIF.

Since Dialogue Manager interprets a date as alphanumeric or numeric, and DATEDIF requires a
standard date stored as an offset from the base date, do not use DATEDIF with Dialogue
Manager unless you first convert the variable used as the input date to an offset from the base
date.

DATEDIF works only with full component dates.

Syntax: How to Find the Difference Between Two Dates

DATEDIF(from_date, to_date, 'component')

where:
from_date
Date

Is the start date from which to calculate the difference. Is a full component date.
to_date
Date

Is the end date from which to calculate the difference.

Using Functions 381


DATEDIF: Finding the Difference Between Two Dates

component
Alphanumeric

Is one of the following enclosed in single quotation marks:

Y indicates a year unit.

M indicates a month unit.

D indicates a day unit.

WD indicates a weekday unit.

BD indicates a business day unit.

Note: DATEDIF does not use an output argument because for the result it uses the format 'I8'.

Example: Truncation With DATEDIF


DATEDIF calculates the difference between March 2, 1996, and March 1, 1997, and returns a
zero because the difference is less than a year:
DATEDIF('19960302', '19970301', 'Y')

Example: Using Month Calculations


The following expressions return a result of minus one month:
DATEDIF('19990228', '19990128', 'M')
DATEDIF('19990228', '19990129', 'M')
DATEDIF('19990228', '19990130', 'M')
DATEDIF('19990228', '19990131', 'M')

Additional examples:

DATEDIF( 'March 31 2001', 'May 31 2001', 'M') yields 2.

DATEDIF( 'March 31 2001', 'May 30 2001', 'M') yields 1 (because May 30 is not the
end of the month).

DATEDIF( 'March 31 2001', 'April 30 2001', 'M') yields 1.

382
12. Date Functions

Example: Finding the Number of Weekdays Between Two Dates (Reporting)


DATECVT converts the legacy dates in HIRE_DATE and DAT_INC to the date format YYMD.
DATEDIF then uses those date formats to determine the number of weekdays between
NEW_HIRE_DATE and NEW_DAT_INC:
TABLE FILE EMPLOYEE
PRINT FIRST_NAME AND
COMPUTE NEW_HIRE_DATE/YYMD = DATECVT(HIRE_DATE, 'I6YMD', 'YYMD'); AND
COMPUTE NEW_DAT_INC/YYMD = DATECVT(DAT_INC, 'I6YMD', 'YYMD'); AND
COMPUTE WDAYS_HIRED/I8 = DATEDIF(NEW_HIRE_DATE, NEW_DAT_INC, 'WD');
BY LAST_NAME
IF WDAYS_HIRED NE 0
WHERE DEPARTMENT EQ 'PRODUCTION';
END

The output is:


LAST_NAME FIRST_NAME NEW_HIRE_DATE NEW_DAT_INC WDAYS_HIRED
--------- ---------- ------------- ----------- -----------
IRVING JOAN 1982/01/04 1982/05/14 94
MCKNIGHT ROGER 1982/02/02 1982/05/14 73
SMITH RICHARD 1982/01/04 1982/05/14 94
STEVENS ALFRED 1980/06/02 1982/01/01 414
ALFRED 1980/06/02 1981/01/01 153

Example: Finding the Number of Years Between Two Dates (Maintain)


DATEDIF determines the number of years between DATE2 and DATE1:
MAINTAIN
Case Top
compute DATE1/yymd = '20020717';
compute DATE2/yymd = '19880705';
compute DIFF/I3= DATEDIF(DATE2, DATE1, 'Y', DIFF);
type "<<DATE1 - <<DATE2 = <DIFF YEARS"
ENDCASE
END

The result is:


2002/07/17 - 1988/07/05 = 14 YEARS

DATEMOV: Moving a Date to a Significant Point


Available Languages: reporting, Maintain

The DATEMOV function moves a date to a significant point on the calendar.

Using Functions 383


DATEMOV: Moving a Date to a Significant Point

Note: Using the beginning of week point (BOW) will always return Monday, and using the end of
week point (EOW) will always return Friday. Also, if the date used with the DATEMOV function
falls on Saturday or Sunday, the actual date used by the function will be the moved forward to
the next Monday. If you do not want to do the calculation by moving the date from Saturday or
Sunday to Monday, or if you want the BOW to be Sunday and the EOW to be Saturday, you can
use the DTRUNC function.

Since Dialogue Manager interprets a date as alphanumeric or numeric, and DATEMOV requires
a standard date stored as an offset from the base date, do not use DATEMOV with Dialogue
Manager unless you first convert the variable used as the input date to an offset from the base
date. For example, the following converts the integer legacy date 20050131 to a smart date,
adds one month, and converts the result to an alphanumeric legacy date:

-SET &STRT=DATECVT(20050131,'I8YYMD', 'YYMD');


-SET &NMT=DATEADD(&STRT,'M',1);
-SET &NMTA=DATECVT(&NMT,'YYMD','A8MTDYY');
-TYPE A MONTH FROM 20050131 IS &NMTA

The output shows that the DATEADD function added the actual number of days in the month of
February to get to the end of the month from the end of January:

A MONTH FROM 20050131 IS 02282005

DATEMOV works only with full component dates.

Syntax: How to Move a Date to a Significant Point

DATEMOV(date, 'move-point')

where:
date
Date

Is the date to be moved. It must be a full component format date (for example, MDYY or
YYJUL).
move-point
Alphanumeric

Is the significant point the date is moved to enclosed in single quotation marks ('). An
invalid point results in a return code of zero. Valid values are:

EOM, which is the end of month.

BOM, which is the beginning of month.

384
12. Date Functions

EOQ, which is the end of quarter.

BOQ, which is the beginning of quarter.

EOY, which is the end of year.

BOY, which is the beginning of year.

EOW, which is the end of week.

BOW, which is the beginning of week.

NWD, which is the next weekday.

NBD, which is the next business day.

PWD, which is the prior weekday.

PBD, which is the prior business day.

WD-, which is a weekday or earlier.

BD-, which is a business day or earlier.

WD+, which is a weekday or later.

BD+, which is a business day or later.

A business day calculation is affected by the BUSDAYS and HDAY parameter settings.

Note that when the DATEADD function calculates the next or previous business day or work
day, it always starts from a business day or work day. So if the actual day is Saturday or
Sunday, and the request wants to calculate the next business day, the function will use
Monday as the starting day, not Saturday or Sunday, and will return Tuesday as the next
business day. Similarly, when calculating the previous business day, it will use the starting day
Friday, and will return Thursday as the previous business day.

To avoid skipping a business day or work day, use DATEMOV. To return the next business or
work day, use BD- or WD- to first move to the previous business or work day (if it is already a
business day or work day, it will not be moved). Then use DATEADD to move to the next
business or work day. If you want to return the previous business or work day, first use BD+ or
WD+ to move to the next business or work day (if it is already the correct type of day, it will not
be moved). Then use DATEADD to return the previous business or work day.

Note: DATEMOV does not use an output argument. It uses the format of the date argument for
the result. As long as the result is a full component date, it can be assigned only to a full
component date field or to an integer field.

Using Functions 385


DATEMOV: Moving a Date to a Significant Point

Example: Returning the Next Business Day


This example shows why you may need to use DATEMOV to get the correct result.

The following request against the GGSALES data source uses the BD (Business Day) move
point against the DATE field. First DATE is converted to a smart date, then DATEADD is called
with the BD move-point:
DEFINE FILE GGSALES
DT1/WMDYY=DATE;
DT2/WMDYY = DATEADD(DT1 ,'BD',1);
DAY/Dt = DT1;
END

TABLE FILE GGSALES


SUM DT1
DT2
BY DT1 NOPRINT
WHERE RECORDLIMIT EQ 10
END

When the date is on a Saturday or Sunday on the output, the next business day is returned as
a Tuesday. This is because before doing the calculation, the original date was moved to a
business day:
DT1 DT2
--- ---
SUN, 09/01/1996 TUE, 09/03/1996
FRI, 11/01/1996 MON, 11/04/1996
SUN, 12/01/1996 TUE, 12/03/1996
SAT, 03/01/1997 TUE, 03/04/1997
TUE, 04/01/1997 WED, 04/02/1997
THU, 05/01/1997 FRI, 05/02/1997
SUN, 06/01/1997 TUE, 06/03/1997
MON, 09/01/1997 TUE, 09/02/1997
WED, 10/01/1997 THU, 10/02/1997

386
12. Date Functions

In the following version of the request, DATEMOV is called to make sure the starting day is a
business day. The move point specified in the first call is BD- which only moves the date to the
prior business day if it is not already a business day. The call to DATEADD then uses the BD
move point to return the next business day:
DEFINE FILE GGSALES
DT1/WMDYY=DATE;
DT1A/WMDYY=DATEMOV(DT1, 'BD-');
DT2/WMDYY = DATEADD(DT1A,'BD',1);
DAY/Dt = DT1;
END

TABLE FILE GGSALES


SUM DT1 DT1A DT2
BY DT1 NOPRINT
WHERE RECORDLIMIT EQ 10
END

On the output, the next business day after a Saturday or Sunday is now returned as Monday:
DT1 DT1A DT2
--- ---- ---
SUN, 09/01/1996 FRI, 08/30/1996 MON, 09/02/1996
FRI, 11/01/1996 FRI, 11/01/1996 MON, 11/04/1996
SUN, 12/01/1996 FRI, 11/29/1996 MON, 12/02/1996
SAT, 03/01/1997 FRI, 02/28/1997 MON, 03/03/1997
TUE, 04/01/1997 TUE, 04/01/1997 WED, 04/02/1997
THU, 05/01/1997 THU, 05/01/1997 FRI, 05/02/1997
SUN, 06/01/1997 FRI, 05/30/1997 MON, 06/02/1997
MON, 09/01/1997 MON, 09/01/1997 TUE, 09/02/1997
WED, 10/01/1997 WED, 10/01/1997 THU, 10/02/1997

Example: Using a DEFINE FUNCTION to Move a Date to the Beginning of the Week
The following DEFINE FUNCTION named BOWK takes a date and the name of the day you want
to consider the beginning of the week and returns a date that corresponds to the beginning of
the week:
DEFINE FUNCTION BOWK(THEDATE/MDYY,WEEKSTART/A10)
DAYOFWEEK/W=THEDATE;
DAYNO/I1=IF DAYOFWEEK EQ 7 THEN 0 ELSE DAYOFWEEK;
FIRSTOFWK/I1=DECODE WEEKSTART('SUNDAY' 0 'MONDAY' 1 'TUESDAY' 2
'WEDNESDAY' 3 'THURSDAY' 4 'FRIDAY' 5 'SATURDAY' 6
'SUN' 0 'MON' 1 'TUE' 2 'WED' 3 'THU' 4 'FRI' 5 'SAT' 6);
BOWK/MDYY=IF DAYNO GE FIRSTOFWK THEN THEDATE-DAYNO+FIRSTOFWK
ELSE THEDATE-7-DAYNO+FIRSTOFWK;
END

Using Functions 387


DATEMOV: Moving a Date to a Significant Point

The following request uses the BOWK function to use return a date (DT2) that corresponds to
the beginning of the week for each value of the DT1 field:
DEFINE FILE GGSALES
DT1/WMDYY=DATE;
DT2/WMDYY = BOWK(DT1 ,'SUN');
END

TABLE FILE GGSALES


SUM DT1
DT2
BY DT1 NOPRINT
WHERE RECORDLIMIT EQ 10
ON TABLE SET PAGE NOLEAD
END

The output is shown in the following image:

388
12. Date Functions

Example: Determining Significant Points for a Date (Reporting)


The BUSDAYS parameter sets the business days to Monday, Tuesday, Wednesday, and
Thursday. DATECVT converts the legacy date HIRE_DATE to the date format YYMD and provides
date display options. DATEMOV then determines significant points for HIRE_DATE.

SET BUSDAY = _MTWT__


TABLE FILE EMPLOYEE
PRINT
COMPUTE NEW_DATE/YYMD = DATECVT(HIRE_DATE, 'I6YMD', 'YYMD'); AND
COMPUTE NEW_DATE/WT = DATECVT(HIRE_DATE, 'I6YMD', 'WT'); AS 'DOW' AND
COMPUTE NWD/WT = DATEMOV(NEW_DATE, 'NWD'); AND
COMPUTE PWD/WT = DATEMOV(NEW_DATE, 'PWD'); AND
COMPUTE WDP/WT = DATEMOV(NEW_DATE, 'WD+'); AS 'WD+' AND
COMPUTE WDM/WT = DATEMOV(NEW_DATE, 'WD-'); AS 'WD-' AND
COMPUTE NBD/WT = DATEMOV(NEW_DATE, 'NBD'); AND
COMPUTE PBD/WT = DATEMOV(NEW_DATE, 'PBD'); AND
COMPUTE WBP/WT = DATEMOV(NEW_DATE, 'BD+'); AS 'BD+' AND
COMPUTE WBM/WT = DATEMOV(NEW_DATE, 'BD-'); AS 'BD-' BY LAST_NAME NOPRINT
HEADING
"Examples of DATEMOV"
"Business days are Monday, Tuesday, Wednesday, + Thursday "
" "
"START DATE.. | MOVE POINTS..........................."
WHERE DEPARTMENT EQ 'MIS';
END

The output is:


Examples of DATEMOV
Business days are Monday, Tuesday, Wednesday, + Thursday
START DATE.. | MOVE POINTS...........................
NEW_DATE DOW NWD PWD WD+ WD- NBD PBD BD+ BD-
-------- --- --- --- --- --- --- --- --- ---
1982/04/01 THU FRI WED THU THU MON WED THU THU
1981/11/02 MON TUE FRI MON MON TUE THU MON MON
1982/04/01 THU FRI WED THU THU MON WED THU THU
1982/05/01 SAT TUE THU MON FRI TUE WED MON THU
1981/07/01 WED THU TUE WED WED THU TUE WED WED
1981/07/01 WED THU TUE WED WED THU TUE WED WED

Example: Determining the End of the Week (Reporting)


DATEMOV determines the end of the week for each date in NEW_DATE and stores the result in
EOW:

TABLE FILE EMPLOYEE


PRINT FIRST_NAME AND
COMPUTE NEW_DATE/YYMDWT = DATECVT(HIRE_DATE, 'I6YMD', 'YYMDWT'); AND
COMPUTE EOW/YYMDWT = DATEMOV(NEW_DATE, 'EOW');
BY LAST_NAME
WHERE DEPARTMENT EQ 'PRODUCTION';
END

Using Functions 389


DATETRAN: Formatting Dates in International Formats

The output is:


LAST_NAME FIRST_NAME NEW_DATE EOW
--------- ---------- -------- ---
BANNING JOHN 1982 AUG 1, SUN 1982 AUG 6, FRI
IRVING JOAN 1982 JAN 4, MON 1982 JAN 8, FRI
MCKNIGHT ROGER 1982 FEB 2, TUE 1982 FEB 5, FRI
ROMANS ANTHONY 1982 JUL 1, THU 1982 JUL 2, FRI
SMITH RICHARD 1982 JAN 4, MON 1982 JAN 8, FRI
STEVENS ALFRED 1980 JUN 2, MON 1980 JUN 6, FRI

Example: Determining the End of the Week (Maintain)


DATEMOV determines the end of the week for each date:
MAINTAIN
COMPUTE X/YYMDWT='20020717';
COMPUTE Y/YYMDWT=DATEMOV(X, 'EOW', Y);
TYPE "<<X <<Y END OF WEEK "
END

The result is:


2002/07/17, WED 2002/07/19, FRI END OF WEEK

DATETRAN: Formatting Dates in International Formats


Available Languages: reporting, Maintain

The DATETRAN function formats dates in international formats.

Syntax: How to Format Dates in International Formats

DATETRAN (indate, '(intype)', '([formatops])', 'lang', outlen, output)

where:
indate
Is the input date (in date format) to be formatted. Note that the date format cannot be an
alphanumeric or numeric format with date display options (legacy date format).

390
12. Date Functions

intype
Is one of the following character strings indicating the input date components and the
order in which you want them to display, enclosed in parentheses and single quotation
marks.

The following table shows the single component input types:

Single Component Input Type Description

'(W)' Day of week component only (original format


must have only W component).

'(M)' Month component only (original format must


have only M component).

The following table shows the two-component input types:

Two-Component Input Type Description

'(YYM)' Four-digit year followed by month.

'(YM)' Two-digit year followed by month.

'(MYY)' Month component followed by four-digit year.

'(MY)' Month component followed by two-digit year.

The following table shows the three-component input types:

Three-Component Input Type Description

'(YYMD)' Four-digit year followed by month followed by


day.

'(YMD)' Two-digit year followed by month followed by


day.

'(DMYY)' Day component followed by month followed by


four-digit year.

Using Functions 391


DATETRAN: Formatting Dates in International Formats

Three-Component Input Type Description

'(DMY)' Day component followed by month followed by


two-digit year.

'(MDYY)' Month component followed by day followed by


four-digit year.

'(MDY)' Month component followed by day followed by


two-digit year.

'(MD)' Month component followed by day (derived from


three-component date by ignoring year
component).

'(DM)' Day component followed by month (derived from


three-component date by ignoring year
component).

formatops
Is a string of zero or more formatting options enclosed in parentheses and single quotation
marks. The parentheses and quotation marks are required even if you do not specify
formatting options. Formatting options fall into the following categories:

Options for suppressing initial zeros in month or day numbers.

Note: Zero suppression replaces initial zeros with blanks spaces.

Options for translating month or day components to full or abbreviated uppercase or


default case (mixed-case or lowercase depending on the language) names.

Date delimiter options and options for punctuating a date with commas.

Valid options for suppressing initial zeros in month or day numbers are listed in the
following table. Note that the initial zero is replaced by a blank space:

Format Option Description

m Zero-suppresses months (displays numeric


months before October as 1 through 9 rather
than 01 through 09).

392
12. Date Functions

Format Option Description

d Displays days before the tenth of the month as 1


through 9 rather than 01 through 09.

dp Displays days before the tenth of the month as 1


through 9 rather than 01 through 09 with a
period after the number.

do Displays days before the tenth of the month as 1


through 9. For English (langcode EN) only,
displays an ordinal suffix (st, nd, rd, or th) after
the number.

The following table shows valid month and day name translation options:

Format Option Description

T Displays month as an abbreviated name, with no


punctuation, all uppercase.

TR Displays month as a full name, all uppercase.

Tp Displays month as an abbreviated name,


followed by a period, all uppercase.

t Displays month as an abbreviated name with no


punctuation. The name is all lowercase or initial
uppercase, depending on language code.

tr Displays month as a full name. The name is all


lowercase or initial uppercase, depending on
language code.

tp Displays month as an abbreviated name,


followed by a period. The name displays in the
default case of the specified language (for
example, all lowercase for French and Spanish,
initial uppercase for English and German).

Using Functions 393


DATETRAN: Formatting Dates in International Formats

Format Option Description

W Includes an abbreviated day-of-the-week name at


the start of the displayed date, all uppercase
with no punctuation.

WR Includes a full day-of-the-week name at the start


of the displayed date, all uppercase.

Wp Includes an abbreviated day-of-the-week name at


the start of the displayed date, all uppercase,
followed by a period.

w Includes an abbreviated day-of-the-week name at


the start of the displayed date with no
punctuation. The name displays in the default
case of the specified language (for example, all
lowercase for French and Spanish, initial
uppercase for English and German).

wr Includes a full day-of-the-week name at the start


of the displayed date. The name displays in the
default case of the specified language (for
example, all lowercase for French and Spanish,
initial uppercase for English and German).

wp Includes an abbreviated day-of-the-week name at


the start of the displayed date followed by a
period. The name displays in the default case of
the specified language (for example, all
lowercase for French and Spanish, initial
uppercase for English and German).

X Includes an abbreviated day-of-the-week name at


the end of the displayed date, all uppercase with
no punctuation.

XR Includes a full day-of-the-week name at the end


of the displayed date, all uppercase.

394
12. Date Functions

Format Option Description

Xp Includes an abbreviated day-of-the-week name at


the end of the displayed date, all uppercase,
followed by a period.

x Includes an abbreviated day-of-the-week name at


the end of the displayed date with no
punctuation. The name displays in the default
case of the specified language (for example, all
lowercase for French and Spanish, initial
uppercase for English and German).

xr Includes a full day-of-the-week name at the end


of the displayed date. The name displays in the
default case of the specified language (for
example, all lowercase for French and Spanish,
initial uppercase for English and German).

xp Includes an abbreviated day-of-the-week name at


the end of the displayed date followed by a
period. The name displays in the default case of
the specified language (for example, all
lowercase for French and Spanish, initial
uppercase for English and German).

The following table shows valid date delimiter options:

Format Option Description

B Uses a blank as the component delimiter. This is


the default if the month or day of week is
translated or if comma is used.

. Uses a period (.) as the component delimiter.

- Uses a minus sign (-) as the component


delimiter. This is the default when the conditions
for a blank default delimiter are not satisfied.

Using Functions 395


DATETRAN: Formatting Dates in International Formats

Format Option Description

/ Uses a slash (/) as the component delimiter.

| Omits component delimiters.

K Uses appropriate Asian characters as component


delimiters.

c Places a comma (,) after the month name


(following T, Tp, TR, t, tp, or tr).

Places a comma and blank after the day name


(following W, Wp, WR, w, wp, or wr).

Places a comma and blank before the day name


(following X, XR, x, or xr).

e Displays the Spanish or Portuguese word de or


DE between the day and month, and between the
month and year. The case of the word de is
determined by the case of the month name. If the
month is displayed in uppercase, DE is displayed.
Otherwise, de is displayed. Useful for formats
DMY, DMYY, MY, and MYY.

D Inserts a comma (,) after the day number and


before the general delimiter character specified.

Y Inserts a comma (,) after the year and before the


general delimiter character specified.

lang
Is the two-character standard ISO code for the language into which the date should be
translated, enclosed in single quotation marks ('). Valid language codes are:

'AR' Arabic

'CS' Czech

'DA' Danish

'DE' German

396
12. Date Functions

'EN' English

'ES' Spanish

'FI' Finnish

'FR' French

'EL' Greek

'IW' Hebrew

'IT' Italian

'JA' Japanese

'KO' Korean

'LT' Lithuanian

'NL' Dutch

'NO' Norwegian

'PO' Polish

'PT' Portuguese

'RU' Russian

'SV' Swedish

'TH' Thai

'TR' Turkish

'TW' Chinese (Traditional)

'ZH' Chinese (Simplified)


outlen
Numeric

Is the length of the output field in bytes. If the length is insufficient, an all blank result is
returned. If the length is greater than required, the field is padded with blanks on the right.

Using Functions 397


DATETRAN: Formatting Dates in International Formats

output
Alphanumeric

Is the name of the field that contains the translated date, or its format enclosed in single
quotation marks.

Reference: Usage Notes for the DATETRAN Function


The output field, though it must be type A, and not AnV, may in fact contain variable length
information, since the lengths of month names and day names can vary, and also month
and day numbers may be either one or two bytes long if a zero-suppression option is
selected. Unused bytes are filled with blanks.

All invalid and inconsistent inputs result in all blank output strings. Missing data also
results in blank output.

The base dates (1900-12-31 and 1900-12 or 1901-01) are treated as though the
DATEDISPLAY setting were ON (that is, not automatically shown as blanks). To suppress
the printing of base dates, which have an internal integer value of 0, test for 0 before
calling DATETRAN. For example:
RESULT/A40 = IF DATE EQ 0 THEN ' ' ELSE
DATETRAN (DATE, '(YYMD)', '(.t)', 'FR', 40, 'A40');

Valid translated date components are contained in files named DTLNGlng where lng is a
three-character code that specifies the language. These files must be accessible for each
language into which you want to translate dates.

For these NLS characters to appear correctly, the Server and Client must be configured with
the correct code pages.

The DATETRAN function is not supported in Dialogue Manager.

398
12. Date Functions

Example: Using the DATETRAN Function


The following request prints the day of the week in the default case of the specific language:
DEFINE FILE VIDEOTRK
TRANS1/YYMD=20050104;
TRANS2/YYMD=20051003;

DATEW/W=TRANS1 ;
DATEW2/W=TRANS2 ;
DATEYYMD/YYMDW=TRANS1 ;
DATEYYMD2/YYMDW=TRANS2 ;

OUT1A/A8=DATETRAN(DATEW, '(W)', '(wr)', 'EN', 8 , 'A8') ;


OUT1B/A8=DATETRAN(DATEW2, '(W)', '(wr)', 'EN', 8 , 'A8') ;
OUT1C/A8=DATETRAN(DATEW, '(W)', '(wr)', 'ES', 8 , 'A8') ;
OUT1D/A8=DATETRAN(DATEW2, '(W)', '(wr)', 'ES', 8 , 'A8') ;
OUT1E/A8=DATETRAN(DATEW, '(W)', '(wr)', 'FR', 8 , 'A8') ;
OUT1F/A8=DATETRAN(DATEW2, '(W)', '(wr)', 'FR', 8 , 'A8') ;
OUT1G/A8=DATETRAN(DATEW, '(W)', '(wr)', 'DE', 8 , 'A8') ;
OUT1H/A8=DATETRAN(DATEW2, '(W)', '(wr)', 'DE', 8 , 'A8') ;
END

TABLE FILE VIDEOTRK


HEADING
"FORMAT wr"
""
"Full day of week name at beginning of date, default case (wr)"
"English / Spanish / French / German"
""
SUM OUT1A AS '' OUT1B AS '' TRANSDATE NOPRINT
OVER OUT1C AS '' OUT1D AS ''
OVER OUT1E AS '' OUT1F AS ''
OVER OUT1G AS '' OUT1H AS ''
ON TABLE SET PAGE-NUM OFF
ON TABLE SET STYLE *
GRID=OFF, $
END

Using Functions 399


DATETRAN: Formatting Dates in International Formats

The output is:

The following request prints a blank delimited date with an abbreviated month name in English.
Initial zeros in the day number are suppressed, and a suffix is added to the end of the number:
DEFINE FILE VIDEOTRK
TRANS1/YYMD=20050104;
TRANS2/YYMD=20050302;

DATEW/W=TRANS1 ;
DATEW2/W=TRANS2 ;
DATEYYMD/YYMDW=TRANS1 ;
DATEYYMD2/YYMDW=TRANS2 ;

OUT2A/A15=DATETRAN(DATEYYMD, '(MDYY)', '(Btdo)', 'EN', 15, 'A15') ;


OUT2B/A15=DATETRAN(DATEYYMD2, '(MDYY)', '(Btdo)', 'EN', 15, 'A15') ;
END

TABLE FILE VIDEOTRK


HEADING
"FORMAT Btdo"
""
"Blank-delimited (B)"
"Abbreviated month name, default case (t)"
"Zero-suppress day number, end with suffix (do)"
"English"
""
SUM OUT2A AS '' OUT2B AS '' TRANSDATE NOPRINT
ON TABLE SET PAGE-NUM OFF
END

400
12. Date Functions

The output is:

The following request prints a blank delimited date, with an abbreviated month name in
German. Initial zeros in the day number are suppressed, and a period is added to the end of
the number:
DEFINE FILE VIDEOTRK
TRANS1/YYMD=20050104;
TRANS2/YYMD=20050302;

DATEW/W=TRANS1 ;
DATEW2/W=TRANS2 ;
DATEYYMD/YYMDW=TRANS1 ;
DATEYYMD2/YYMDW=TRANS2 ;

OUT3A/A12=DATETRAN(DATEYYMD, '(DMYY)', '(Btdp)', 'DE', 12, 'A12');


OUT3B/A12=DATETRAN(DATEYYMD2, '(DMYY)', '(Btdp)', 'DE', 12, 'A12');
END

TABLE FILE VIDEOTRK


HEADING
"FORMAT Btdp"
""
"Blank-delimited (B)"
"Abbreviated month name, default case (t)"
"Zero-suppress day number, end with period (dp)"
"German"
""
SUM OUT3A AS '' OUT3B AS '' TRANSDATE NOPRINT
ON TABLE SET PAGE-NUM OFF
END

Using Functions 401


DATETRAN: Formatting Dates in International Formats

The output is:

The following request prints a blank delimited date in French, with a full day name at the
beginning and a full month name, in lowercase (the default for French):
DEFINE FILE VIDEOTRK
TRANS1/YYMD=20050104;
TRANS2/YYMD=20050302;

DATEW/W=TRANS1 ;
DATEW2/W=TRANS2 ;
DATEYYMD/YYMDW=TRANS1 ;
DATEYYMD2/YYMDW=TRANS2 ;

OUT4A/A30 = DATETRAN(DATEYYMD, '(DMYY)', '(Bwrtr)', 'FR', 30, 'A30');


OUT4B/A30 = DATETRAN(DATEYYMD2, '(DMYY)', '(Bwrtr)', 'FR', 30, 'A30');
END

TABLE FILE VIDEOTRK


HEADING
"FORMAT Bwrtr"
""
"Blank-delimited (B)"
"Full day of week name at beginning of date, default case (wr)"
"Full month name, default case (tr)"
"English"
""
SUM OUT4A AS '' OUT4B AS '' TRANSDATE NOPRINT
ON TABLE SET PAGE-NUM OFF
END

402
12. Date Functions

The output is:

The following request prints a blank delimited date in Spanish with a full day name at the
beginning in lowercase (the default for Spanish), followed by a comma, and with the word “de”
between the day number and month and between the month and year:
DEFINE FILE VIDEOTRK
TRANS1/YYMD=20050104;
TRANS2/YYMD=20050302;

DATEW/W=TRANS1 ;
DATEW2/W=TRANS2 ;
DATEYYMD/YYMDW=TRANS1 ;
DATEYYMD2/YYMDW=TRANS2 ;

OUT5A/A30=DATETRAN(DATEYYMD, '(DMYY)', '(Bwrctrde)', 'ES', 30, 'A30');


OUT5B/A30=DATETRAN(DATEYYMD2, '(DMYY)', '(Bwrctrde)', 'ES', 30, 'A30');
END

TABLE FILE VIDEOTRK


HEADING
"FORMAT Bwrctrde"
""
"Blank-delimited (B)"
"Full day of week name at beginning of date, default case (wr)"
"Comma after day name (c)"
"Full month name, default case (tr)"
"Zero-suppress day number (d)"
"de between day and month and between month and year (e)"
"Spanish"
""
SUM OUT5A AS '' OUT5B AS '' TRANSDATE NOPRINT
ON TABLE SET PAGE-NUM OFF
END

Using Functions 403


DATETRAN: Formatting Dates in International Formats

The output is:

The following request prints a date in Japanese characters with a full month name at the
beginning, in the default case and with zero suppression:
DEFINE FILE VIDEOTRK
TRANS1/YYMD=20050104;
TRANS2/YYMD=20050302;

DATEW/W=TRANS1 ;
DATEW2/W=TRANS2 ;
DATEYYMD/YYMDW=TRANS1 ;
DATEYYMD2/YYMDW=TRANS2 ;

OUT6A/A30=DATETRAN(DATEYYMD , '(YYMD)', '(Ktrd)', 'JA', 30, 'A30');


OUT6B/A30=DATETRAN(DATEYYMD2, '(YYMD)', '(Ktrd)', 'JA', 30, 'A30');
END

TABLE FILE VIDEOTRK


HEADING
"FORMAT Ktrd"
""
"Japanese characters (K in conjunction with the language code JA)"
"Full month name at beginning of date, default case (tr)"
"Zero-suppress day number (d)"
"Japanese"
""
SUM OUT6A AS '' OUT6B AS '' TRANSDATE NOPRINT
ON TABLE SET PAGE-NUM OFF
END

404
12. Date Functions

The output is:

The following request prints a blank delimited date in Greek with a full day name at the
beginning in the default case, followed by a comma, and with a full month name in the default
case:
DEFINE FILE VIDEOTRK
TRANS1/YYMD=20050104;
TRANS2/YYMD=20050302;

DATEW/W=TRANS1 ;
DATEW2/W=TRANS2 ;
DATEYYMD/YYMDW=TRANS1 ;
DATEYYMD2/YYMDW=TRANS2 ;

OUT7A/A30=DATETRAN(DATEYYMD , '(DMYY)', '(Bwrctr)', 'GR', 30, 'A30');


OUT7B/A30=DATETRAN(DATEYYMD2, '(DMYY)', '(Bwrctr)', 'GR', 30, 'A30');
END

TABLE FILE VIDEOTRK


HEADING
"FORMAT Bwrctrde"
""
"Blank-delimited (B)"
"Full day of week name at beginning of date, default case (wr)"
"Comma after day name (c)"
"Full month name, default case (tr)"
"Greek"
""
SUM OUT7A AS '' OUT7B AS '' TRANSDATE NOPRINT
ON TABLE SET PAGE-NUM OFF
END

Using Functions 405


DPART: Extracting a Component From a Date

The output is:

DPART: Extracting a Component From a Date


The DPART function extracts a specified component from a date field and returns it in numeric
format.

Since Dialogue Manager interprets a date as alphanumeric or numeric, and DPART requires a
standard date stored as an offset from the base date, do not use DPART with Dialogue
Manager unless you first convert the variable used as the input date to an offset from the base
date.

Available Languages: reporting, Maintain

Syntax: How to Extract a Date Component and Return It in Integer Format

DPART (datevalue, 'component', output)

where:
datevalue
Date

Is a full component date.


component
Alphanumeric

Is the name of the component to be retrieved enclosed in single quotation marks. Valid
values are:

For year: YEAR, YY

406
12. Date Functions

For month: MONTH, MM

For day: DAY, For day of month: DAY-OF-MONTH, DD.

For weekday: WEEKDAY, WW.

For quarter: QUARTER, QQ


output
Integer

Is the field that contains the result, or the integer format of the output value enclosed in
single quotation marks.

Example: Extracting Date Components in Integer Format


The following request against the VIDEOTRK data source uses the DPART function to extract
the year, month, and day component from the TRANSDATE field:

DEFINE FILE
VIDEOTRK
YEAR/I4 = DPART(TRANSDATE, 'YEAR', 'I11');
MONTH/I4 = DPART(TRANSDATE, 'MM', 'I11');
DAY/I4 = DPART(TRANSDATE, 'DAY', 'I11');
END

TABLE FILE VIDEOTRK


PRINT TRANSDATE YEAR MONTH DAY
BY LASTNAME BY FIRSTNAME
WHERE LASTNAME LT 'DIAZ'
END

The output is:

LASTNAME FIRSTNAME TRANSDATE YEAR MONTH DAY


-------- --------- --------- ---- ----- ---
ANDREWS NATALIA 91/06/19 1991 6 19
91/06/18 1991 6 18
BAKER MARIE 91/06/19 1991 6 19
91/06/17 1991 6 17
BERTAL MARCIA 91/06/23 1991 6 23
91/06/18 1991 6 18
CHANG ROBERT 91/06/28 1991 6 28
91/06/27 1991 6 27
91/06/26 1991 6 26
COLE ALLISON 91/06/24 1991 6 24
91/06/23 1991 6 23
CRUZ IVY 91/06/27 1991 6 27
DAVIS JASON 91/06/24 1991 6 24

Using Functions 407


FIQTR: Obtaining the Financial Quarter

FIQTR: Obtaining the Financial Quarter


The FIQTR function returns the financial quarter corresponding to a given calendar date based
on the financial year starting date and the financial year numbering convention.

Since Dialogue Manager interprets a date as alphanumeric or numeric, and FIQTR requires a
standard date stored as an offset from the base date, do not use FIQTR with Dialogue
Manager unless you first convert the variable used as the input date to an offset from the base
date.

Syntax: How to Obtain the Financial Quarter

FIQTR(inputdate, lowcomponent, startmonth, startday, yrnumbering, output)

where:
inputdate
Date

Is the date for which the financial year is returned. The date must be a standard date
stored as an offset from the base date.

If the financial year does not begin on the first day of a month, the date must have Y(Y), M,
and D components, or Y(Y) and JUL components (note that JUL is equivalent to YJUL).
Otherwise, the date only needs Y(Y) and M components or Y(Y) and Q components.
lowcomponent
Alphanumeric

Is one of the following:

D if the date contains a D or JUL component.

M if the date contains an M component, but no D component.

Q if the date contains a Q component.

startmonth
Numeric

1 through 12 are used to represent the starting month of the financial year, where 1
represents January and 12 represents December. If the low component is Q, the start
month must be 1, 4, 7, or 10.
startday
Numeric

408
12. Date Functions

Is the starting day of the starting month, usually 1. If the low component is M or Q, 1 is
required.
yrnumbering
Alphanumeric

Valid values are:

FYE to specify the Financial Year Ending convention. The financial year number is the
calendar year of the ending date of the financial year. For example, when the financial year
starts on October 1, 2008, the date 2008 November 1 is in FY 2009 Q1 because that
date is in the financial year that ends on 2009 September 30.

FYS to specify the Financial Year Starting convention. The financial year number is the
calendar year of the starting date of the financial year. For example, when the financial
year starts on April 6, 2008, the date 2008 July 6 is in FY 2008 Q2 because that date is
in the financial year that starts on 2008 April 6.
output
I or Q

The result will be in integer format, or Q. This function will return a value of 1 through 4. In
case of an error, zero is returned.

Note: February 29 cannot be used as a start day for a financial year.

Example: Obtaining the Financial Quarter


The following request against the CENTHR data source obtains the financial quarter
corresponding to an employee starting date (field START_DATE, format YYMD) and returns the
values in each of the supported formats: Q and I1.
DEFINE FILE CENTHR
FISCALQ/Q=FIQTR(START_DATE,'D',10,1,'FYE',FISCALQ);
FISCALI/I1=FIQTR(START_DATE,'D',10,1,'FYE',FISCALI);
END
TABLE FILE CENTHR
PRINT START_DATE FISCALQ FISCALI
BY LNAME BY FNAME
WHERE LNAME LIKE 'C%'
END

Using Functions 409


FIYR: Obtaining the Financial Year

On the output, note that the date November 12, 1998 (1998/11/12) is in fiscal quarter Q1
because the starting month is October (10):

Last First Starting


Name Name Date FISCALQ FISCALI
---- ----- -------- ------- -------
CHARNEY ROSS 1998/09/12 Q4 4
CHIEN CHRISTINE 1997/10/01 Q1 1
CLEVELAND PHILIP 1996/07/30 Q4 4
CLINE STEPHEN 1998/11/12 Q1 1
COHEN DANIEL 1997/10/05 Q1 1
CORRIVEAU RAYMOND 1997/12/05 Q1 1
COSSMAN MARK 1996/12/19 Q1 1
CRONIN CHRIS 1996/12/03 Q1 1
CROWDER WESLEY 1996/09/17 Q4 4
CULLEN DENNIS 1995/09/05 Q4 4
CUMMINGS JAMES 1993/07/11 Q4 4
CUTLIP GREGG 1997/03/26 Q2 2

FIYR: Obtaining the Financial Year


The FIYR function returns the financial year, also known as the fiscal year, corresponding to a
given calendar date based on the financial year starting date and the financial year numbering
convention.

Since Dialogue Manager interprets a date as alphanumeric or numeric, and FIYR requires a
standard date stored as an offset from the base date, do not use FIYR with Dialogue Manager
unless you first convert the variable used as the input date to an offset from the base date.

Syntax: How to Obtain the Financial Year

FIYR(inputdate, lowcomponent, startmonth, startday, yrnumbering, output)

where:
inputdate
Date

Is the date for which the financial year is returned. The date must be a standard date
stored as an offset from the base date.

If the financial year does not begin on the first day of a month, the date must have Y(Y), M,
and D components, or Y(Y) and JUL components (note that JUL is equivalent to YJUL).
Otherwise, the date only needs Y(Y) and M components or Y(Y) and Q components.
lowcomponent
Alphanumeric

410
12. Date Functions

Is one of the following:

D if the date contains a D or JUL component.

M if the date contains an M component, but no D component.

Q if the date contains a Q component.

startmonth
Numeric

1 through 12 are used to represent the starting month of the financial year, where 1
represents January and 12 represents December. If the low component is Q, the start
month must be 1, 4, 7, or 10.
startday
Numeric

Is the starting day of the starting month, usually 1. If the low component is M or Q, 1 is
required.
yrnumbering
Alphanumeric

Valid values are:

FYE to specify the Financial Year Ending convention. The financial year number is the
calendar year of the ending date of the financial year. For example, when the financial year
starts on October 1, 2008, the date 2008 November 1 is in FY 2009 Q1 because that
date is in the financial year that ends on 2009 September 30.

FYS to specify the Financial Year Starting convention. The financial year number is the
calendar year of the starting date of the financial year. For example, when the financial
year starts on April 6, 2008, the date 2008 July 6 is in FY 2008 Q2 because that date is
in the financial year that starts on 2008 April 6.
output
I, Y, or YY

The result will be in integer format, or Y or YY. This function returns a year value. In case
of an error, zero is returned.

Note: February 29 cannot be used as a start day for a financial year.

Using Functions 411


FIYYQ: Converting a Calendar Date to a Financial Date

Example: Obtaining the Financial Year


The following request against the CENTSTMT data source obtains the financial year
corresponding to an account period (field PERIOD, format YYM) and returns the values in each
of the supported formats: Y, YY, and I4.
DEFINE FILE CENTSTMT
FISCALYY/YY=FIYR(PERIOD,'M', 4,1,'FYE',FISCALYY);
FISCALY/Y=FIYR(PERIOD,'M', 4,1,'FYE',FISCALY);
FISCALI/I4=FIYR(PERIOD,'M', 4,1,'FYE',FISCALI);
END
TABLE FILE CENTSTMT
PRINT PERIOD FISCALYY FISCALY FISCALI
BY GL_ACCOUNT
WHERE GL_ACCOUNT LT '2100'
END

On the output, note that the period April 2002 (2002/04) is in fiscal year 2003 because the
starting month is April (4), and the FYE numbering convention is used:
Ledger
Account PERIOD FISCALYY FISCALY FISCALI
------- ------ -------- ------- -------
1000 2002/01 2002 02 2002
2002/02 2002 02 2002
2002/03 2002 02 2002
2002/04 2003 03 2003
2002/05 2003 03 2003
2002/06 2003 03 2003
2000 2002/01 2002 02 2002
2002/02 2002 02 2002
2002/03 2002 02 2002
2002/04 2003 03 2003
2002/05 2003 03 2003
2002/06 2003 03 2003

FIYYQ: Converting a Calendar Date to a Financial Date


The FIYYQ function returns a financial date containing both the financial year and quarter that
corresponds to a given calendar date. The returned financial date is based on the financial
year starting date and the financial year numbering convention.

Since Dialogue Manager interprets a date as alphanumeric or numeric, and FIYYQ requires a
standard date stored as an offset from the base date, do not use FIYYQ with Dialogue
Manager unless you first convert the variable used as the input date to an offset from the base
date.

412
12. Date Functions

Syntax: How to Convert a Calendar Date to a Financial Date

FIYYQ(inputdate, lowcomponent, startmonth, startday, yrnumbering, output)

where:
inputdate
Date

Is the date for which the financial year is returned. The date must be a standard date
stored as an offset from the base date.

If the financial year does not begin on the first day of a month, the date must have Y(Y), M,
and D components, or Y(Y) and JUL components (note that JUL is equivalent to YJUL).
Otherwise, the date only needs Y(Y) and M components or Y(Y) and Q components.
lowcomponent
Alphanumeric

Is one of the following:

D if the date contains a D or JUL component.

M if the date contains an M component, but no D component.

Q if the date contains a Q component.

startmonth
Numeric

1 through 12 are used to represent the starting month of the financial year, where 1
represents January and 12 represents December. If the low component is Q, the start
month must be 1, 4, 7, or 10.
startday
Numeric

Is the starting day of the starting month, usually 1. If the low component is M or Q, 1 is
required.
yrnumbering
Alphanumeric

Valid values are:

Using Functions 413


FIYYQ: Converting a Calendar Date to a Financial Date

FYE to specify the Financial Year Ending convention. The financial year number is the
calendar year of the ending date of the financial year. For example, when the financial year
starts on October 1, 2008, the date 2008 November 1 is in FY 2009 Q1 because that
date is in the financial year that ends on 2009 September 30.

FYS to specify the Financial Year Starting convention. The financial year number is the
calendar year of the starting date of the financial year. For example, when the financial
year starts on April 6, 2008, the date 2008 July 6 is in FY 2008 Q2 because that date is
in the financial year that starts on 2008 April 6.
output
Y[Y]Q or QY[Y]

In case of an error, zero is returned.

Note: February 29 cannot be used as a start day for a financial year.

Example: Converting a Calendar Date to a Financial Date


The following request against the CENTHR data source converts each employee starting date
(field START_DATE, format YYMD) to a financial date containing year and quarter components
in all the supported formats: YQ, YYQ, QY, and QYY.
DEFINE FILE CENTHR
FISYQ/YQ=FIYYQ(START_DATE,'D',10,1,'FYE',FISYQ);
FISYYQ/YYQ=FIYYQ(START_DATE,'D',10,1,'FYE',FISYYQ);
FISQY/QY=FIYYQ(START_DATE,'D',10,1,'FYE',FISQY);
FISQYY/QYY=FIYYQ(START_DATE,'D',10,1,'FYE',FISQYY);
END
TABLE FILE CENTHR
PRINT START_DATE FISYQ FISYYQ FISQY FISQYY
BY LNAME BY FNAME
WHERE LNAME LIKE 'C%'
END

414
12. Date Functions

On the output, note that the date November 12, 1998 (1998/11/12) is converted to Q1 1999
because the starting month is October (10), and the FYE numbering convention is used:

Last First Starting


Name Name Date FISYQ FISYYQ FISQY FISQYY
---- ----- -------- ----- ------ ----- ------
CHARNEY ROSS 1998/09/12 98 Q4 1998 Q4 Q4 98 Q4 1998
CHIEN CHRISTINE 1997/10/01 98 Q1 1998 Q1 Q1 98 Q1 1998
CLEVELAND PHILIP 1996/07/30 96 Q4 1996 Q4 Q4 96 Q4 1996
CLINE STEPHEN 1998/11/12 99 Q1 1999 Q1 Q1 99 Q1 1999
COHEN DANIEL 1997/10/05 98 Q1 1998 Q1 Q1 98 Q1 1998
CORRIVEAU RAYMOND 1997/12/05 98 Q1 1998 Q1 Q1 98 Q1 1998
COSSMAN MARK 1996/12/19 97 Q1 1997 Q1 Q1 97 Q1 1997
CRONIN CHRIS 1996/12/03 97 Q1 1997 Q1 Q1 97 Q1 1997
CROWDER WESLEY 1996/09/17 96 Q4 1996 Q4 Q4 96 Q4 1996
CULLEN DENNIS 1995/09/05 95 Q4 1995 Q4 Q4 95 Q4 1995
CUMMINGS JAMES 1993/07/11 93 Q4 1993 Q4 Q4 93 Q4 1993
CUTLIP GREGG 1997/03/26 97 Q2 1997 Q2 Q2 97 Q2 1997

TODAY: Returning the Current Date


Available Languages: reporting

The TODAY function retrieves the current date from the operating system in the format
MM/DD/YY or MM/DD/YYYY. It always returns a date that is current. Therefore, if you are
running an application late at night, use TODAY. You can remove the default embedded
slashes with the EDIT function.

You can also retrieve the date in the same format (separated by slashes) using the Dialogue
Manager system variable &DATE. You can retrieve the date without the slashes using the
system variables &YMD, &MDY, and &DMY. The system variable &DATEfmt retrieves the date
in a specified format.

Syntax: How to Retrieve the Current Date

TODAY(output)

where:
output
Alphanumeric, at least A8

Is the name of the field that contains the result, or the format of the output value enclosed
in single quotation marks.

The following apply:

If the format is A8 or A9, TODAY returns the 2-digit year.

Using Functions 415


Using Legacy Date Functions

If the format is A10 or greater, TODAY returns the 4-digit year.

Example: Retrieving the Current Date


TODAY retrieves the current date and stores it in the DATE field. The request then displays the
date in the page heading.

DEFINE FILE EMPLOYEE


DATE/A10 WITH EMP_ID = TODAY(DATE);
END

TABLE FILE EMPLOYEE


SUM CURR_SAL BY DEPARTMENT
HEADING
"PAGE <TABPAGENO "
"SALARY REPORT RUN ON <DATE "
END

The output is:


SALARY REPORT RUN ON 12/13/2006
DEPARTMENT CURR_SAL
---------- --------
MIS $108,002.00
PRODUCTION $114,282.00

Using Legacy Date Functions


The legacy date functions were created for use with dates in integer, packed decimal, or
alphanumeric format.

For detailed information on each legacy date function, see:

AYM: Adding or Subtracting Months on page 419

AYMD: Adding or Subtracting Days on page 420

CHGDAT: Changing How a Date String Displays on page 421

DA Functions: Converting a Legacy Date to an Integer on page 424

DMY, MDY, YMD: Calculating the Difference Between Two Dates on page 426

DOWK and DOWKL: Finding the Day of the Week on page 427

DT Functions: Converting an Integer to a Date on page 428

GREGDT: Converting From Julian to Gregorian Format on page 430

JULDAT: Converting From Gregorian to Julian Format on page 432

416
12. Date Functions

YM: Calculating Elapsed Months on page 433

Using Old Versions of Legacy Date Functions


The functions described in this section are legacy date functions. They were created for use
with dates in integer or alphanumeric format. They are no longer recommended for date
manipulation. Standard date and date-time functions are preferred.

All legacy date functions support dates for the year 2000 and later.

Using Dates With Two- and Four-Digit Years


Legacy date functions accept dates with two- or four-digit years. Four-digit years that display
the century, such as 2000 or 1900, can be used if their formats are specified as I8YYMD,
P8YYMD, D8YYMD, F8YYMD, or A8YYMD. Two-digit years can use the DEFCENT and
YRTHRESH parameters to assign century values if the field has a length of six (for example,
I6YMD). For information on these parameters, see Customizing Your Environment in Developing
Reporting Applications.

Example: Using Four-Digit Years


The EDIT function creates dates with four-digit years. The functions JULDAT and GREGDT then
convert these dates to Julian and Gregorian formats.
DEFINE FILE EMPLOYEE
DATE/I8YYMD = EDIT('19'|EDIT(HIRE_DATE));
JDATE/I7 = JULDAT(DATE, 'I7');
GDATE/I8 = GREGDT(JDATE, 'I8');
END
TABLE FILE EMPLOYEE
PRINT DATE JDATE GDATE
END

Using Functions 417


Using Legacy Date Functions

The output is:


DATE JDATE GDATE
---- ----- -----
1980/06/02 1980154 19800602
1981/07/01 1981182 19810701
1982/05/01 1982121 19820501
1982/01/04 1982004 19820104
1982/08/01 1982213 19820801
1982/01/04 1982004 19820104
1982/07/01 1982182 19820701
1981/07/01 1981182 19810701
1982/04/01 1982091 19820401
1982/02/02 1982033 19820202
1982/04/01 1982091 19820401
1981/11/02 1981306 19811102
1982/04/01 1982091 19820401
1982/05/15 1982135 19820515

Example: Using Two-Digit Years


The AYMD function returns an eight-digit date when the input argument has a six-digit legacy
date format. Since DEFCENT is 19 and YRTHRESH is 83, year values from 83 through 99 are
interpreted as 1983 through 1999, and year values from 00 through 82 are interpreted as
2000 through 2082.

SET DEFCENT=19, YRTHRESH=83

DEFINE FILE EMPLOYEE


NEW_DATE/I8YYMD = AYMD(EFFECT_DATE, 30, 'I8');
END

TABLE FILE EMPLOYEE


PRINT EFFECT_DATE NEW_DATE BY EMP_ID
END

The output is:


EMP_ID EFFECT_DATE NEW_DATE
------ ----------- --------
071382660
112847612
117593129 82/11/01 2082/12/01
119265415
119329144 83/01/01 1983/01/31
123764317 83/03/01 1983/03/31
126724188
219984371
326179357 82/12/01 2082/12/31
451123478 84/09/01 1984/10/01
543729165
818692173 83/05/01 1983/05/31

418
12. Date Functions

AYM: Adding or Subtracting Months


Available Languages: reporting, Maintain

The AYM function adds months to or subtracts months from a date in year-month format. You
can convert a date to this format using the CHGDAT or EDIT function.

Syntax: How to Add or Subtract Months to or From a Date

AYM(indate, months, output)

where:
indate
I4, I4YM, I6, or I6YYM

Is the legacy date in year-month format, the name of a field that contains the date, or an
expression that returns the date. If the date is not valid, the function returns the value 0
(zero).
months
Integer

Is the number of months you are adding to or subtracting from the date. To subtract
months, use a negative number.
output
I4YM or I6YYM

Is the resulting legacy date. Is the name of the field that contains the result, or the format
of the output value enclosed in single quotation marks.

Tip: If the input date is in integer year-month-day format (I6YMD or I8YYMD), divide the
date by 100 to convert to year-month format and set the result to an integer. This drops
the day portion of the date, which is now after the decimal point.

Using Functions 419


AYMD: Adding or Subtracting Days

Example: Adding Months to a Date


The COMPUTE command converts the dates in HIRE_DATE from year-month-day to year-month
format and stores the result in HIRE_MONTH. AYM then adds six months to HIRE_MONTH and
stores the result in AFTER6MONTHS:

TABLE FILE EMPLOYEE


PRINT HIRE_DATE AND COMPUTE
HIRE_MONTH/I4YM = HIRE_DATE/100 ;
AFTER6MONTHS/I4YM = AYM(HIRE_MONTH, 6, AFTER6MONTHS);
BY LAST_NAME BY FIRST_NAME
WHERE DEPARTMENT EQ 'MIS';
END

The output is:


LAST_NAME FIRST_NAME HIRE_DATE HIRE_MONTH AFTER6MONTHS
--------- ---------- --------- ---------- ------------
BLACKWOOD ROSEMARIE 82/04/01 82/04 82/10
CROSS BARBARA 81/11/02 81/11 82/05
GREENSPAN MARY 82/04/01 82/04 82/10
JONES DIANE 82/05/01 82/05 82/11
MCCOY JOHN 81/07/01 81/07 82/01
SMITH MARY 81/07/01 81/07 82/01

AYMD: Adding or Subtracting Days


Available Languages: reporting, Maintain

The AYMD function adds days to or subtracts days from a date in year-month-day format. You
can convert a date to this format using the CHGDAT or EDIT function.

Syntax: How to Add or Subtract Days to or From a Date

AYMD(indate, days, output)

where:
indate
I6, I6YMD, I8, I8YYMD

Is the legacy date in year-month-day format. If the date is not valid, the function returns the
value 0 (zero).
days
Integer

Is the number of days you are adding to or subtracting from indate. To subtract days, use a
negative number.

420
12. Date Functions

output
I6, I6YMD, I8, or I8YYMD

Is the name of the field that contains the result, or the format of the output value enclosed
in single quotation marks. If indate is a field, output must have the same format.

If the addition or subtraction of days crosses forward or backward into another century, the
century digits of the output year are adjusted.

Example: Adding Days to a Date


AYMD adds 35 days to each value in the HIRE_DATE field, and stores the result in
AFTER35DAYS:

TABLE FILE EMPLOYEE


PRINT HIRE_DATE AND COMPUTE
AFTER35DAYS/I6YMD = AYMD(HIRE_DATE, 35, AFTER35DAYS);
BY LAST_NAME BY FIRST_NAME
WHERE DEPARTMENT EQ 'PRODUCTION';
END

The output is:


LAST_NAME FIRST_NAME HIRE_DATE AFTER35DAYS
--------- ---------- --------- -----------
BANNING JOHN 82/08/01 82/09/05
IRVING JOAN 82/01/04 82/02/08
MCKNIGHT ROGER 82/02/02 82/03/09
ROMANS ANTHONY 82/07/01 82/08/05
SMITH RICHARD 82/01/04 82/02/08
STEVENS ALFRED 80/06/02 80/07/07

CHGDAT: Changing How a Date String Displays


Available Languages: reporting, Maintain

The CHGDAT function rearranges the year, month, and day portions of an input character string
representing a date. It may also convert the input string from long to short or short to long date
representation. Long representation contains all three date components: year, month, and day;
short representation omits one or two of the date components, such as year, month, or day.
The input and output date strings are described by display options that specify both the order
of date components (year, month, day) in the date string and whether two or four digits are
used for the year (for example, 04 or 2004). CHGDAT reads an input date character string and
creates an output date character string that represents the same date in a different way.

Note: CHGDAT requires a date character string as input, not a date itself. Whether the input is
a standard or legacy date, convert it to a date character string (using the EDIT or DATECVT
functions, for example) before applying CHGDAT.

Using Functions 421


CHGDAT: Changing How a Date String Displays

The order of date components in the date character string is described by display options
comprised of the following characters in your chosen order:

Character Description

D Day of the month (01 through 31).

M Month of the year (01 through 12).

Y[Y] Year. Y indicates a two-digit year (such as 94); YY indicates


a four-digit year (such as 1994).

To spell out the month rather than use a number in the resulting string, append one of the
following characters to the display options for the resulting string:

Character Description

T Displays the month as a three-letter abbreviation.

X Displays the full name of the month.

Display options can consist of up to five display characters. Characters other than those
display options are ignored.

For example: The display options 'DMYY' specify that the date string starts with a two digit
day, then two digit month, then four digit year.

Note: Display options are not date formats.

Reference: Short to Long Conversion


If you are converting a date from short to long representation (for example, from year-month to
year-month-day), the function supplies the portion of the date missing in the short
representation, as shown in the following table:

Portion of Date Missing Portion Supplied by Function

Day (for example, from YM to YMD) Last day of the month.

Month (for example, from Y to YM) Last month of the year (December).

422
12. Date Functions

Portion of Date Missing Portion Supplied by Function

Year (for example, from MD to YMD) The year 99.

Converting year from two-digit to four-digit The century will be determined by the 100-year
(for example, from YMD to YYMD) window defined by DEFCENT and YRTHRESH.

See Customizing Your Environment in


Developing Reporting Applications or Working
With Cross-Century Dates in the iBase archive
for details on DEFCENT and YRTHRESH.

Syntax: How to Change the Date Display String

CHGDAT('in_display_options','out_display_options',date_string,output)

where:
'in_display_options'

A1 to A5

Is a series of up to five display options that describe the layout of date_string. These
options can be stored in an alphanumeric field or supplied as a literal enclosed in single
quotation marks.
'out_display_options'

A1 to A5

Is a series of up to five display options that describe the layout of the converted date
string. These options can be stored in an alphanumeric field or supplied as a literal
enclosed in single quotation marks.
date_string
A2 to A8

Is the input date character string with date components in the order specified by
in_display_options.

Note that if the original date is in numeric format, you must convert it to a date character
string. If date_string does not correctly represent the date (the date is invalid), the function
returns blank spaces.

Using Functions 423


DA Functions: Converting a Legacy Date to an Integer

output
Axx, where xx is a number of characters large enough to fit the date string specified by
out_display_options. A17 is long enough to fit the longest date string.

Is the name of the field that contains the result, or the format of the output value enclosed
in single quotation marks.

Note: Since CHGDAT uses a date string (as opposed to a date) and returns a date string with
up to 17 characters, use the EDIT or DATECVT functions or any other means to convert the
date to or from a date character string.

Example: Converting the Date Display From YMD to MDYYX


The EDIT function changes HIRE_DATE from numeric to alphanumeric format. CHGDAT then
converts each value in ALPHA_HIRE from displaying the components as YMD to MDYYX and
stores the result in HIRE_MDY, which has the format A17. The option X in the output value
displays the full name of the month.

TABLE FILE EMPLOYEE


PRINT HIRE_DATE AND COMPUTE
ALPHA_HIRE/A17 = EDIT(HIRE_DATE); NOPRINT AND COMPUTE
HIRE_MDY/A17 = CHGDAT('YMD', 'MDYYX', ALPHA_HIRE, 'A17');
BY LAST_NAME BY FIRST_NAME
WHERE DEPARTMENT EQ 'PRODUCTION';
END

The output is:


LAST_NAME FIRST_NAME HIRE_DATE HIRE_MDY
--------- ---------- --------- --------
BANNING JOHN 82/08/01 AUGUST 01 1982
IRVING JOAN 82/01/04 JANUARY 04 1982
MCKNIGHT ROGER 82/02/02 FEBRUARY 02 1982
ROMANS ANTHONY 82/07/01 JULY 01 1982
SMITH RICHARD 82/01/04 JANUARY 04 1982
STEVENS ALFRED 80/06/02 JUNE 02 1980

DA Functions: Converting a Legacy Date to an Integer


Available Languages: reporting, Maintain

The DA functions convert a legacy date to the number of days between it and a base date
(December 31, 1899). By converting a date to the number of days, you can add and subtract
dates and calculate the intervals between them, or you can add to or subtract numbers from
the dates to get new dates.

There are six DA functions; each one accepts a date in a different format.

424
12. Date Functions

Syntax: How to Convert a Date to an Integer

function(indate, output)

where:
function
Is one of the following:

DADMY converts a date in day-month-year format.

DADYM converts a date in day-year-month format.

DAMDY converts a date in month-day-year format.

DAMYD converts a date in month-year-day format.

DAYDM converts a date in year-day-month format.

DAYMD converts a date in year-month-day format.

indate
I6xxx or P6xxx, where xxx corresponds to the function DAxxx you are using.

Is the legacy date to be converted, or the name of a field that contains the date. The date
is truncated to an integer before conversion. If indate is a numeric literal, enter only the
last two digits of the year; the function assumes the century component. If the date is
invalid, the function returns a 0.
output
Integer

Is the name of the field that contains the result, or the format of the output value enclosed
in single quotation marks. The format of the date returned depends on the function.

Example: Converting Dates and Calculating the Difference Between Them


DAYMD converts the DAT_INC and HIRE_DATE fields to the number of days since December
31, 1899, and the smaller number is then subtracted from the larger number:
TABLE FILE EMPLOYEE
PRINT DAT_INC AS 'RAISE DATE' AND COMPUTE
DAYS_HIRED/I8 = DAYMD(DAT_INC, 'I8') - DAYMD(HIRE_DATE, 'I8');
BY LAST_NAME BY FIRST_NAME
IF DAYS_HIRED NE 0
WHERE DEPARTMENT EQ 'PRODUCTION';
END

Using Functions 425


DMY, MDY, YMD: Calculating the Difference Between Two Dates

The output is:


LAST_NAME FIRST_NAME RAISE DATE DAYS_HIRED
--------- ---------- ---------- ----------
IRVING JOAN 82/05/14 130
MCKNIGHT ROGER 82/05/14 101
SMITH RICHARD 82/05/14 130
STEVENS ALFRED 82/01/01 578
81/01/01 213

DMY, MDY, YMD: Calculating the Difference Between Two Dates


Available Languages: reporting, Maintain

The DMY, MDY, and YMD functions calculate the difference between two legacy dates in
integer, alphanumeric, or packed format.

Syntax: How to Calculate the Difference Between Two Dates

function(from_date, to_date)

where:
function
Is one of the following:

DMY calculates the difference between two dates in day-month-year format.

MDY calculates the difference between two dates in month-day-year format.

YMD calculates the difference between two dates in year-month-day format.

from_date
I, P, or A format with date display options.
Is the beginning legacy date, or the name of a field that contains the date.
to_date
I, P, or A format with date display options.I6xxx or I8xxx where xxx corresponds to the
specified function (DMY, YMD, or MDY).
Is the end date, or the name of a field that contains the date.

426
12. Date Functions

Example: Calculating the Number of Days Between Two Dates


YMD calculates the number of days between the dates in HIRE_DATE and DAT_INC:
TABLE FILE EMPLOYEE
SUM HIRE_DATE FST.DAT_INC AS 'FIRST PAY,INCREASE' AND COMPUTE
DIFF/I4 = YMD(HIRE_DATE, FST.DAT_INC); AS 'DAYS,BETWEEN'
BY LAST_NAME BY FIRST_NAME
WHERE DEPARTMENT EQ 'MIS';
END

The output is:

LAST_NAME FIRST_NAME HIRE_DATE FIRST DAYS


PAY INCREASE BETWEEN
BLACKWOOD ROSEMARIE 82/04/01 82/04/01 0
CROSS BARBARA 81/11/02 82/04/09 158
GREENSPAN MARY 82/04/01 82/06/11 71
JONES DIANE 82/05/01 82/06/01 31
MCCOY JOHN 81/07/01 82/01/01 184
SMITH MARY 81/07/01 82/01/01 184

DOWK and DOWKL: Finding the Day of the Week


Available Languages: reporting, Maintain

The DOWK and DOWKL functions find the day of the week that corresponds to a date. DOWK
returns the day as a three letter abbreviation; DOWKL displays the full name of the day.

Syntax: How to Find the Day of the Week

{DOWK|DOWKL}(indate, output)

where:
indate
I6YMD or I8YYMD

Is the legacy date in year-month-day format. If the date is not valid, the function returns
spaces. If the date specifies a two digit year and DEFCENT and YRTHRESH values have not
been set, the function assumes the 20th century.
output
DOWK: A4. DOWKL: A12

Is the name of the field that contains the result, or the format of the output value enclosed
in single quotation marks.

Using Functions 427


DT Functions: Converting an Integer to a Date

Example: Finding the Day of the Week


DOWK determines the day of the week that corresponds to the value in the HIRE_DATE field
and stores the result in DATED:
TABLE FILE EMPLOYEE
PRINT EMP_ID AND HIRE_DATE AND COMPUTE
DATED/A4 = DOWK(HIRE_DATE, DATED);
WHERE DEPARTMENT EQ 'PRODUCTION';
END

The output is:


EMP_ID HIRE_DATE DATED
------ --------- -----
071382660 80/06/02 MON
119265415 82/01/04 MON
119329144 82/08/01 SUN
123764317 82/01/04 MON
126724188 82/07/01 THU
451123478 82/02/02 TUE

DT Functions: Converting an Integer to a Date


Available Languages: reporting, Maintain

The DT functions convert an integer representing the number of days elapsed since December
31, 1899 to the corresponding date. They are useful when you are performing arithmetic on a
date converted to the number of days. The DT functions convert the result back to a date.

There are six DT functions; each one converts a number into a date of a different format.

Note: When USERFNS is set to LOCAL, DT functions only display a six-digit date.

428
12. Date Functions

Syntax: How to Convert an Integer to a Date

function(number, output)

where:
function
Is one of the following:

DTDMY converts a number to a day-month-year date.

DTDYM converts a number to a day-year-month date.

DTMDY converts a number to a month-day-year date.

DTMYD converts a number to a month-year-day date.

DTYDM converts a number to a year-day-month date.

DTYMD converts a number to a year-month-day date.

number
Integer

Is the number of days since December 31, 1899. The number is truncated to an integer.
output
I8xxx, where xxx corresponds to the function DTxxx in the above list.

Is the name of the field containing the result or the format of the output value enclosed in
single quotation marks. The output format depends on the function being used.

Example: Converting an Integer to a Date


DTMDY converts the NEWF field (which was converted to the number of days by DAYMD) to the
corresponding date and stores the result in NEW_HIRE_DATE:

Using Functions 429


GREGDT: Converting From Julian to Gregorian Format

-* THIS PROCEDURE CONVERTS HIRE_DATE, WHICH IS IN I6YMD FORMAT,


-* TO A DATE IN I8MDYY FORMAT.
-* FIRST IT USES THE DAYMD FUNCTION TO CONVERT HIRE_DATE
-* TO A NUMBER OF DAYS.
-* THEN IT USES THE DTMDY FUNCTION TO CONVERT THIS NUMBER OF
-* DAYS TO I8MDYY FORMAT
-*
DEFINE FILE EMPLOYEE
NEWF/I8 WITH EMP_ID = DAYMD(HIRE_DATE, NEWF);
NEW_HIRE_DATE/I8MDYY WITH EMP_ID = DTMDY(NEWF, NEW_HIRE_DATE);
END
TABLE FILE EMPLOYEE
PRINT HIRE_DATE NEW_HIRE_DATE
BY FN BY LN
WHERE DEPARTMENT EQ 'MIS'
END

The output is:


FIRST_NAME LAST_NAME HIRE_DATE NEW_HIRE_DATE
---------- --------- --------- -------------
BARBARA CROSS 81/11/02 11/02/1981
DIANE JONES 82/05/01 05/01/1982
JOHN MCCOY 81/07/01 07/01/1981
MARY GREENSPAN 82/04/01 04/01/1982
SMITH 81/07/01 07/01/1981
ROSEMARIE BLACKWOOD 82/04/01 04/01/1982

GREGDT: Converting From Julian to Gregorian Format


Available Languages: reporting, Maintain

The GREGDT function converts a date in Julian format (year-day) to Gregorian format (year-
month-day).

A date in Julian format is a five- or seven-digit number. The first two or four digits are the year;
the last three digits are the number of the day, counting from January 1. For example, January
1, 1999 in Julian format is either 99001 or 1999001; June21, 2004 in Julian format is
2004173.

Reference: Format Options for GREGDT


GREGDT converts a Julian date to either YMD or YYMD format using the DEFCENT and
YRTHRESH parameter settings to determine the century, if required. GREGDT returns a date as
follows:

If the format is I6 or I7, GREGDT returns the date in YMD format.

If the format is I8 or greater, GREGDT returns the date in YYMD format.

430
12. Date Functions

Syntax: How to Convert From Julian to Gregorian Format

GREGDT(indate, output)

where:
indate
I5 or I7

Is the Julian date, which is truncated to an integer before conversion. Each value must be
a five- or seven-digit number after truncation. If the date is invalid, the function returns a 0
(zero).
output
I6, I8, I6YMD, or I8YYMD

Is the name of the field that contains the result, or the format of the output value enclosed
in single quotation marks.

Example: Converting From Julian to Gregorian Format


GREGDT converts the JULIAN field to YYMD (Gregorian) format. It determines the century using
the default DEFCENT and YRTHRESH parameter settings.
TABLE FILE EMPLOYEE
PRINT HIRE_DATE AND
COMPUTE JULIAN/I5 = JULDAT(HIRE_DATE, JULIAN); AND
COMPUTE GREG_DATE/I8 = GREGDT(JULIAN, 'I8');
BY LAST_NAME BY FIRST_NAME
WHERE DEPARTMENT EQ 'PRODUCTION';
END

The output is:


LAST_NAME FIRST_NAME HIRE_DATE JULIAN GREG_DATE
--------- ---------- --------- ------ ---------
BANNING JOHN 82/08/01 82213 19820801
IRVING JOAN 82/01/04 82004 19820104
MCKNIGHT ROGER 82/02/02 82033 19820202
ROMANS ANTHONY 82/07/01 82182 19820701
SMITH RICHARD 82/01/04 82004 19820104
STEVENS ALFRED 80/06/02 80154 19800602

Using Functions 431


JULDAT: Converting From Gregorian to Julian Format

JULDAT: Converting From Gregorian to Julian Format


Available Languages: reporting, Maintain

The JULDAT function converts a date from Gregorian format (year-month-day) to Julian format
(year-day). A date in Julian format is a five- or seven-digit number. The first two or four digits
are the year; the last three digits are the number of the day, counting from January 1. For
example, January 1, 1999 in Julian format is either 99001 or 1999001.

Reference: Format Settings for JULDAT


JULDAT converts a Gregorian date to either YYNNN or YYYYNNN format, using the DEFCENT
and YRTHRESH parameter settings to determine if the century is required.

JULDAT returns dates as follows:

If the format is I6, JULDAT returns the date in YYNNN format.

If the format is I7 or greater, JULDAT returns the date in YYYYNNN format.

Syntax: How to Convert From Gregorian to Julian Format

JULDAT(indate, output)

where:
indate
I6, I8, I6YMD, I8YYMD

Is the legacy date to convert or the name of the field that contains the date in year-month-
day format (YMD or YYMD).
output
I5 or I7

Is the name of the field that contains the result, or the format of the output value enclosed
in single quotation marks.

432
12. Date Functions

Example: Converting From Gregorian to Julian Format


JULDAT converts the HIRE_DATE field to Julian format. It determines the century using the
default DEFCENT and YRTHRESH parameter settings.

TABLE FILE EMPLOYEE


PRINT HIRE_DATE AND COMPUTE
JULIAN/I7 = JULDAT(HIRE_DATE, JULIAN);
BY LAST_NAME BY FIRST_NAME
WHERE DEPARTMENT EQ 'PRODUCTION';
END

The output is:


LAST_NAME FIRST_NAME HIRE_DATE JULIAN
--------- ---------- --------- ------
BANNING JOHN 82/08/01 1982213
IRVING JOAN 82/01/04 1982004
MCKNIGHT ROGER 82/02/02 1982033
ROMANS ANTHONY 82/07/01 1982182
SMITH RICHARD 82/01/04 1982004
STEVENS ALFRED 80/06/02 1980154

YM: Calculating Elapsed Months

Available Languages: reporting, Maintain

The YM function calculates the number of months between two dates. The dates must be in
year-month format. You can convert a date to this format by using the CHGDAT or EDIT
function.

Syntax: How to Calculate Elapsed Months

YM(fromdate, todate, output)

where:
fromdate
I4YM or I6YYM

Is the start date in year-month format (for example, I4YM). If the date is not valid, the
function returns the value 0 (zero).
todate
I4YM or I6YYM

Is the end date in year-month format. If the date is not valid, the function returns the value
0 (zero).

Using Functions 433


YM: Calculating Elapsed Months

output
Integer

Is the name of the field that contains the result, or the format of the output value enclosed
in single quotation marks.

Tip: If fromdate or todate is in integer year-month-day format (I6YMD or I8YYMD), simply


divide by 100 to convert to year-month format and set the result to an integer. This drops
the day portion of the date, which is now after the decimal point.

Example: Calculating Elapsed Months


The COMPUTE commands convert the dates from year-month-day to year-month format; then
YM calculates the difference between the values in the HIRE_DATE/100 and DAT_INC/100
fields:
TABLE FILE EMPLOYEE
PRINT DAT_INC AS 'RAISE DATE' AND COMPUTE
HIRE_MONTH/I4YM = HIRE_DATE/100; NOPRINT AND COMPUTE
MONTH_INC/I4YM = DAT_INC/100; NOPRINT AND COMPUTE
MONTHS_HIRED/I3 = YM(HIRE_MONTH, MONTH_INC, 'I3');
BY LAST_NAME BY FIRST_NAME BY HIRE_DATE
IF MONTHS_HIRED NE 0
WHERE DEPARTMENT EQ 'MIS';
END

The output is:


LAST_NAME FIRST_NAME HIRE_DATE RAISE DATE MONTHS_HIRED
--------- ---------- --------- ---------- ------------
CROSS BARBARA 81/11/02 82/04/09 5
GREENSPAN MARY 82/04/01 82/06/11 2
JONES DIANE 82/05/01 82/06/01 1
MCCOY JOHN 81/07/01 82/01/01 6
SMITH MARY 81/07/01 82/01/01 6

434
Chapter 13 Date-Time Functions
Date-Time functions are for use with timestamps in date-time formats, also known as H
formats. A timestamp value refers to internally stored data capable of holding both date
and time components with an accuracy of up to a nanosecond.
In this chapter:

Using Date-Time Functions HHMS: Converting a Date-Time Value to


a Time Value
HADD: Incrementing a Date-Time Value
HINPUT: Converting an Alphanumeric
HCNVRT: Converting a Date-Time Value
String to a Date-Time Value
to Alphanumeric Format
HMIDNT: Setting the Time Portion of a
HDATE: Converting the Date Portion of a
Date-Time Value to Midnight
Date-Time Value to a Date Format
HMASK: Extracting Date-Time
HDIFF: Finding the Number of Units
Components and Preserving Remaining
Between Two Date-Time Values
Components
HDTTM: Converting a Date Value to a
HNAME: Retrieving a Date-Time
Date-Time Value
Component in Alphanumeric Format
HEXTR: Extracting Components of a
HPART: Retrieving a Date-Time
Date-Time Value and Setting Remaining
Component as a Numeric Value
Components to Zero
HSETPT: Inserting a Component Into a
HGETC: Storing the Current Local Date
Date-Time Value
and Time in a Date-Time Field
HTIME: Converting the Time Portion of a
HGETZ: Storing the Current Coordinated
Date-Time Value to a Number
Universal Time in a Date-Time Field
HTMTOTS or TIMETOTS: Converting a
HHMMSS: Retrieving the Current Time
Time to a Timestamp

HYYWD: Returning the Year and Week


Number From a Date-Time Value

Using Functions 435


Using Date-Time Functions

Using Date-Time Functions


The functions described in this section operate on fields in date-time format (sometimes called
H format).

Date-Time Parameters
The DATEFORMAT parameter specifies the order of the date components for certain types of
date-time values. The WEEKFIRST parameter specifies the first day of the week. The DTSTRICT
parameter determines the extent to which date-time values are checked for validity.

Specifying the Order of Date Components


The DATEFORMAT parameter specifies the order of the date components (month/day/year)
when date-time values are entered in the formatted string and translated string formats
described in Using Date-Time Formats on page 440. It makes the input format of a value
independent of the format of the variable to which it is being assigned.

Syntax: How to Specify the Order of Date Components in a Date-Time Field

SET DATEFORMAT = option

where:
option
Can be one of the following: MDY, DMY, YMD, or MYD. MDY is the default value for the
U.S. English format.

Example: Using the DATEFORMAT Parameter


The following request uses a natural date literal with ambiguous numeric day and month
components (APR 04 05) as input to the HINPUT function:

SET DATEFORMAT = MYD


DEFINE FILE EMPLOYEE
DTFLDYYMD/HYYMDI = HINPUT(9,'APR 04 05', 8, DTFLDYYMD);
END
TABLE FILE EMPLOYEE
SUM CURR_SAL NOPRINT DTFLDYYMD
END

With DATEFORMAT set to MYD, the value is interpreted as April 5, 1904:

DTFLDYYMD
---------
1904-04-05 00:00

436
13. Date-Time Functions

Specifying the First Day of the Week for Use in Date-Time Functions
The WEEKFIRST parameter specifies a day of the week as the start of the week. This is used in
week computations by the HADD, HDIFF, HNAME, HPART, and HYYWD functions. It is also
used by the DTADD, DTDIFF, DTRUNC, and DTPART functions. The default values are different
for these functions, as described in How to Set a Day as the Start of the Week on page 437.
The WEEKFIRST parameter does not change the day of the month that corresponds to each
day of the week, but only specifies which day is considered the start of the week.

The HPART, DTPART, HYYWD, and HNAME subroutines can extract a week number from a date-
time value. To determine a week number, they can use different definitions. For example, ISO
8601 standard week numbering defines the first week of the year as the first week in January
with four or more days. Any preceding days in January belong to week 52 or 53 of the
preceding year. The ISO standard also establishes Monday as the first day of the week.

You specify which type of week numbering to use by setting the WEEKFIRST parameter, as
described in How to Set a Day as the Start of the Week on page 437.

Since the week number returned by HNAME, DTPART, and HPART functions can be in the
current year or the year preceding or following, the week number by itself may not be useful.
The function HYYWD returns both the year and the week for a given date-time value.

Syntax: How to Set a Day as the Start of the Week

SET WEEKFIRST = value

where:
value
Can be:

1 through 7, representing Sunday through Saturday with non-standard week numbering.

Week numbering using these values establishes the first week in January with seven
days as week number 1. Preceding days in January belong to the last week of the
previous year. All weeks have seven days.

ISO1 through ISO7, representing Sunday through Saturday with ISO standard week
numbering.

Note: ISO is a synonym for ISO2.

Week numbering using these values establishes the first week in January with at least
four days as week number 1. Preceding days in January belong to the last week of the
previous year. All weeks have seven days.

Using Functions 437


Using Date-Time Functions

STD1 through STD7, in which the digit 1 (Sunday) through 7 (Saturday) indicates the
starting day of the week.

Note: STD without a digit is equivalent to STD1.

Week numbering using these values is as follows. Week number 1 begins on January 1
and ends on the day preceding the first day of the week. For example, for STD1, the
first week ends on the first Saturday of the year. The first and last week may have
fewer than seven days.

SIMPLE, which establishes January 1 as the start of week 1, January 8 is the start of
week 2, and so on. The first day of the week is, thus, the same as the first day of the
year. The last week (week 53) is either one or two days long.

0 (zero), is the value of the WEEKFIRST setting before the user issues an explicit
WEEKFIRST setting. The date-time functions HPART, HNAME, HYYWD, HADD, and HDIFF
use Saturday as the start of the week, when the WEEKFIRST setting is 0. The simplified
functions DTADD, DTDIFF, DTRUNC, and DTPART, as well as printing of dates truncated
to weeks, and recognition of date constant strings that contain week numbers, use
Sunday as the default value, when the WEEKFIRST setting is 0. If the user explicitly
sets WEEKFIRST to another value, that value is used by all of the functions.

Example: Setting Sunday as the Start of the Week


The following designates Sunday as the start of the week, using non-standard week
numbering:

SET WEEKFIRST = 1

Syntax: How to View the Current Setting of WEEKFIRST

? SET WEEKFIRST

This returns the value that indicates the week numbering algorithm and the first day of the
week. For example, the integer 1 represents Sunday with non-standard week numbering.

Controlling Processing of Date-Time Values


Strict processing checks date-time values when they are input by an end user, read from a
transaction file, displayed, or returned by a subroutine to ensure that they represent a valid
date and time. For example, a numeric month must be between 1 and 12, and the day must
be within the number of days for the specified month.

438
13. Date-Time Functions

Syntax: How to Enable Strict Processing of Date-Time Values

SET DTSTRICT = {ON|OFF}

where:
ON

Invokes strict processing. ON is the default value.

Strict processing checks date-time values when they are input by an end user, read from a
transaction file, displayed, or returned by a subroutine to ensure that they represent a
valid date and time. For example, a numeric month must be between 1 and 12, and the
day must be within the number of days for the specified month.

If DTSTRICT is ON and the result would be an invalid date-time value, the function returns
the value zero (0).
OFF

Does not invoke strict processing. Date-time components can have any value within the
constraint of the number of decimal digits allowed in the field. For example, if the field is a
two-digit month, the value can be 12 or 99, but not 115.

Supplying Arguments for Date-Time Functions


Date-time functions may operate on a component of a date-time value. This topic lists the valid
component names and abbreviations for use with these functions.

Reference: Arguments for Use With Date and Time Functions


The following component names, valid abbreviations, and values are supported as arguments
for the date-time functions that require them:

Component Name Abbreviation Valid Values

year yy 0001-9999

quarter qq 1-4

month mm 1-12 or a month name, depending on the


function.

day-of-year dy 1-366

Using Functions 439


Using Date-Time Functions

Component Name Abbreviation Valid Values

day or day-of-month dd 1-31 (The two component names are


equivalent.)

week wk 1-53

weekday dw 1-7 (Sunday-Saturday)

hour hh 0-23

minute mi 0-59

second ss 0-59

millisecond ms 0-999

microsecond mc 0-999999

nanosecond ns 0-999999999

Note:

For an argument that specifies a length of eight, ten, or 12 characters, use eight to include
milliseconds, ten to include microseconds, and 12 to include nanoseconds in the returned
value.

The last argument is always a USAGE format that indicates the data type returned by the
function. The type may be A (alphanumeric), I (integer), D (floating-point double precision),
H (date-time), or a date format (for example, YYMD).

Using Date-Time Formats


There are three types of date formats that are valid in date-time values: numeric string format,
formatted-string format, and translated-string format. In each format, two-digit years are
interpreted using the DEFCENT and YRTHRESH parameters.

Time components are separated by colons and may be followed by A.M., P.M., a.m., or p.m.

440
13. Date-Time Functions

The DATEFORMAT parameter specifies the order of the date components (month/day/year)
when date-time values are entered in the formatted string and translated string formats. It
makes a value’s input format independent of the format of the variable to which it is being
assigned.

Numeric String Format


The numeric string format is exactly two, four, six, or eight digits. Four-digit strings are
considered to be a year (century must be specified), and the month and day are set to January
1. Six and eight-digit strings contain two or four digits for the year, followed by two for the
month, and two for the day. Because the component order is fixed with this format, the
DATEFORMAT setting is ignored.

If a numeric-string format longer than eight digits is encountered, it is treated as a combined


date-time string in the Hnn format.

Example: Using Numeric String Format


The following are examples of numeric string date constants:

String Date

99 January 1, 1999

1999 January 1, 1999

19990201 February 1, 1999

Formatted-string Format
The formatted-string format contains a one or two-digit day, a one or two-digit month, and a two
or four-digit year, each component separated by a space, slash, hyphen, or period. All three
components must be present and follow the DATEFORMAT setting. If any of the three fields is
four digits, it is interpreted as the year, and the other two fields must follow the order given by
the DATEFORMAT setting.

Using Functions 441


Using Date-Time Functions

Example: Using Formatted-string Format


The following are examples of formatted-string date constants and specify May 20, 1999:

1999/05/20
5 20 1999
99.05.20
1999-05-20

Translated-string Format
The translated-string format contains the full or abbreviated month name. The year must also
be present in four-digit or two-digit form. If the day is missing, day 1 of the month is assumed;
if present, it can have one or two digits. If the string contains both a two-digit year and a two-
digit day, they must be in the order given by the DATEFORMAT setting.

Example: Using Translated-string Format


The following date is in translated-string format:

January 6 2000

Time Format
Time components are separated by colons and may be followed by A.M., P.M., a.m., or p.m.

Seconds can be expressed with a decimal point or be followed by a colon. If there is a colon
after seconds, the value following it represents milliseconds. There is no way to express
microseconds or nanoseconds using this notation.

A decimal point in the seconds value indicates the decimal fraction of a second. Microseconds
can be represented using six decimal digits. Nanoseconds can be represented using nine
decimal digits.

Example: Using Time Formats


The following are examples of acceptable time formats:

14:30:20:99 (99 milliseconds)


14:30
14:30:20.99 (99/100 seconds)
14:30:20.999999 (999999 microseconds)
02:30:20:500pm

442
13. Date-Time Functions

Example: Using Universal Date-Time Input Values


With DTSTANDARD settings of STANDARD and STANDARDU, the following date-time values can
be read as input:

Input Value Description

14:30[:20,99] Comma separates time components instead of period

14:30[:20.99]Z Universal time

15:30[:20,99]+01 Each of these is the same as above in Central European


15:30[:20,99]+0100
15:30[:20,99]+01:00
Time

09:30[:20.99]-05 Same as above in Eastern Standard Time

Note that these values are stored identically internally with the STANDARDU setting. With the
STANDARD setting, everything following the Z, +, or - is ignored.

Assigning Date-Time Values


A date-time value is a constant in character format assigned by one of the following:

A sequential data source.

An expression that defines WHERE or IF criteria or creates a temporary field using the
DEFINE or COMPUTE command.

A date-time constant can have blanks at the beginning or end or immediately preceding an
am/pm indicator.

Syntax: How to Assign Date-Time Values


In a character file
date_string [time_string]

or
time_string [date_string]

Using Functions 443


Using Date-Time Functions

In a COMPUTE, DEFINE, or WHERE expression


DT(date_string [time_string])

or
DT(time_string [date_string])

In an IF expression
'date_string [time_string]'

or
'time_string [date_string]'

where:
time_string
Is a time string in acceptable format. A time string can have a blank immediately preceding
an am/pm indicator.
date_string
Is a date string in numeric string, formatted-string, or translated-string format.

In an IF criteria, if the value does not contain blanks or special characters, the single
quotation marks are not necessary.

Note: The date and time strings must be separated by at least one blank space. Blank
spaces are also permitted at the beginning and end of the date-time string.

Example: Assigning Date-Time Literals


The DT prefix can be used in a COMPUTE, DEFINE, or WHERE expression to assign a date-time
literal to a date-time field. For example:
DT2/HYYMDS = DT(20051226 05:45);
DT3/HYYMDS = DT(2005 DEC 26 05:45);
DT4/HYYMDS = DT(December 26 2005 05:45);

444
13. Date-Time Functions

Example: Assigning a Date-Time Value in a COMPUTE Command


The following uses the DT function in a COMPUTE command to create a new field containing an
assigned date-time value.
TABLE FILE EMPLOYEE
PRINT LAST_NAME FIRST_NAME AND COMPUTE
NEWSAL/D12.2M = CURR_SAL + (0.1 * CURR_SAL);
RAISETIME/HYYMDIA = DT(20000101 09:00AM);
WHERE CURR_JOBCODE LIKE 'B%'
END

The output is:


LAST_NAME FIRST_NAME NEWSAL RAISETIME
--------- ---------- ------ ---------
SMITH MARY $14,520.00 2000/01/01 9:00AM
JONES DIANE $20,328.00 2000/01/01 9:00AM
ROMANS ANTHONY $23,232.00 2000/01/01 9:00AM
MCCOY JOHN $20,328.00 2000/01/01 9:00AM
BLACKWOOD ROSEMARIE $23,958.00 2000/01/01 9:00AM
MCKNIGHT ROGER $17,710.00 2000/01/01 9:00AM

Example: Assigning a Date-Time Value in WHERE Criteria


The following uses the DT function to create a new field containing an assigned date-time
value. This value is then used as a WHERE criteria.
DEFINE FILE EMPLOYEE
NEWSAL/D12.2M = CURR_SAL + (0.1 * CURR_SAL);
RAISETIME/HYYMDIA = DT(20000101 09:00AM);
END

TABLE FILE EMPLOYEE


PRINT LAST_NAME FIRST_NAME NEWSAL RAISETIME
WHERE RAISETIME EQ DT(20000101 09:00AM)
END

The output is:


LAST_NAME FIRST_NAME NEWSAL RAISETIME
--------- ---------- ------ ---------
STEVENS ALFRED $12,100.00 2000/01/01 9:00AM
SMITH MARY $14,520.00 2000/01/01 9:00AM
JONES DIANE $20,328.00 2000/01/01 9:00AM
SMITH RICHARD $10,450.00 2000/01/01 9:00AM
BANNING JOHN $32,670.00 2000/01/01 9:00AM
IRVING JOAN $29,548.20 2000/01/01 9:00AM
ROMANS ANTHONY $23,232.00 2000/01/01 9:00AM
MCCOY JOHN $20,328.00 2000/01/01 9:00AM
BLACKWOOD ROSEMARIE $23,958.00 2000/01/01 9:00AM
MCKNIGHT ROGER $17,710.00 2000/01/01 9:00AM
GREENSPAN MARY $9,900.00 2000/01/01 9:00AM
CROSS BARBARA $29,768.20 2000/01/01 9:00AM

Using Functions 445


HADD: Incrementing a Date-Time Value

Example: Assigning a Date-Time Value in IF Criteria


The following uses the DT function to create a new field containing an assigned date-time
value. This value is then used in the IF phrase.
DEFINE FILE EMPLOYEE
NEWSAL/D12.2M = CURR_SAL + (0.1 * CURR_SAL);
RAISETIME/HYYMDIA = DT(20000101 09:00AM);
END

TABLE FILE EMPLOYEE


PRINT LAST_NAME FIRST_NAME NEWSAL RAISETIME
IF RAISETIME EQ '20000101 09:00AM'
END

The output is:


LAST_NAME FIRST_NAME NEWSAL RAISETIME
--------- ---------- ------ ---------
STEVENS ALFRED $12,100.00 2000/01/01 9:00AM
SMITH MARY $14,520.00 2000/01/01 9:00AM
JONES DIANE $20,328.00 2000/01/01 9:00AM
SMITH RICHARD $10,450.00 2000/01/01 9:00AM
BANNING JOHN $32,670.00 2000/01/01 9:00AM
IRVING JOAN $29,548.20 2000/01/01 9:00AM
ROMANS ANTHONY $23,232.00 2000/01/01 9:00AM
MCCOY JOHN $20,328.00 2000/01/01 9:00AM
BLACKWOOD ROSEMARIE $23,958.00 2000/01/01 9:00AM
MCKNIGHT ROGER $17,710.00 2000/01/01 9:00AM
GREENSPAN MARY $9,900.00 2000/01/01 9:00AM
CROSS BARBARA $29,768.20 2000/01/01 9:00AM

HADD: Incrementing a Date-Time Value


Available Languages: reporting, Maintain

The HADD function increments a date-time value by a given number of units.

Syntax: How to Increment a Date-Time Value

HADD(datetime, 'component', increment, length, output)

where:
datetime
Date-time

Is the date-time value to be incremented, the name of a date-time field that contains the
value, or an expression that returns the value.

446
13. Date-Time Functions

component
Alphanumeric

Is the name of the component to be incremented enclosed in single quotation marks.

Note: WEEKDAY is not a valid component for HADD.


increment
Integer

Is the number of units (positive or negative) by which to increment the component, the
name of a numeric field that contains the value, or an expression that returns the value.
length
Integer

Is the number of characters returned. Valid values are:

8 indicates a date-time value that includes one to three decimal digits (milliseconds).

10 indicates a date-time value that includes four to six decimal digits (microseconds).

12 indicates a date-time value that includes seven to nine decimal digits


(nanoseconds).
output
Date-time

Is the field that contains the result, or the format of the output value enclosed in single
quotation marks. This field must be in date-time format (data type H).

Example: Incrementing the Month Component of a Date-Time Field (Reporting)


HADD adds two months to each value in TRANSDATE and stores the result in ADD_MONTH. If
necessary, the day is adjusted so that it is valid for the resulting month.
TABLE FILE VIDEOTR2
PRINT CUSTID TRANSDATE AS 'DATE-TIME' AND COMPUTE
ADD_MONTH/HYYMDS = HADD(TRANSDATE, 'MONTH', 2, 8, 'HYYMDS');
WHERE DATE EQ 2000;
END

The output is:


CUSTID DATE-TIME ADD_MONTH
------ --------- ---------
1237 2000/02/05 03:30 2000/04/05 03:30:00
1118 2000/06/26 05:45 2000/08/26 05:45:00

Using Functions 447


HADD: Incrementing a Date-Time Value

Example: Incrementing the Month Component of a Date-Time Field (Maintain)


HADD adds two months to the DT1 field:
MAINTAIN FILE DATETIME
FOR 1 NEXT ID DT1 INTO DTSTK
COMPUTE
NEW_DATE/HYYMDS = HADD(DTSTK.DT1, 'MONTH', 2,10, NEW_DATE);
TYPE "DT1 IS: <DTSTK(1).DT1 "
TYPE "NEW_DATE IS: <NEW_DATE "

The result is:


DT1 IS: 2000/1/1 02:57:25
NEW_DATE IS: 2000/3/1 02:57:25
TRANSACTIONS: COMMITS = 1 ROLLBACKS = 0
SEGMENTS : INCLUDED = 0 UPDATED = 0 DELETED = 0

Example: Converting Unix (Epoch) Time to a Date-Time Value


Unix time (also known as Epoch time) defines an instant in time as the number of seconds
that have elapsed since 00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January
1970, not counting leap seconds.

The following DEFINE FUNCTION takes a number representing epoch time and converts it to a
date-time value by using the HADD function to add the number of seconds represented by the
input value in epoch time to the epoch base date:
DEFINE FUNCTION UNIX2GMT(INPUT/I9)
UNIX2GMT/HYYMDS = HADD(DT(1970 JAN 1),'SECONDS',INPUT,8,'HYYMDS');
END

The following request uses this DEFINE FUNCTION to convert the epoch time 1449068652 to
a date-time value:
DEFINE FILE GGSALES
INPUT/I9=1449068652;
OUTDATE/HMTDYYSb = UNIX2GMT(INPUT);
END
TABLE FILE GGSALES
PRINT DATE NOPRINT INPUT OUTDATE
WHERE RECORDLIMIT EQ 1
ON TABLE SET PAGE NOLEAD
END

448
13. Date-Time Functions

The output is shown in the following image:

HCNVRT: Converting a Date-Time Value to Alphanumeric Format


Available Languages: reporting, Maintain

The HCNVRT function converts a date-time value to alphanumeric format for use with operators
such as EDIT, CONTAINS, and LIKE.

Syntax: How to Convert a Date-Time Value to Alphanumeric Format

HCNVRT(datetime, '(format)', length, output)

where:
datetime
Date-time

Is the date-time value to be converted, the name of a date-time field that contains the
value, or an expression that returns the value.
format
Alphanumeric

Is the format of the date-time field enclosed in parentheses and single quotation marks. It
must be a date-time format (data type H, up to H23).
length
Integer

Is the number of characters in the alphanumeric field that is returned. You can supply the
actual value, the name of a numeric field that contains the value, or an expression that
returns the value. If length is smaller than the number of characters needed to display the
alphanumeric field, the function returns a blank.
output
Alphanumeric

Using Functions 449


HDATE: Converting the Date Portion of a Date-Time Value to a Date Format

Is the field that contains the result, or the format of the output value enclosed in single
quotation marks. This field must be in alphanumeric format and must be long enough to
contain all of the characters returned.

Example: Converting a Date-Time Field to Alphanumeric Format (Reporting)


HCNVRT converts the TRANSDATE field to alphanumeric format. The first function does not
include date-time display options for the field; the second function does for readability. It also
specifies the display of seconds in the input field.
TABLE FILE VIDEOTR2
PRINT CUSTID TRANSDATE AS 'DATE-TIME' AND COMPUTE
ALPHA_DATE_TIME1/A20 = HCNVRT(TRANSDATE, '(H17)', 17, 'A20');
ALPHA_DATE_TIME2/A20 = HCNVRT(TRANSDATE, '(HYYMDS)', 20, 'A20');
WHERE DATE EQ 2000
END

The output is:


CUSTID DATE-TIME ALPHA_DATE_TIME1 ALPHA_DATE_TIME2
------ --------- ---------------- ----------------
1237 2000/02/05 03:30 20000205033000000 2000/02/05 03:30:00
1118 2000/06/26 05:45 20000626054500000 2000/06/26 05:45:00

Example: Converting a Date-Time Field to Alphanumeric Format (Maintain)


HCNVRT converts the DT1 field to alphanumeric format:
MAINTAIN FILE DATETIME
FOR ALL NEXT ID INTO STK;
COMPUTE
RESULT_HCNVRT/A20 = HCNVRT(STK.DT1,'(HYYMDH)',20, RESULT_HCNVRT);
TYPE "STK(1).DT1 = "STK(1).DT1;
TYPE "RESULT_HCNVRT = " RESULT_HCNVRT;
END

HDATE: Converting the Date Portion of a Date-Time Value to a Date Format


Available Languages: reporting, Maintain

The HDATE function converts the date portion of a date-time value to the date format YYMD.
You can then convert the result to other date formats.

450
13. Date-Time Functions

Syntax: How to Convert the Date Portion of a Date-Time Value to a Date Format

HDATE(datetime, output)

where:
datetime
Date-time

Is the date-time value to be converted, the name of a date-time field that contains the
value, or an expression that returns the value.
output
Date

Is the format in single quotation marks or the field that contains the result.

Example: Converting the Date Portion of a Date-Time Field to a Date Format (Reporting)
HDATE converts the date portion of the TRANSDATE field to the date format YYMD:
TABLE FILE VIDEOTR2
PRINT CUSTID TRANSDATE AS 'DATE-TIME' AND COMPUTE
TRANSDATE_DATE/YYMD = HDATE(TRANSDATE, 'YYMD');
WHERE DATE EQ 2000;
END

The output is:


CUSTID DATE-TIME TRANSDATE_DATE
------ --------- --------------
1237 2000/02/05 03:30 2000/02/05
1118 2000/06/26 05:45 2000/06/26

Example: Converting the Date Portion of a Date-Time Field to a Date Format (Maintain)
HDATE converts the date portion of DT1 to date format YYMD:
MAINTAIN FILE DATETIME
FOR 1 NEXT ID INTO STK;
COMPUTE
DT1_DATE/YYMD = HDATE(STK.DT1, DT1_DATE);
TYPE "STK(1).DT1 = <STK(1).DT1";
TYPE "DT1_DATE = <DT1_DATE";
END

The output is:


STK(1).DT1 = 2000/1/1 02:57:25
DT1_DATE = 2000/01/01

Using Functions 451


HDIFF: Finding the Number of Units Between Two Date-Time Values

HDIFF: Finding the Number of Units Between Two Date-Time Values


Available Languages: reporting, Maintain

The HDIFF function calculates the number of date or time component units between two date-
time values.

Reference: Usage Notes for HDIFF


HDIFF does its subtraction differently from DATEDIF, which subtracts date components stored
in date fields. The DATEDIF calculation looks for full years or full months. Therefore,
subtracting the following two dates and requesting the number of months or years, results in
0:
DATE1 12/25/2014, DATE2 1/5/2015

Performing the same calculation using HDIFF on date-time fields results in a value of 1 month
or 1 year as, in this case, the month or year is first extracted from each date-time value, and
then the subtraction occurs.

Syntax: How to Find the Number of Units Between Two Date-Time Values

HDIFF(end_dt, start_dt, 'component', output)

where:
end_dt
Date-time

Is the date-time value to subtract from, the name of a date-time field that contains the
value, or an expression that returns the value.
start_dt
Date-time

Is the date-time value to subtract, the name of a date-time field that contains the value, or
an expression that returns the value.
component
Alphanumeric

Is the name of the component to be used in the calculation, enclosed in single quotation
marks. If the component is a week, the WEEKFIRST parameter setting is used in the
calculation.

452
13. Date-Time Functions

output
Floating-point double-precision

Is the field that contains the result, or the format of the output value enclosed in single
quotation marks. The format must be floating-point double-precision.

Example: Finding the Number of Days Between Two Date-Time Fields (Reporting)
HDIFF calculates the number of days between the TRANSDATE and ADD_MONTH fields and
stores the result in DIFF_PAYS, which has the format D12.2:
TABLE FILE VIDEOTR2
PRINT CUSTID TRANSDATE AS 'DATE-TIME' AND COMPUTE
ADD_MONTH/HYYMDS = HADD(TRANSDATE, 'MONTH', 2, 8, 'HYYMDS');
DIFF_DAYS/D12.2 = HDIFF(ADD_MONTH, TRANSDATE, 'DAY', 'D12.2');
WHERE DATE EQ 2000;
END

The output is:


CUSTID DATE-TIME ADD_MONTH DIFF_DAYS
------ --------- --------- ---------
1237 2000/02/05 03:30 2000/04/05 03:30:00 60.00
1118 2000/06/26 05:45 2000/08/26 05:45:00 61.00

Example: Finding the Number of Days Between Two Date-Time Fields (Maintain)
HDIFF calculates the number of days between ADD_MONTH and DT1:
MAINTAIN FILE DATETIME
FOR 1 NEXT ID INTO STK;
COMPUTE
NEW_DATE/HYYMDS = HADD(STK.DT1, 'MONTH', 2,10, NEW_DATE);
DIFF_DAYS/D12.2 = HDIFF(NEW_DATE,STK.DT1,'DAY', DIFF_DAYS);
TYPE "STK(1).DT1 = "STK(1).DT1;
TYPE "NEW_DATE = "NEW_DATE;
TYPE "DIFF_DAYS = "DIFF_DAYS
END

HDTTM: Converting a Date Value to a Date-Time Value


Available Languages: reporting, Maintain

The HDTTM function converts a date value to a date-time value. The time portion is set to
midnight.

Using Functions 453


HDTTM: Converting a Date Value to a Date-Time Value

Syntax: How to Convert a Date Value to a Date-Time Value

HDTTM(date, length, output)

where:
date
Date

Is the date to be converted, the name of a date field that contains the value, or an
expression that returns the value. It must be a full component format date. For example, it
can be MDYY or YYJUL.
length
Integer

Is the length of the returned date-time value. Valid values are:

8 indicates a time value that includes milliseconds.

10 indicates a time value that includes microseconds.

12 indicates a time value that includes nanoseconds.


output
Date-time

Is the generated date-time value. It can be a field or the format of the output value
enclosed in single quotation marks. The value must have a date-time format (data type H).

Example: Converting a Date Field to a Date-Time Field (Reporting)


HDTTM converts the date field TRANSDATE_DATE to a date-time field:

TABLE FILE VIDEOTR2


PRINT CUSTID TRANSDATE AS 'DATE-TIME' AND COMPUTE
TRANSDATE_DATE/YYMD = HDATE(TRANSDATE, 'YYMD');
DT2/HYYMDIA = HDTTM(TRANSDATE_DATE, 8, 'HYYMDIA');
WHERE DATE EQ 2000;
END

The output is:


CUSTID DATE-TIME TRANSDATE_DATE DT2
------ --------- -------------- ---
1237 2000/02/05 03:30 2000/02/05 2000/02/05 12:00AM
1118 2000/06/26 05:45 2000/06/26 2000/06/26 12:00AM

454
13. Date-Time Functions

Example: Converting a Date Field to a Date-Time Field (Maintain)


HDTTM converts the date field DT1_DATE to a date-time field:

MAINTAIN FILE DATETIME


FOR 1 NEXT ID INTO STK;
COMPUTE
DT1_DATE/YYMD = HDATE(DT1, DT1_DATE);
DT2/HYYMDIA = HDTTM(DT1_DATE, 8, DT2);
TYPE "STK(1).DT1 = <STK(1).DT1";
TYPE "DT1_DATE = <DT1_DATE";
TYPE "DT2 = <DT2";
END

HEXTR: Extracting Components of a Date-Time Value and Setting Remaining Components


to Zero
Available Languages: reporting, Maintain

The HEXTR function extracts one or more components from a date-time value and moves them
to a target date-time field with all other components set to zero.

Syntax: How to Extract Multiple Components From a Date-Time Value

HEXTR(datetime, 'componentstring', length, output)

where:
datetime
Date-time
Is the date-time value from which to extract the specified components.
componentstring
Alphanumeric
Is a string of codes, in any order, that indicates which components are to be extracted and
moved to the output date-time field. The following table shows the valid values. The string
is considered to be terminated by any character not in this list:

Code Description

C century (the two high-order digits only of the four-digit year)

Y year (the two low-order digits only of the four-digit year)

YY Four digit year.

Using Functions 455


HEXTR: Extracting Components of a Date-Time Value and Setting Remaining Components to Zero

Code Description

M month

D day

H hour

I minutes

S seconds

s milliseconds (the three high-order digits of the six-digit microseconds value)

u microseconds (the three low-order digits of the six-digit microseconds value)

m All six digits of the microseconds value.

n Low order three digits of nine decimal digits.

length
Is the length of the returned date-time value. Valid values are:

8 indicates a time value that includes milliseconds.

10 indicates a time value that includes microseconds.

12 indicates a time value that includes nanoseconds.


output
Is the field that contains the result, or the format of the output value enclosed in single
quotation marks. This field must be in date-time format (data type H).

Example: Extracting Hour and Minute Components Using HEXTR


The VIDEOTR2 data source has a date-time field named TRANSDATE of type HYYMDI. The
following request selects all records containing the time 09:18AM, regardless of the value of
the remaining components:

TABLE FILE VIDEOTR2


PRINT TRANSDATE
BY LASTNAME
BY FIRSTNAME
WHERE HEXTR(TRANSDATE, 'HI', 8, 'HYYMDI') EQ DT(09:18AM)
END

456
13. Date-Time Functions

The output is:


LASTNAME FIRSTNAME TRANSDATE
-------- --------- ---------
DIZON JANET 1999/11/05 09:18
PETERSON GLEN 1999/09/09 09:18

HGETC: Storing the Current Local Date and Time in a Date-Time Field
Available Languages: reporting, Maintain

The HGETC function returns the current local date and time in the desired date-time format. If
millisecond or microsecond values are not available in your operating environment, the
function retrieves the value zero for these components.

Syntax: How to Store the Current Local Date and Time in a Date-Time Field

HGETC(length, output)

where:
length
Integer

Is the length of the returned date-time value. Valid values are:

8 indicates a time value that includes milliseconds.

10 indicates a time value that includes microseconds.

12 indicates a time value that includes nanoseconds.


output
Date-time

Is the returned date-time value. Can be a field that contains the result, or the format of the
output value enclosed in single quotation marks. The format must be in date-time format
(data type H).

Example: Storing the Current Date and Time in a Date-Time Field (Reporting)
HGETC stores the current date and time in DT2:

TABLE FILE VIDEOTR2


PRINT CUSTID TRANSDATE AS 'DATE-TIME' AND COMPUTE
DT2/HYYMDm = HGETC(10, 'HYYMDm');
WHERE DATE EQ 2000;
END

Using Functions 457


HGETZ: Storing the Current Coordinated Universal Time in a Date-Time Field

The output is:


CUSTID DATE-TIME DT2
------ --------- ---
1237 2000/02/05 03:30 2000/10/03 15:34:24.000000
1118 2000/06/26 05:45 2000/10/03 15:34:24.000000

Example: Storing the Current Local Date and Time in a Date-Time Field (Maintain)
HGETC stores the current date and time in DT2:
MAINTAIN
COMPUTE DT2/HYYMDm = HGETC(10, DT2);
TYPE "DT2 = <DT2";
END

HGETZ: Storing the Current Coordinated Universal Time in a Date-Time Field


Available Languages: reporting, Maintain

HGETZ provides the current Coordinated Universal Time (UTC/GMT time, often called Zulu
time). UTC is the primary civil time standard by which the world regulates clocks and time.

The value is returned in the desired date-time format. If millisecond or microsecond values are
not available in your operating environment, the function retrieves the value zero for these
components.

Syntax: How to Store the Current Universal Date and Time in a Date-Time Field

HGETZ(length, output)

where:
length
Integer

Is the length of the returned date-time value. Valid values are:

8 indicates a time value that includes milliseconds.

10 indicates a time value that includes microseconds.

12 indicates a time value that includes nanoseconds.

458
13. Date-Time Functions

output
Date-time

Is the returned date-time value. Can be a field that contains the result, or the format of the
output value enclosed in single quotation marks. The format must be in date-time format
(data type H).

Example: Storing the Current Universal Date and Time in a Date-Time Field (Reporting)
HGETZ stores the current universal date and time in DT2:

TABLE FILE VIDEOTRK


PRINT CUSTID AND COMPUTE
DT2/HYYMDm = HGETZ(10, 'HYYMDm');
WHERE CUSTID GE '2000' AND CUSTID LE '3000';
END

The output is:


CUSTID DT2
------ ---
2165 2015/05/08 14:43:08.740000
2187 2015/05/08 14:43:08.740000
2280 2015/05/08 14:43:08.740000
2282 2015/05/08 14:43:08.740000
2884 2015/05/08 14:43:08.740000

Example: Calculating the Time Zone


The time zone can be calculated as a positive or negative hourly offset from GMT. Locations to
the west of the prime meridian have a negative offset. The following request uses the HGETC
function to retrieve the local time, and the HGETZ function to retrieve the GMT time. The HDIFF
function calculates the number of boundaries between them in minutes. The zone is found by
dividing the minutes by 60:

DEFINE FILE EMPLOYEE


LOCALTIME/HYYMDS = HGETC(8, LOCALTIME);
UTCTIME/HYYMDS = HGETZ(8, UTCTIME);
MINUTES/D4= HDIFF(LOCALTIME, UTCTIME, 'MINUTES', 'D4');
ZONE/P3 = MINUTES/60;
END
TABLE FILE EMPLOYEE
PRINT EMP_ID NOPRINT OVER
LOCALTIME OVER
UTCTIME OVER
MINUTES OVER
ZONE
IF RECORDLIMIT IS 1
END

Using Functions 459


HHMMSS: Retrieving the Current Time

The output is:


LOCALTIME 2015/05/12 12:47:04
UTCTIME 2015/05/12 16:47:04
MINUTES -240
ZONE -4

HHMMSS: Retrieving the Current Time


Available Languages: reporting

The HHMMSS function retrieves the current time from the operating system as an eight
character string, separating the hours, minutes, and seconds with periods.

A compiled MODIFY procedure must use HHMMSS to obtain the time; it cannot use the &TOD
variable, which also returns the time. The &TOD variable is made current only when you
execute a MODIFY, SCAN, or FSCAN procedure.

There is also an HHMMSS function available in the Maintain language. For information on this
function, see HHMMSS: Retrieving the Current Time (Maintain) on page 479.

Syntax: How to Retrieve the Current Time

HHMMSS(output)

where:
output
Alphanumeric, at least A8

Is the name of the field that contains the result, or the format of the output value enclosed
in single quotation marks.

Example: Retrieving the Current Time


HHMMSS retrieves the current time and displays it in the page footing:
TABLE FILE EMPLOYEE
SUM CURR_SAL AS 'TOTAL SALARIES' AND COMPUTE
NOWTIME/A8 = HHMMSS(NOWTIME); NOPRINT
BY DEPARTMENT
FOOTING
"SALARY REPORT RUN AT TIME <NOWTIME"
END

460
13. Date-Time Functions

The output is:


DEPARTMENT TOTAL SALARIES
---------- --------------
MIS $108,002.00
PRODUCTION $114,282.00

SALARY REPORT RUN AT TIME 15.21.14

HHMS: Converting a Date-Time Value to a Time Value


Available Languages: reporting

The HHMS function converts a date-time value to a time value.

Syntax: How to Convert a Date-Time Value to a Time Value

HHMS(datetime, length, output)

where:
datetime
Date-time

Is the date-time value to be converted.


length
Numeric

Is the length of the returned time value. Valid values are:

8 indicates a time value that includes milliseconds.

10 indicates a time value that includes microseconds.

12 indicates a time value that includes nanoseconds.


output
Time

Is the name of the field that contains the result, or the format of the output value enclosed
in single quotation marks.

Using Functions 461


HINPUT: Converting an Alphanumeric String to a Date-Time Value

Example: Converting a Date-Time Value to a Time value


The following example converts the date-time field TRANSDATE to a time field with time format
HHIS,

DEFINE FILE VIDEOTR2


TRANSYEAR/I4 = HPART(TRANSDATE, 'YEAR', 'I4');
END
TABLE FILE VIDEOTR2
PRINT CUSTID TRANSDATE AS 'DATE-TIME' AND COMPUTE
TRANS_TIME/HHIS = HHMS(TRANSDATE, 8, 'HHIS');
WHERE TRANSYEAR EQ 2000;
END

The output is:

CUSTID DATE-TIME TRANS_TIME


------ --------- ----------
1118 2000/06/26 05:45 05:45:00
1237 2000/02/05 03:30 03:30:00

HINPUT: Converting an Alphanumeric String to a Date-Time Value


Available Languages: reporting, Maintain

The HINPUT function converts an alphanumeric string to a date-time value.

Syntax: How to Convert an Alphanumeric String to a Date-Time Value

HINPUT(source_length, 'source_string', output_length, output)

where:
source_length
Integer

Is the number of characters in the source string to be converted. You can supply the actual
value, the name of a numeric field that contains the value, or an expression that returns
the value.
source_string
Alphanumeric

Is the string to be converted enclosed in single quotation marks, the name of an


alphanumeric field that contains the string, or an expression that returns the string. The
string can consist of any valid date-time input value.

462
13. Date-Time Functions

output_length
Integer

Is the length of the returned date-time value. Valid values are:

8 indicates a time value that includes one to three decimal digits (milliseconds).

10 indicates a time value that includes four to six decimal digits (microseconds).

12 indicates a time value that includes seven to nine decimal digits (nanoseconds).
output
Date-time

Is the returned date-time value. Is a field that contains the result, or the format of the
output value enclosed in single quotation marks. The format must be in date-time format
(data type H).

Example: Converting an Alphanumeric String to a Date-Time Value (Reporting)


HCNVRT converts the TRANSDATE field to alphanumeric format, then HINPUT converts the
alphanumeric string to a date-time value:

TABLE FILE VIDEOTR2


PRINT CUSTID TRANSDATE AS 'DATE-TIME' AND COMPUTE
ALPHA_DATE_TIME/A20 = HCNVRT(TRANSDATE, '(H17)', 17, 'A20');
DT_FROM_ALPHA/HYYMDS = HINPUT(14, ALPHA_DATE_TIME, 8, 'HYYMDS');
WHERE DATE EQ 2000;
END

The output is:


CUSTID DATE-TIME ALPHA_DATE_TIME DT_FROM_ALPHA
------ --------- --------------- -------------
1237 2000/02/05 03:30 20000205033000000 2000/02/05 03:30:00
1118 2000/06/26 05:45 20000626054500000 2000/06/26 05:45:00

Example: Converting an Alphanumeric String to a Date-Time Value (Maintain)


HINPUT converts the DT1 field to alphanumeric format:

MAINTAIN FILE DATETIME


COMPUTE
RESULT/HMtDYYmA = HINPUT(20,'19971029133059888999',10,RESULT);
TYPE RESULT;
END

Using Functions 463


HMIDNT: Setting the Time Portion of a Date-Time Value to Midnight

HMIDNT: Setting the Time Portion of a Date-Time Value to Midnight


Available Languages: reporting, Maintain

The HMIDNT function changes the time portion of a date-time value to midnight (all zeros by
default). This allows you to compare a date field with a date-time field.

Syntax: How to Set the Time Portion of a Date-Time Value to Midnight

HMIDNT(datetime, length, output)

where:
datetime
Date-time

Is the date-time value whose time is to be set to midnight, the name of a date-time field
that contains the value, or an expression that returns the value.
length
Integer

Is the length of the returned date-time value. Valid values are:

8 indicates a time value that includes milliseconds.

10 indicates a time value that includes microseconds.

12 indicates a time value that includes nanoseconds.


output
Date-time

Is the date-time return value whose time is set to midnight and whose date is copied from
timestamp. Is the field that contains the result, or the format of the output value enclosed
in single quotation marks. The format must be in date-time format (data type H).

Example: Setting the Time to Midnight (Reporting)


HMIDNT sets the time portion of the TRANSDATE field to midnight first in the 24-hour system
and then in the 12-hour system:
TABLE FILE VIDEOTR2
PRINT CUSTID TRANSDATE AS 'DATE-TIME' AND COMPUTE
TRANSDATE_MID_24/HYYMDS = HMIDNT(TRANSDATE, 8, 'HYYMDS');
TRANSDATE_MID_12/HYYMDSA = HMIDNT(TRANSDATE, 8, 'HYYMDSA');
WHERE DATE EQ 2000;
END

464
13. Date-Time Functions

The output is:


CUSTID DATE-TIME TRANSDATE_MID_24 TRANSDATE_MID_12
------ --------- ---------------- ----------------
1118 2000/06/26 05:45 2000/06/26 00:00:00 2000/06/26 12:00:00AM
1237 2000/02/05 03:30 2000/02/05 00:00:00 2000/02/05 12:00:00AM

Example: Setting the Time to Midnight (Maintain)


HMIDNT sets the time portion of DT1 to midnight in both the 24-hour and 12-hour systems:

MAINTAIN FILE DATETIME


FOR 1 NEXT ID INTO STK;
COMPUTE
DT_MID_24/HYYMDS = HMIDNT(STK(1).DT1, 8, DT_MID_24);
DT_MID_12/HYYMDSA= HMIDNT(STK(1).DT1, 8, DT_MID_12);
TYPE "STK(1).DT1 = "STK(1).DT1;
TYPE "DT_MID_24 = <DT_MID_24";
TYPE "DT_MID_12 = <DT_MID_12";
END

HMASK: Extracting Date-Time Components and Preserving Remaining Components


Available Languages: reporting, Maintain

The HMASK function extracts one or more components from a date-time value and moves
them to a target date-time field with all other components of the target field preserved.

Syntax: How to Move Multiple Date-Time Components to a Target Date-Time Field

HMASK(source, 'componentstring', input, length, output)

where:
source
Is the date-time value from which the specified components are extracted.
componentstring
Is a string of codes, in any order, that indicates which components are to be extracted and
moved to the output date-time field. The following table shows the valid values. The string
is considered to be terminated by any character not in this list:

Code Description

C century (the two high-order digits only of the four-digit year)

Y year (the two low-order digits only of the four-digit year)

Using Functions 465


HMASK: Extracting Date-Time Components and Preserving Remaining Components

Code Description

YY Four digit year.

M month

D day

H hour

I minutes

S seconds

s milliseconds (the three high-order digits of the six-digit microseconds value)

u microseconds (the three low-order digits of the six-digit microseconds value)

m All six digits of the microseconds value.

n Low order three digits of nine decimal digits.

input
Is the date-time value that provides all the components for the output that are not
specified in the component string.
length
Is the length of the returned date-time value. Valid values are:

8 indicates a time value that includes one to three decimal digits (milliseconds).

10 indicates a time value that includes four to six decimal digits (microseconds).

12 indicates a time value that includes seven to nine decimal digits (nanoseconds).
output
Is the field that contains the result, or the format of the output value enclosed in single
quotation marks. This field must be in date-time format (data type H).

Reference: Usage Notes for the HMASK Function


HMASK processing is subject to the DTSTRICT setting. Moving the day (D) component without
the month (M) component could lead to an invalid result, which is not permitted if the
DTSTRICT setting is ON. Invalid date-time values cause any date-time function to return zeros.

466
13. Date-Time Functions

Example: Changing a Date-Time Field Using HMASK


The VIDEOTRK data source has a date-time field named TRANSDATE of format HYYMDI. The
following request changes any TRANSDATE value with a time component greater than 11:00 to
8:30 of the following day. First the HEXTR function extracts the hour and minutes portion of the
value and compares it to 11:00. If it is greater than 11:00, the HADD function calls HMASK to
change the time to 08:30 and adds one day to the date:
DEFINE FILE VIDEOTR2
ORIG_TRANSDATE/HYYMDI = TRANSDATE;
TRANSDATE =
IF HEXTR(TRANSDATE, 'HI', 8, 'HHI') GT DT(12:00)
THEN HADD (HMASK(DT(08:30), 'HISs', TRANSDATE, 8, 'HYYMDI'), 'DAY',
1,8, 'HYYMDI')
ELSE TRANSDATE;
END

TABLE FILE VIDEOTR2


PRINT ORIG_TRANSDATE TRANSDATE
BY LASTNAME
BY FIRSTNAME
WHERE ORIG_TRANSDATE NE TRANSDATE
END

The output is
LASTNAME FIRSTNAME ORIG_TRANSDATE TRANSDATE
-------- --------- -------------- ---------
BERTAL MARCIA 1999/07/29 12:19 1999/07/30 08:30
GARCIA JOANN 1998/05/08 12:48 1998/05/09 08:30
1999/11/30 12:12 1999/12/01 08:30
PARKER GLENDA 1999/01/06 12:22 1999/01/07 08:30
RATHER MICHAEL 1998/02/28 12:33 1998/03/01 08:30
WILSON KELLY 1999/06/26 12:34 1999/06/27 08:30

HNAME: Retrieving a Date-Time Component in Alphanumeric Format


Available Languages: reporting, Maintain

The HNAME function extracts a specified component from a date-time value and returns it in
alphanumeric format.

Using Functions 467


HNAME: Retrieving a Date-Time Component in Alphanumeric Format

Syntax: How to Retrieve a Date-Time Component in Alphanumeric Format

HNAME(datetime, 'component', output)

where:
datetime
Date-time

Is the date-time value from which a component value is to be extracted, the name of a
date-time field containing the value that contains the value, or an expression that returns
the value.
component
Alphanumeric

Is the name of the component to be retrieved enclosed in single quotation marks.


output
Alphanumeric, at least A2

Is the field that contains the result, or the format of the output value enclosed in single
quotation marks. The format must be in alphanumeric format.

The function converts a month argument to an abbreviation of the month name and
converts and all other components to strings of digits only. The year is always four digits,
and the hour assumes the 24-hour system.

Example: Retrieving the Week Component in Alphanumeric Format (Reporting)


HNAME returns the week in alphanumeric format from the TRANSDATE field. Changing the
WEEKFIRST parameter setting changes the value of the component.
SET WEEKFIRST = 7
TABLE FILE VIDEOTR2
PRINT CUSTID TRANSDATE AS 'DATE-TIME' AND COMPUTE
WEEK_COMPONENT/A10 = HNAME(TRANSDATE, 'WEEK', 'A10');
WHERE DATE EQ 2000;
END

When WEEKFIRST is set to seven, the output is:


CUSTID DATE-TIME WEEK_COMPONENT
------ --------- --------------
1237 2000/02/05 03:30 06
1118 2000/06/26 05:45 26

468
13. Date-Time Functions

When WEEKFIRST is set to three, the output is:


CUSTID DATE-TIME WEEK_COMPONENT
------ --------- --------------
1237 2000/02/05 03:30 05
1118 2000/06/26 05:45 25
For details on WEEKFIRST, see the Developing Reporting Applications manual.

Example: Retrieving the Day Component in Alphanumeric Format (Reporting)


HNAME retrieves the day in alphanumeric format from the TRANSDATE field:
TABLE FILE VIDEOTR2
PRINT CUSTID TRANSDATE AS 'DATE-TIME' AND COMPUTE
DAY_COMPONENT/A2 = HNAME(TRANSDATE, 'DAY', 'A2');
WHERE DATE EQ 2000;
END

The output is:


CUSTID DATE-TIME DAY_COMPONENT
------ --------- -------------
1237 2000/02/05 03:30 05
1118 2000/06/26 05:45 26

Example: Retrieving the Day Component in Alphanumeric Format (Maintain)


HNAME extracts the day in alphanumeric format from DT1:
MAINTAIN FILE DATETIME
FOR 1 NEXT ID INTO STK;
COMPUTE
DAY_COMPONENT/A2=HNAME(STK.DT1,'DAY',DAY_COMPONENT);
TYPE "STK(1).DT1 = "STK(1).DT1;
TYPE "DAY_COMPONENT = <DAY_COMPONENT"
END

HPART: Retrieving a Date-Time Component as a Numeric Value


Available Languages: reporting, Maintain

The HPART function extracts a specified component from a date-time value and returns it in
numeric format.

Using Functions 469


HPART: Retrieving a Date-Time Component as a Numeric Value

Syntax: How to Retrieve a Date-Time Component in Numeric Format

HPART(datetime, 'component', output)

where:
datetime
Date-time

Is the date-time value from which the component is to be extracted, the name of a date-
time field that contains the value, or an expression that returns the value.
component
Alphanumeric

Is the name of the component to be retrieved enclosed in single quotation marks.


output
Integer

Is the field that contains the result, or the integer format of the output value enclosed in
single quotation marks.

Example: Retrieving the Day Component in Numeric Format (Reporting)


HPART retrieves the day in integer format from the TRANSDATE field:
TABLE FILE VIDEOTR2
PRINT CUSTID TRANSDATE AS 'DATE-TIME' AND COMPUTE
DAY_COMPONENT/I2 = HPART(TRANSDATE, 'DAY', 'I2');
WHERE DATE EQ 2000;
END

The output is:

CUSTID DATE-TIME DAY_COMPONENT


1237 2000/02/05 03:30 5
1118 2000/06/26 05:45 26

470
13. Date-Time Functions

Example: Retrieving the Day Component in Numeric Format (Maintain)


HPART extracts the day in integer format from DT1:
MAINTAIN FILE DATETIME
FOR 1 NEXT ID INTO STK;
COMPUTE
DAY_COMPONENT/I2 = HPART(STK.DT1,'DAY',DAY_COMPONENT);
TYPE "STK(1).DT1 = <STK(1).DT1";
TYPE "DAY_COMPONENT = <DAY_COMPONENT";
END

HSETPT: Inserting a Component Into a Date-Time Value


Available Languages: reporting, Maintain

The HSETPT function inserts the numeric value of a specified component into a date-time
value.

Syntax: How to Insert a Component Into a Date-Time Value

HSETPT(datetime, 'component', value, length, output)

where:
datetime
Date-time

Is the date-time value in which to insert the component, the name of a date-time field that
contains the value, or an expression that returns the value.
component
Alphanumeric

Is the name of the component to be inserted enclosed in single quotation marks.


value
Integer

Is the numeric value to be inserted for the requested component, the name of a numeric
field that contains the value, or an expression that returns the value.

Using Functions 471


HSETPT: Inserting a Component Into a Date-Time Value

length
Integer

Is the length of the returned date-time value. Valid values are:

8 indicates a time value that includes one to three decimal digits (milliseconds).

10 indicates a time value that includes four to six decimal digits (microseconds).

12 indicates a time value that includes seven to nine decimal digits (nanoseconds).
output
Date-time

Is the returned date-time value whose chosen component is updated. All other
components are copied from the source date-time value.

Is the field that contains the result, or the format of the output value enclosed in single
quotation marks. The format must be in date-time format (data type H).

Example: Inserting the Day Component Into a Date-Time Field (Reporting)


HSETPT inserts the day as 28 into the ADD_MONTH field and stores the result in INSERT_DAY:

TABLE FILE VIDEOTR2


PRINT CUSTID TRANSDATE AS 'DATE-TIME' AND COMPUTE
ADD_MONTH/HYYMDS = HADD(TRANSDATE, 'MONTH', 2, 8, 'HYYMDS');
INSERT_DAY/HYYMDS = HSETPT(ADD_MONTH, 'DAY', 28, 8, 'HYYMDS');
WHERE DATE EQ 2000;
END

The output is:


CUSTID DATE-TIME ADD_MONTH INSERT_DAY
------ --------- --------- ----------
1118 2000/06/26 05:45 2000/08/26 05:45:00 2000/08/28 05:45:00
1237 2000/02/05 03:30 2000/04/05 03:30:00 2000/04/28 03:30:00

472
13. Date-Time Functions

Example: Inserting the Day Component Into a Date-Time Field (Maintain)


HSETPT inserts the day into ADD_MONTH:
MAINTAIN FILE DATETIME
FOR 1 NEXT ID INTO STK;
COMPUTE
ADD_MONTH/HYYMDS = HADD(STK.DT1,'MONTH', 2, 8, ADD_MONTH);
INSERT_DAY/HYYMDS = HSETPT(ADD_MONTH,'DAY', 28, 8, INSERT_DAY);
TYPE "STK(1).DT1 = <STK(1).DT1";
TYPE "ADD_MONTH = <ADD_MONTH";
TYPE "INSERT_DAY = <INSERT_DAY";
END

HTIME: Converting the Time Portion of a Date-Time Value to a Number


Available Languages: reporting, Maintain

The HTIME function converts the time portion of a date-time value to the number of
milliseconds if the length argument is eight, microseconds if the length argument is ten, or
nanoseconds if the length argument is 12.

Syntax: How to Convert the Time Portion of a Date-Time Value to a Number

HTIME(length, datetime, output)

where:
length
Integer

Is the length of the input date-time value. Valid values are:

8 indicates a time value that includes one to three decimal digits (milliseconds).

10 indicates a time value that includes four to six decimal digits (microseconds).

12 indicates a time value that includes seven to nine decimal digits (nanoseconds).
datetime
Date-time

Is the date-time value from which to convert the time, the name of a date-time field that
contains the value, or an expression that returns the value.

Using Functions 473


HTMTOTS or TIMETOTS: Converting a Time to a Timestamp

output
Floating-point double-precision

Is the field that contains the result, or the format of the output value enclosed in single
quotation marks. The format must be floating-point double-precision.

Example: Converting the Time Portion of a Date-Time Field to a Number (Reporting)


HTIME converts the time portion of the TRANSDATE field to the number of milliseconds:

TABLE FILE VIDEOTR2


PRINT CUSTID TRANSDATE AS 'DATE-TIME' AND COMPUTE
MILLISEC/D12.2 = HTIME(8, TRANSDATE, 'D12.2');
WHERE DATE EQ 2000;
END

The output is:


CUSTID DATE-TIME MILLISEC
------ --------- --------
1237 2000/02/05 03:30 12,600,000.00
1118 2000/06/26 05:45 20,700,000.00

Example: Converting the Time Portion of a Date-Time Field to a Number (Maintain)


HTIME converts the time portion of the DT1 field to the number of milliseconds:

MAINTAIN FILE DATETIME


FOR 1 NEXT ID INTO STK;
COMPUTE MILLISEC/D12.2 = HTIME(8, STK.DT1, MILLISEC);
TYPE "STK(1).DT1 = <STK(1).DT1";
TYPE "MILLISEC = <MILLISEC";
END

HTMTOTS or TIMETOTS: Converting a Time to a Timestamp


The HTMTOTS function returns a timestamp using the current date to supply the date
components of its value, and copies the time components from its input date-time value.

Note: TIMETOTS is a synonym for HTMTOTS.

Syntax: How to Convert a Time to a Timestamp

HTMTOTS(time, length, output)

or

TIMETOTS(time, length, output)

474
13. Date-Time Functions

where:
time
Date-Time

Is the date-time value whose time will be used. The date portion will be ignored.
length
Integer

Is the length of the result. This can be one of the following:

8 for input time values including milliseconds.

10 for input time values including microseconds.

12 for input time values including nanoseconds.


output_format
Date-Time

Is the timestamp whose date is set to the current date, and whose time is copied from
time.

Is the name of the field that contains the result, or the format of the output value enclosed
in single quotation marks.

Example: Converting a Time to a Timestamp


HTMTOTS converts the time portion of the TRANSDATE field to a timestamp, using the current
date for the date portion of the returned value:
DEFINE FILE VIDEOTR2
TSTMPSEC/HYYMDS = HTMTOTS(TRANSDATE, 8, 'HYYMDS');
END
TABLE FILE VIDEOTR2
PRINT TRANSDATE TSTMPSEC
BY LASTNAME BY FIRSTNAME
WHERE DATE EQ '1991'
END

Using Functions 475


HYYWD: Returning the Year and Week Number From a Date-Time Value

The output is:


LASTNAME FIRSTNAME TRANSDATE TSTMPSEC
-------- --------- --------- --------
CRUZ IVY 1991/06/27 02:45 2011/01/11 02:45:00
GOODMAN JOHN 1991/06/25 01:19 2011/01/11 01:19:00
GREEVEN GEORGIA 1991/06/24 10:27 2011/01/11 10:27:00
HANDLER EVAN 1991/06/20 05:15 2011/01/11 05:15:00
1991/06/21 07:11 2011/01/11 07:11:00
KRAMER CHERYL 1991/06/21 01:10 2011/01/11 01:10:00
1991/06/19 07:18 2011/01/11 07:18:00
1991/06/19 04:11 2011/01/11 04:11:00
MONROE CATHERINE 1991/06/25 01:17 2011/01/11 01:17:00
PATRICK 1991/06/27 01:17 2011/01/11 01:17:00
SPIVEY TOM 1991/11/17 11:28 2011/01/11 11:28:00
WILLIAMS KENNETH 1991/06/24 04:43 2011/01/11 04:43:00
1991/06/24 02:08 2011/01/11 02:08:00

HYYWD: Returning the Year and Week Number From a Date-Time Value
The week number returned by HNAME and HPART can actually be in the year preceding or
following the input date.

The HYYWD function returns both the year and the week number from a given date-time value.

The output is edited to conform to the ISO standard format for dates with week numbers, yyyy-
Www-d.

Syntax: How to Return the Year and Week Number From a Date-Time Value

HYYWD(dtvalue, output)

where:
dtvalue
Date-time

Is the date-time value to be edited, the name of a date-time field that contains the value,
or an expression that returns the value.

476
13. Date-Time Functions

output
Alphanumeric

Is the field that contains the result, or the format of the output value enclosed in single
quotation marks.

The output format must be at least 10 characters long. The output is in the following
format:
yyyy-Www-d

where:
yyyy

Is the four-digit year.


ww

Is the two-digit week number (01 to 53).


d

Is the single-digit day of the week (1 to 7). The d value is relative to the current
WEEKFIRST setting. If WEEKFIRST is 2 or ISO2 (Monday), then Monday is represented
in the output as 1, Tuesday as 2.

Using the EDIT function, you can extract the individual subfields from this output.

Example: Returning the Year and Week Number From a Date-Time Value
The following request against the VIDEOTR2 data source calls HYYWD to convert the
TRANSDATE date-time field to the ISO standard format for dates with week numbers.
WEEKFIRST is set to ISO2, which produces ISO standard week numbering:
SET WEEKFIRST = ISO2
TABLE FILE VIDEOTR2
SUM TRANSTOT QUANTITY
COMPUTE ISODATE/A10 = HYYWD(TRANSDATE, 'A10');
BY TRANSDATE
WHERE QUANTITY GT 1
END

Using Functions 477


HYYWD: Returning the Year and Week Number From a Date-Time Value

The output is:


TRANSDATE TRANSTOT QUANTITY ISODATE
--------- -------- -------- -------
1991/06/24 04:43 16.00 2 1991-W26-1
1991/06/25 01:17 2.50 2 1991-W26-2
1991/06/27 02:45 16.00 2 1991-W26-4
1996/08/17 05:11 5.18 2 1996-W33-6
1998/02/04 04:11 12.00 2 1998-W06-3
1999/01/30 04:16 13.00 2 1999-W04-6
1999/04/22 06:19 3.75 3 1999-W16-4
1999/05/06 05:14 1.00 2 1999-W18-4
1999/08/09 03:17 15.00 2 1999-W32-1
1999/09/09 09:18 14.00 2 1999-W36-4
1999/10/16 09:11 5.18 2 1999-W41-6
1999/11/05 11:12 2.50 2 1999-W44-5
1999/12/09 09:47 5.18 2 1999-W49-4
1999/12/15 04:04 2.50 2 1999-W50-3

Example: Extracting a Component From a Date Returned by HYYWD


The following request against the VIDEOTR2 data source calls HYYWD to convert the
TRANSDATE date-time field to the ISO standard format for dates with week numbers. It then
uses the EDIT function to extract the week component from this date. WEEKFIRST is set to
ISO2, which produces ISO standard week numbering:
SET WEEKFIRST = ISO2
TABLE FILE VIDEOTR2
SUM TRANSTOT QUANTITY
COMPUTE ISODATE/A10 = HYYWD(TRANSDATE, 'A10');
COMPUTE WEEK/A2 = EDIT(ISODATE, '$$$$$$99$$');
BY TRANSDATE
WHERE QUANTITY GT 1 AND DATE EQ 1991
END

The output is:


TRANSDATE TRANSTOT QUANTITY ISODATE WEEK
--------- -------- -------- ------- ----
1991/06/24 04:43 16.00 2 1991-W26-1 26
1991/06/25 01:17 2.50 2 1991-W26-2 26
1991/06/27 02:45 16.00 2 1991-W26-4 26

478
Chapter 14 Maintain-specific Date and Time
Functions
Maintain-specific date and time functions manipulate date and time values. These
functions are available only in Maintain Data.

There are additional date and time functions available in both the reporting and Maintain
languages. For more information on these functions, see Date-Time Functions on page
435.
In this chapter:

Maintain-specific Standard Date and Time Functions

Maintain-specific Standard Date and Time Functions


Standard date and time functions are for use with non-legacy dates. For a definition of
standard dates and times, see Date-Time Functions on page 435.

HHMMSS: Retrieving the Current Time (Maintain)

The HHMMSS function retrieves the current time from the operating system as an 8-character
string, separating the hours, minutes, and seconds with periods.

To use this function, you must import the function library MNTUWS. For information on
importing a function library, see Calling a Function on page 59.

There is also an HHMMSS function available in the reporting language. For information on this
function, see HHMMSS: Retrieving the Current Time on page 460.

Syntax: How to Retrieve the Current Time

HHMMSS()

Using Functions 479


Maintain-specific Standard Date and Time Functions

Example: Retrieving the Current Time


HHMMSS retrieves the current time from the operating system:
MAINTAIN
Module Import (mntuws);
Case Top
Compute now/a10 = hhmmss();
type "Now = <<now"
EndCase
END

The output is:


Now = 14.25.33

Initial_HHMMSS: Returning the Time the Application Was Started

The Initial_HHMMSS function returns the time when the Maintain Data application was started
as an 8-character string, with embedded periods separating the hours, minutes, and seconds.

To use this function, you must import the function library MNTUWS. For details on importing
this library, see Calling a Function on page 59.

Syntax: How to Retrieve the Initial Time

Initial_HHMMSS()

Initial_TODAY: Returning the Date the Application Was Started

The Initial_TODAY function returns the date in MM/DD/YY format when the Maintain Data
application was started as an 8-character string with embedded slashes.

To use this function, you must import the function library MNTUWS. For details on importing
this library, see Calling a Function on page 59.

Syntax: How to Retrieve the Initial Date

Initial_TODAY()

TODAY: Retrieving the Current Date (Maintain)

The TODAY function retrieves the current date from the system in the format MM/DD/YY.
TODAY always returns a date that is current. Therefore, if you are running an application late at
night, use TODAY. You can remove the embedded slashes using the EDIT function.

480
14. Maintain-specific Date and Time Functions

To use this function, you must import the function library MNTUWS. For information on
importing this library, see Calling a Function on page 59.

There is a version of the TODAY function that is available only in the reporting language. For
information on this function, see HTMTOTS or TIMETOTS: Converting a Time to a Timestamp on
page 474.

Syntax: How to Retrieve the Current Date

TODAY()

Example: Retrieving the Current Date


TODAY retrieves the current date from the system:
MAINTAIN
Module Import (mntuws);

Case Top
Compute date1/a8 = today();
type "Date1 = <<date1"
Endcase
END

The result is:


Date1 = 07/17/02

TODAY2: Returning the Current Date

The TODAY2 function retrieves the current date from the operating system in the format
MM/DD/YYYY. Use format A10 with the TODAY2 function to ensure proper results.

To use this function, you must import the function library MNTUWS. For information on
importing this library, see Calling a Function on page 59.

Syntax: How to Retrieve the Current Date

TODAY2()

Using Functions 481


Maintain-specific Standard Date and Time Functions

Example: Retrieving the Current Date


TODAY2 retrieves the current date from the system:
MAINTAIN
Module Import (mntuws);

Case Top
Compute date2/a10 = today2();
type "Date2 = <<date2"
Endcase
END

The result is:


Date2 = 07/17/2002

ADD: Adding Days to a Date

The ADD function adds a given number of days to a date.

Syntax: How to Add Days to a Date

ADD(date,value)

or
date.ADD(value)

where:
date
Is the date to add days to, or a field containing the date.
value
Is the number of days by which to increase the date.
This function changes the value of date.

Example: Adding Days to a Date


ADD adds 10 days to the each value in the DateVar field:
ADD(DateVar, 10)

482
14. Maintain-specific Date and Time Functions

The following are sample values for DateVar and the corresponding values for
ADD(DateVar, 10):
DateVar ADD(DateVar, 10);
------- -----------------
12/31/1999 01/10/2000
01/01/2000 01/11/2000
01/02/2000 01/12/2000

DAY: Extracting the Day of the Month From a Date

The DAY function extracts the day of the month from a date and returns the result as an
integer.

Syntax: How to Extract the Day of the Month From a Date

DAY(date);

where:
date
Is the date (in date format) from which to extract the day of the month, or a field
containing the date.

Example: Extracting the Day of the Month From a Date


DAY extracts the day of the month from the DATE field:
DAY(DATE)

The following are sample values for DATE and the corresponding values for DAY(DATE):
DATE DAY(DATE)
---- ---------
01/01/2000 1
01/02/2000 2
01/03/2000 3

JULIAN: Determining How Many Days Have Elapsed in the Year

The JULIAN function determines the number of days that have elapsed in the given year up to a
given date, and returns the result as an integer.

Using Functions 483


Maintain-specific Standard Date and Time Functions

Syntax: How to Determine How Many Days Have Elapsed in the Year

JULIAN(date);

where:
date
Is the date (in date format) for which to determine the number of days elapsed in the
given year, or a field containing the date.

Example: Determining How Many Days Have Elapsed in the Year


JULIAN determines the number of days that have elapsed up to the date in the DATE field:
JULIAN(DATE)

The following are sample values for DATE and the corresponding values for JULIAN(DATE):
DATE JULIAN(DATE)
---- ------------
01/01/2000 1
02/01/2000 32
03/01/2000 61

MONTH: Extracting the Month From a Date

The MONTH function extracts the month from a date and returns the result as an integer.

Syntax: How to Extract the Month From a Date

MONTH(date);

where:
date
Is the date (in date format) from which to extract the month, or a field containing the
date.

Example: Extracting the Month From a Date


MONTH extracts the month from each value in the DATE field:
MONTH(DATE)

484
14. Maintain-specific Date and Time Functions

The following are sample values for DATE and the corresponding values for MONTH(DATE):
DATE MONTH(DATE)
---- -----------
01/01/2000 1
02/01/2000 2
03/01/2000 3

QUARTER: Determining the Quarter

The QUARTER function determines the quarter of the year in which a date resides, and returns
the result as an integer.

Syntax: How to Determine the Quarter for a Date

QUARTER(date);

where:
date
Is the date (in date format) for which to determine the quarter, or a field containing the
date.

Example: Determining the Quarter for a Date


QUARTER extracts the quarter component from each value in the DATE field:
QUARTER(DATE)

The following are sample values for DATE and the corresponding values for QUARTER(DATE):
DATE QUARTER(DATE)
---- -------------
01/01/2000 1
04/01/2000 2
07/01/2000 3

SETMDY: Setting the Value to a Date

The SETMDY function sets a value to a date based on numeric values representing a day,
month, and year. SETMDY returns a 0 if the function is successful, and a negative number if
the function fails.

Using Functions 485


Maintain-specific Standard Date and Time Functions

Syntax: How to Set a Value to a Date

SETMDY(date, month, day, year);

or
date.SETMDY(month, day, year);

where:
date
Is the date, in date format, or a field containing the date.
month
Is an integer value representing a month.
day
Is an integer value representing the day of the month.
year
Is an integer value representing a year.

Example: Setting a Value to a Date


SETMDY sets the value of DateVar, which is formatted as a date that appears as wrMtrDYY
(for example, Saturday, January 1, 2000):
SETMDY(DateVar, month, day, year);

The following are sample values for month, day, and year, and the corresponding dates for
DateVar:
month day year DateVar
----- --- ---- -------
04 05 1965 Monday, April 5, 1965
02 01 1997 Saturday, February 1, 1997
01 01 2000 Saturday, January 1, 2000

SUB: Subtracting a Value From a Date

The SUB function subtracts a given number of days from a date.

486
14. Maintain-specific Date and Time Functions

Syntax: How to Subtract a Value From a Date

SUB(date,value)

or

date.SUB(value)

where:
date
Is the date from which to subtract the value, or a field containing the date.
value
Is the value to subtract from the date.

Example: Subtracting Days From a Date


SUB subtracts 10 days from each value in the DateVar field.

SUB(DateVar, 10)

The following are sample values for DateVar and the corresponding values for
SUB(DateVar, 10):
DateVar SUB(DateVar, 10);
------- -----------------
12/31/1999 12/21/2000
01/01/2000 12/22/2000
01/02/2000 12/23/2000

WEEKDAY: Determining the Day of the Week for a Date

The WEEKDAY function determines the day of the week for a date and returns the result as an
integer (1=Monday, 2=Tuesday, and so on).

Syntax: How to Determine the Day of the Week for a Date

WEEKDAY(date);

where:
date
Is the date (in date format) for which to determine the weekday, or a field containing
the date.

Using Functions 487


Maintain-specific Standard Date and Time Functions

Example: Determining the Day of the Week for a Date


WEEKDAY determines the day of the week for each date in the DATE field, and stores that day
as a number corresponding to a weekday:
WEEKDAY(DATE)

The following are sample values for DATE and the corresponding values for WEEKDAY(DATE):
DATE WEEKDAY(DATE)
---- -------------
01/01/2000 6
01/02/2000 7
01/03/2000 1

YEAR: Extracting the Year From a Date

The YEAR function extracts the year from a date.

Syntax: How to Extract the Year From a Date

YEAR(date);

where:
date
Is the date from which to extract the year, or a field containing the date.

Example: Extracting a Year From a Date


YEAR extracts the year from the DATE field, and stores that year in the YEAR(DATE) field:
YEAR(DATE)

The following are sample values for DATE and the corresponding values for YEAR(DATE):
DATE YEAR(DATE)
---- ----------
01/01/2000 2000
02/01/2001 2001
03/01/2002 2002

488
Chapter 15 Simplified Conversion Functions
Simplified conversion functions have streamlined parameter lists, similar to those used
by SQL functions. In some cases, these simplified functions provide slightly different
functionality than previous versions of similar functions.

The simplified functions do not have an output argument. Each function returns a value
that has a specific data type.

When used in a request against a relational data source, these functions are optimized
(passed to the RDBMS for processing).
In this chapter:

CHAR: Returning a Character Based on a Numeric Code

COMPACTFORMAT: Displaying Numbers in an Abbreviated Format

CTRLCHAR: Returning a Non-Printable Control Character

DT_FORMAT: Converting a Date or Date-Time Value to an Alphanumeric String

FPRINT: Displaying a Value in a Specified Format

HEXTYPE: Returning the Hexadecimal View of an Input Value

PHONETIC: Returning a Phonetic Key for a String

TO_INTEGER: Converting a Character String to an Integer Value

TO_NUMBER: Converting a Character String to a Numeric Value

CHAR: Returning a Character Based on a Numeric Code


The CHAR function accepts a decimal integer and returns the character identified by that
number converted to ASCII or EBCDIC, depending on the operating environment. The output is
returned as variable length alphanumeric. If the number is above the range of valid characters,
a null value is returned.

For a chart of printable characters and their decimal equivalents, see Character Chart for ASCII
and EBCDIC on page 50.

Using Functions 489


COMPACTFORMAT: Displaying Numbers in an Abbreviated Format

Syntax: How to Return a Character Based on a Numeric Code

CHAR(number_code)

where:
number_code
Integer

Is a field, number, or numeric expression whose whole absolute value will be used as a
number code to retrieve an output character.

For example, a TAB character is returned by CHAR(9) in ASCII environments, or by CHAR(5)


in EBCDIC environments.

Example: Using the CHAR Function to Insert Control Characters Into a String
The following request defines a field with carriage return (CHAR(13)) and line feed (CHAR(10))
characters inserted between the words HELLO and GOODBYE (in an ASCII environment). To
show that these characters were inserted, the output is generated in PDF format and the
StyleSheet attribute LINEBREAK='CRLF' is used to have these characters respected and print
the field value on two lines.

DEFINE FILE WF_RETAIL_LITE


MYFIELD/A20 WITH COUNTRY_NAME='HELLO' | CHAR(13) | CHAR(10) | 'GOODBYE';
END
TABLE FILE WF_RETAIL_LITE
SUM MYFIELD
ON TABLE PCHOLD FORMAT PDF
ON TABLE SET PAGE NOLEAD
ON TABLE SET STYLE *
TYPE=REPORT,LINEBREAK='CRLF',$
ENDSTYLE
END

The output is shown in the following image.

COMPACTFORMAT: Displaying Numbers in an Abbreviated Format


COMPACTFORMAT displays numbers in a compact format where:

K is an abbreviation for thousands.

490
15. Simplified Conversion Functions

M is an abbreviation for millions.

B is an abbreviation for billions.

T is an abbreviation for trillions.

COMPACTFORMAT computes which abbreviation to use, based on the order of magnitude of


the largest value in the column. The returned value is an alphanumeric string. Attempting to
output this value to a numeric format will result in a format error, and the value zero (0) will be
displayed.

Syntax: How to Display Numbers in an Abbreviated Format

COMPACTFORMAT(input)

where:
input
Is the name of a numeric field.

Example: Displaying Numbers in an Abbreviated Format


The following example uses the COMPACTFORMAT function to abbreviate the display of the
summed values of the DAYSDELAYED, QUANTITY_SOLD, and COGS_US fields.
TABLE FILE WF_RETAIL_LITE
SUM DAYSDELAYED QUANTITY_SOLD COGS_US
COMPUTE
CDAYS/A30= COMPACTFORMAT(DAYSDELAYED);
CQUANT/A30= COMPACTFORMAT(QUANTITY_SOLD);
CCOGS/A30= COMPACTFORMAT(COGS_US);
ON TABLE SET PAGE NOPAGE
ON TABLE SET STYLE *
GRID=OFF,$
ENDSTYLE
END

The output is shown in the following image.

Using Functions 491


CTRLCHAR: Returning a Non-Printable Control Character

CTRLCHAR: Returning a Non-Printable Control Character


The CTRLCHAR function returns a nonprintable control character specific to the running
operating environment, based on a supported list of keywords. The output is returned as
variable length alphanumeric.

Syntax: How to Return a Non-Printable Control Character

CTRLCHAR(ctrl_char)

where:
ctrl_char
Is one of the following keywords.

NUL returns a null character.

SOH returns a start of heading character.

STX returns a start of text character.

ETX returns an end of text character.

EOT returns an end of transmission character.

ENQ returns an enquiry character.

ACK returns an acknowledge character.

BEL returns a bell or beep character.

BS returns a backspace character.

TAB or HT returns a horizontal tab character.

LF returns a line feed character.

VT returns a vertical tab character.

FF returns a form feed (top of page) character.

CR returns a carriage control character.

SO returns a shift out character.

SI returns a shift in character.

DLE returns a data link escape character.

492
15. Simplified Conversion Functions

DC1 or XON returns a device control 1 character.

DC2 returns a device control 2 character.

DC3 or XOFF returns a device control 3 character.

DC4 returns a device control 4 character.

NAK returns a negative acknowledge character.

SYN returns a synchronous idle character.

ETB returns an end of transmission block character.

CAN returns a cancel character.

EM returns an end of medium character.

SUB returns a substitute character.

ESC returns an escape, prefix, or altmode character.

FS returns a file separator character.

GS returns a group separator character.

RS returns a record separator character.

US returns a unit separator character.

DEL returns a delete, rubout, or interrupt character.

Using Functions 493


DT_FORMAT: Converting a Date or Date-Time Value to an Alphanumeric String

Example: Using the CTRLCHAR Function to Insert Control Characters Into a String
The following request defines a field with carriage return (CTRLCHAR(CR)) and line feed
(CTRLCHAR(LF)) characters inserted between the words HELLO and GOODBYE. To show that
these characters were inserted, the output is generated in PDF format and the StyleSheet
attribute LINEBREAK='CRLF' is used to have these characters respected and print the field
value on two lines.
DEFINE FILE WF_RETAIL_LITE
MYFIELD/A20 WITH COUNTRY_NAME='HELLO' | CTRLCHAR(CR) | CTRLCHAR(LF) |
'GOODBYE';
END
TABLE FILE WF_RETAIL_LITE
SUM MYFIELD
ON TABLE PCHOLD FORMAT PDF
ON TABLE SET PAGE NOLEAD
ON TABLE SET STYLE *
TYPE=REPORT,LINEBREAK='CRLF',$
ENDSTYLE
END

The output is shown in the following image.

DT_FORMAT: Converting a Date or Date-Time Value to an Alphanumeric String


DT_FORMAT converts a date or date-time value to an alphanumeric string in a specified date or
date-time format. For information about date and date-time formats, see the Describing Data
With WebFOCUS Language manual.

Syntax: How to Convert a Date Value to an Alphanumeric String in a Specified Date Format

DT_FORMAT(date,'date_format')

where:
date
Numeric, date, or date-time

Is the date or date-time field or value to be converted.


'date_format'
Alphanumeric literal

494
15. Simplified Conversion Functions

Is a date or date-time format that fits the input date format type, enclosed in single
quotation marks.

Example: Converting Date and Date_Time Values to Alphanumeric Format


The following request converts date and date-time values to alphanumeric values with
specified date and date-time formats.
DEFINE FILE VIDEOTRK
NEWDT1/A12 = DT_FORMAT(TRANSDAT,'YYMD');
NEWDT2/A30 = DT_FORMAT(DT_CURRENT_DATETIME(SECOND),'HYYMTDs');
NEWDT3/A30= DT_FORMAT('April 1, 2019','YYMDTr');
END
TABLE FILE VIDEOTRK
PRINT NEWDT1 NEWDT2 NEWDT3
BY TRANSDAT
WHERE OUTPUTLIMIT EQ 1
ON TABLE SET PAGE NOLEAD
ON TABLE SET STYLE *
GRID=OFF,$
END

The output is shown in the following image.

FPRINT: Displaying a Value in a Specified Format


Given an output format, the simplified conversion function FPRINT converts a value to
alphanumeric format for display.

Note: A legacy FPRINT function also exists and is still supported. For information, see FPRINT:
Converting Fields to Alphanumeric Format on page 506. The legacy function has an additional
argument for the name or format of the returned value.

Syntax: How to Display a Value in a Specified Format

FPRINT(value, 'out_format')

where:
value
Any data type

Is the value to be converted.

Using Functions 495


FPRINT: Displaying a Value in a Specified Format

'out_format'
Fixed length alphanumeric

Is the display format. For information about valid display formats, see the Describing Data
With WebFOCUS Language manual.

Example: Displaying a Value in a Specified Format


The following request displays COGS_US as format 'D9M', and TIME_DATE as format 'YYMtrD',
by converting them to alphanumeric using FPRINT.
DEFINE FILE WF_RETAIL_LITE
COGS_A/A25 = FPRINT(COGS_US, 'D9M');
DATE1/A25 = FPRINT(TIME_DATE, 'YYMtrD');
END
TABLE FILE WF_RETAIL_LITE
PRINT LST.COGS_US COGS_A DATE1
BY TIME_DATE
WHERE RECORDLIMIT EQ 10
ON TABLE SET PAGE NOLEAD
ON TABLE SET STYLE *
GRID=OFF,$
ENDSTYLE
END

The output is shown in the following image.

496
15. Simplified Conversion Functions

HEXTYPE: Returning the Hexadecimal View of an Input Value


The HEXTYPE function returns the hexadecimal view of an input value of any data type. The
result is returned as variable length alphanumeric. The alphanumeric field to which the
hexidecimal value is returned must be large enough to hold two characters for each input
character. The value returned depends on the running operating environment.

Syntax: How to Returning the Hexadecimal View of an Input Value

HEXTYPE(in_value)

where:
in_value
Is an alphanumeric or integer field, constant, or expression.

Example: Returning a Hexadecimal View


The following request returns a hexadecimal view of the country names and the sum of the
days delayed.
DEFINE FILE WF_RETAIL_LITE
Days/I8 = DAYSDELAYED;
Country/A20 = COUNTRY_NAME;
HexCountry/A30 = HEXTYPE(Country);
END
TABLE FILE WF_RETAIL_LITE
SUM COUNTRY_NAME NOPRINT Country HexCountry Days
COMPUTE HexDays/A40 = HEXTYPE(Days);
BY COUNTRY_NAME NOPRINT
WHERE COUNTRY_NAME LT 'P'
ON TABLE SET PAGE NOPAGE
END

Using Functions 497


HEXTYPE: Returning the Hexadecimal View of an Input Value

The output is shown in the following image.

498
15. Simplified Conversion Functions

PHONETIC: Returning a Phonetic Key for a String


PHONETIC calculates a phonetic key for a string, or a null value on failure. Phonetic keys are
useful for grouping alphanumeric values, such as names, that may have spelling variations.
This is done by generating an index number that will be the same for the variations of the
same name based on pronunciation. One of two phonetic algorithms can be used for indexing,
Metaphone and Soundex. Metaphone is the default algorithm, except on z/OS where the
default is Soundex.

You can set the algorithm to use with the following command.
SET PHONETIC_ALGORITHM = {METAPHONE|SOUNDEX}

Most phonetic algorithms were developed for use with the English language. Therefore,
applying the rules to words in other languages may not give a meaningful result.

Metaphone is suitable for use with most English words, not just names. Metaphone algorithms
are the basis for many popular spell checkers.

Note: Metaphone is not optimized in generated SQL. Therefore, if you need to optimize the
request for an SQL DBMS, the SOUNDEX setting should be used.

Soundex is a legacy phonetic algorithm for indexing names by sound, as pronounced in


English.

Syntax: How to Return a Phonetic Key

PHONETIC(string)

where:
string
Alphanumeric

Is a string for which to create the key. A null value will be returned on failure.

Using Functions 499


PHONETIC: Returning a Phonetic Key for a String

Example: Generating a Phonetic Key


The following request changes the spelling of the last name for MARY SMITH to SMYTHE and
generates a phonetic key for each last name.
DEFINE FILE EMPLOYEE
LAST_NAME2/A16 = IF LAST_NAME EQ 'SMITH' AND FIRST_NAME EQ 'MARY' THEN
'SMYTHE' ELSE LAST_NAME;
PKEY/A10 = PHONETIC(LAST_NAME2);
END
TABLE FILE EMPLOYEE
PRINT FIRST_NAME LAST_NAME2
BY PKEY
ON TABLE SET PAGE NOLEAD
ON TABLE SET STYLE *
GRID=OFF,$
ENDSTYLE
END

The output is shown in the following image. Note that the two spellings for SMITH are assigned
the same index number.

500
15. Simplified Conversion Functions

TO_INTEGER: Converting a Character String to an Integer Value


TO_INTEGER converts a character string that contains a valid number consisting of digits and
an optional decimal point to an integer value. If the value contains a decimal point, the value
after the decimal point is truncated. If the value does not represent a valid number, zero (0) is
returned.

Syntax: How to Convert a Character String to an Integer

TO_INTEGER(string)

where:
string
Is a character string enclosed in single quotation marks or a character field that
represents a number containing digits and an optional decimal point.

Example: Converting a Character String to an Integer Value


The following request converts character strings to integers. Digits following the decimal point
are truncated.
DEFINE FILE WF_RETAIL_LITE
INT1/I8 = TO_INTEGER('56.78');
INT2/I8 = TO_INTEGER('.5678');
INT3/I8 = TO_INTEGER('5678');
END
TABLE FILE WF_RETAIL_LITE
PRINT INT1 INT2 INT3
BY BUSINESS_REGION AS Region
WHERE READLIMIT EQ 1
ON TABLE SET PAGE NOLEAD
ON TABLE SET STYLE *
GRID=OFF,$
ENDSTYLE
END

The output is shown in the following image.

Using Functions 501


TO_NUMBER: Converting a Character String to a Numeric Value

TO_NUMBER: Converting a Character String to a Numeric Value


TO_NUMBER converts a character string that contains a valid number consisting of digits and
an optional decimal point to the numeric format most appropriate to the context. If the value
does not represent a valid number, zero (0) is returned.

Syntax: How to Convert a Character String to a Number

TO_NUMBER(string)

where:
string
Is a character string enclosed in single quotation marks or a character field that
represents a number containing digits and an optional decimal point. This string will
be converted to a double-precision floating point number.

Example: Converting a Character String to a Number


The following request converts character strings to double-precision floating point numbers.
DEFINE FILE WF_RETAIL_LITE
NUM1/D12.1 = TO_NUMBER('56.78');
NUM2/D12.2 = TO_NUMBER('0.5678');
END
TABLE FILE WF_RETAIL_LITE
PRINT NUM1 NUM2
BY BUSINESS_REGION AS Region
WHERE READLIMIT EQ 1
ON TABLE SET PAGE NOLEAD
ON TABLE SET STYLE *
GRID=OFF,$
ENDSTYLE
END

The output is shown in the following image.

502
Chapter 16 Format Conversion Functions
Format conversion functions convert fields from one format to another.

For information on field formats see the Describing Data With WebFOCUS Language
manual

For many functions, the output argument can be supplied either as a field name or as a
format enclosed in single quotation marks. However, if a function is called from a
Dialogue Manager command, this argument must always be supplied as a format, and if
a function is called from a Maintain Data procedure, this argument must always be
supplied as a field name.
In this chapter:

ATODBL: Converting an Alphanumeric ITOPACK: Converting a Large Binary


String to Double-Precision Format Integer to Packed-Decimal Format

EDIT: Converting the Format of a Field ITOZ: Converting a Number to Zoned


Format
FPRINT: Converting Fields to
Alphanumeric Format PCKOUT: Writing a Packed Number of
Variable Length
FTOA: Converting a Number to
Alphanumeric Format PTOA: Converting a Packed-Decimal
Number to Alphanumeric Format
HEXBYT: Converting a Decimal Integer to
a Character TSTOPACK: Converting an MSSQL or
Sybase Timestamp Column to Packed
ITONUM: Converting a Large Binary
Decimal
Integer to Double-Precision Format
UFMT: Converting an Alphanumeric
String to Hexadecimal

XTPACK: Writing a Packed Number With


Up to 31 Significant Digits to an Output
File

ATODBL: Converting an Alphanumeric String to Double-Precision Format


Available Languages: reporting, Maintain

Using Functions 503


ATODBL: Converting an Alphanumeric String to Double-Precision Format

The ATODBL function converts a number in alphanumeric format to decimal (double-precision)


format.

Syntax: How to Convert an Alphanumeric String to Double-Precision Format

ATODBL(source_string, length, output)

where:
source_string
Alphanumeric
Is the string consisting of digits and, optionally, one sign and one decimal point to be
converted, or a field or variable that contains the string.
length
Alphanumeric
Is the two-character length of the source string in bytes. This can be a numeric constant,
or a field or variable that contains the value. If you specify a numeric constant, enclose it
in single quotation marks, for example '12'.
output
Double precision floating-point
Is the name of the field that contains the result, or the format of the output value enclosed
in single quotation marks.

Example: Converting an Alphanumeric Field to Double-Precision Format


ATODBL converts the EMP_ID field into double-precision format and stores the result in
D_EMP_ID:
TABLE FILE EMPLOYEE
PRINT LAST_NAME AND FIRST_NAME AND
EMP_ID AND
COMPUTE D_EMP_ID/D12.2 = ATODBL(EMP_ID, '09', D_EMP_ID);
WHERE DEPARTMENT EQ 'MIS';
END

The output is:

LAST_NAME FIRST_NAME EMP_ID D_EMP_ID


SMITH MARY 112847612 112,847,612.00
JONES DIANE 117593129 117,593,129.00
MCCOY JOHN 219984371 219,984,371.00
BLACKWOOD ROSEMARIE 326179357 326,179,357.00
GREENSPAN MARY 543729165 543,729,165.00
CROSS BARBARA 818692173 818,692,173.00

504
16. Format Conversion Functions

EDIT: Converting the Format of a Field


Available Languages: reporting

The EDIT function converts an alphanumeric field that contains numeric characters to numeric
format or converts a numeric field to alphanumeric format.

This function is useful for manipulating a field in an expression that performs an operation that
requires operands in a particular format.

When EDIT assigns a converted value to a new field, the format of the new field must
correspond to the format of the returned value. For example, if EDIT converts a numeric field to
alphanumeric format, you must give the new field an alphanumeric format:
DEFINE ALPHAPRICE/A6 = EDIT(PRICE);

EDIT deals with a symbol in the following way:

When an alphanumeric field is converted to numeric format, a sign or decimal point in the
field is stored as part of the numeric value.

Any other non-numeric characters are invalid, and EDIT returns the value zero.

When converting a floating-point or packed-decimal field to alphanumeric format, EDIT


removes the sign, the decimal point, and any number to the right of the decimal point. It
then right-justifies the remaining digits and adds leading zeros to achieve the specified field
length. Converting a number with more than nine significant digits in floating-point or
packed-decimal format may produce an incorrect result.

Syntax: How to Convert the Format of a Field

EDIT(fieldname);

where:
fieldname
Alphanumeric or Numeric
Is the field name.

Using Functions 505


FPRINT: Converting Fields to Alphanumeric Format

Example: Converting From Numeric to Alphanumeric Format


EDIT converts HIRE_DATE (a legacy date format) to alphanumeric format. CHGDAT is then able
to use the field, which it expects in alphanumeric format:

TABLE FILE EMPLOYEE


PRINT HIRE_DATE AND COMPUTE
ALPHA_HIRE/A17 = EDIT(HIRE_DATE); NOPRINT AND COMPUTE
HIRE_MDY/A17 = CHGDAT('YMD', 'MDYYX', ALPHA_HIRE, 'A17');
BY LAST_NAME BY FIRST_NAME
WHERE DEPARTMENT EQ 'MIS';
END

The output is:


LAST_NAME FIRST_NAME HIRE_DATE HIRE_MDY
--------- ---------- --------- --------
BLACKWOOD ROSEMARIE 82/04/01 APRIL 01 1982
CROSS BARBARA 81/11/02 NOVEMBER 02 1981
GREENSPAN MARY 82/04/01 APRIL 01 1982
JONES DIANE 82/05/01 MAY 01 1982
MCCOY JOHN 81/07/01 JULY 01 1981
SMITH MARY 81/07/01 JULY 01 1981

FPRINT: Converting Fields to Alphanumeric Format


The FPRINT function converts any type of field except for a text field to its alphanumeric
equivalent for display. The alphanumeric representation will include any display options that
are specified in the format of the original field.

Available Languages: reporting

Syntax: How to Convert Fields Using FPRINT

FPRINT(in_value, 'usageformat', output)

where:
in_value
Any format except TX

Is the value to be converted.


usageformat
Alphanumeric

Is the usage format of the value to be converted, including display options. The format
must be enclosed in single quotation marks.

506
16. Format Conversion Functions

output
Alphanumeric

Is the name of the output field or its format enclosed in single quotation marks.

The output format must be long enough to hold the converted number itself, with a sign
and decimal point, plus any additional characters generated by display options, such as
commas, a currency symbol, or a percent sign.

For example, D12.2 format is converted to A14 because it outputs two decimal digits, a
decimal point, a possible minus sign, up to eight integer digits, and two commas. If the
output format is not large enough, excess right-hand characters may be truncated.

Reference: Usage Notes for the FPRINT Function


The USAGE format must match the actual data in the field.

The output of FPRINT for numeric values is right-justified within the area required for the
maximum number of characters corresponding to the supplied format. This ensures that all
possible values are aligned vertically along the decimal point or units digit.

By default, the column title is left justified for alphanumeric fields. To right justify the
column title, use the /R reformatting option for the field.

Maintain Data does not support the FPRINT function. However, you can do the same type of
conversion in Maintain Data using the COMPUTE command.

Example: Converting Numeric Fields to Alphanumeric Format


The following request against the EMPLOYEE data source uses FPRINT to convert the
CURR_SAL, ED_HRS, and BANK_ACCT fields to alphanumeric for display on the report output.
Then, the STRREP function replaces the blanks in the alphanumeric representation of
CURR_SAL with asterisks. CURR_SAL has format D12.2M, so the alphanumeric representation
has format A15. The ED_HRS field has format F6.2, so the alphanumeric representation has
format A6. The BANK_ACCT field has format I9S, so the alphanumeric representation has
format A9. The alphanumeric representations of the numeric fields are right-justified. The /R
options in the PRINT command cause the column titles to be right-justified over the values:

Using Functions 507


FPRINT: Converting Fields to Alphanumeric Format

DEFINE FILE EMPLOYEE


ASAL/A15 = FPRINT(CURR_SAL, 'D12.2M', ASAL);
ASAL/A15 = STRREP(15, ASAL, 1, ' ', 1, '*', 15, ASAL);
AED/A6 = FPRINT(ED_HRS, 'F6.2', AED);
ABANK/A9 = FPRINT(BANK_ACCT, 'I9S', ABANK);
END
TABLE FILE EMPLOYEE
PRINT CURR_SAL ASAL
ED_HRS AED/R
BANK_ACCT ABANK/R
WHERE BANK_NAME NE ' '
ON TABLE SET PAGE NOPAGE
END

The output is:


CURR_SAL ASAL ED_HRS AED BANK_ACCT ABANK
-------- ---- ------ ------ --------- ---------
$18,480.00 *****$18,480.00 50.00 50.00 40950036 40950036
$29,700.00 *****$29,700.00 .00 .00 160633 160633
$26,862.00 *****$26,862.00 30.00 30.00 819000702 819000702
$21,780.00 *****$21,780.00 75.00 75.00 122850108 122850108
$16,100.00 *****$16,100.00 50.00 50.00 136500120 136500120
$27,062.00 *****$27,062.00 45.00 45.00 163800144 163800144

Example: Converting Alphanumeric and Numeric Date Fields to Alphanumeric Format


The following request against the EMPLOYEE data source converts the HIRE_DATE field to
alphanumeric format. It also creates an alphanumeric date field named ADATE and converts it
to its alphanumeric representation. The HIRE_DATE field has format I6YMD and the ADATE
field has format A6YMD, so the alphanumeric representations have format A8 to account for
the slashes between the date components. The /R option right-justifies the column titles over
the field values:
DEFINE FILE EMPLOYEE
AHDATE/A8 = FPRINT(HIRE_DATE,'I6YMD', AHDATE);
ADATE/A6YMD = EDIT(HIRE_DATE);
AADATE/A8 = FPRINT(ADATE,'A6YMD', AADATE);
END
TABLE FILE EMPLOYEE
PRINT HIRE_DATE AHDATE/R
ADATE AADATE/R
ON TABLE SET PAGE NOPAGE
END

The output is:

508
16. Format Conversion Functions

HIRE_DATE AHDATE ADATE AADATE


--------- -------- ----- --------
80/06/02 80/06/02 80/06/02 80/06/02
81/07/01 81/07/01 81/07/01 81/07/01
82/05/01 82/05/01 82/05/01 82/05/01
82/01/04 82/01/04 82/01/04 82/01/04
82/08/01 82/08/01 82/08/01 82/08/01
82/01/04 82/01/04 82/01/04 82/01/04
82/07/01 82/07/01 82/07/01 82/07/01
81/07/01 81/07/01 81/07/01 81/07/01
82/04/01 82/04/01 82/04/01 82/04/01
82/02/02 82/02/02 82/02/02 82/02/02
82/04/01 82/04/01 82/04/01 82/04/01
81/11/02 81/11/02 81/11/02 81/11/02

Example: Converting a Date Field to Alphanumeric Format


The following request against the VIDEOTRK data source converts the TRANSDATE (YMD) field
to alphanumeric format. The alphanumeric representation has format A8 to account for the
slashes between the date components:
DEFINE FILE VIDEOTRK
ALPHA_DATE/A8 = FPRINT(TRANSDATE,'YMD', ALPHA_DATE);
END
TABLE FILE VIDEOTRK
PRINT TRANSDATE ALPHA_DATE
WHERE TRANSDATE LE '91/06/20'
ON TABLE SET PAGE NOPAGE
END

The output is:


TRANSDATE ALPHA_DATE
--------- ----------
91/06/19 91/06/19
91/06/17 91/06/17
91/06/20 91/06/20
91/06/19 91/06/19
91/06/18 91/06/18
91/06/17 91/06/17
91/06/17 91/06/17
91/06/17 91/06/17
91/06/20 91/06/20
91/06/19 91/06/19
91/06/18 91/06/18
91/06/19 91/06/19
91/06/18 91/06/18
91/06/20 91/06/20
91/06/18 91/06/18
91/06/20 91/06/20
91/06/19 91/06/19
91/06/17 91/06/17

Using Functions 509


FPRINT: Converting Fields to Alphanumeric Format

Example: Converting a Date-Time Field to Alphanumeric Format and Creating a HOLD File
The following request against the VIDEOTR2 data source converts the TRANSDATE (HYYMDI)
field to alphanumeric format. The alphanumeric representation has format A16 to account for a
four-digit year, two-digit month, two-digit day, two slashes between the date components, a
space between the date and time, a two-digit hour, a colon between the hour and minute
components, and a two-digit minute:
DEFINE FILE VIDEOTR2
DATE/I4 = HPART(TRANSDATE, 'YEAR', 'I4');
ALPHA_DATE/A16 = FPRINT(TRANSDATE,'HYYMDI', ALPHA_DATE);
END
TABLE FILE VIDEOTR2
PRINT TRANSDATE ALPHA_DATE/R
WHERE DATE EQ '1991'
ON TABLE SET PAGE NOPAGE
END

The output is:


TRANSDATE ALPHA_DATE
--------- ----------------
1991/06/27 02:45 1991/06/27 02:45
1991/06/20 05:15 1991/06/20 05:15
1991/06/21 07:11 1991/06/21 07:11
1991/06/21 01:10 1991/06/21 01:10
1991/06/19 07:18 1991/06/19 07:18
1991/06/19 04:11 1991/06/19 04:11
1991/06/25 01:19 1991/06/25 01:19
1991/06/24 04:43 1991/06/24 04:43
1991/06/24 02:08 1991/06/24 02:08
1991/06/25 01:17 1991/06/25 01:17
1991/06/27 01:17 1991/06/27 01:17
1991/11/17 11:28 1991/11/17 11:28
1991/06/24 10:27 1991/06/24 10:27

If you hold the output in a comma-delimited or other alphanumeric output file, you can see that
while the original field propagates only the numeric representation of the value, the converted
field propagates the display options as well:
DEFINE FILE VIDEOTR2
DATE/I4 = HPART(TRANSDATE, 'YEAR', 'I4');
ALPHA_DATE/A16 = FPRINT(TRANSDATE,'HYYMDI', ALPHA_DATE);
END
TABLE FILE VIDEOTR2
PRINT TRANSDATE ALPHA_DATE/R
WHERE DATE EQ '1991'
ON TABLE HOLD FORMAT COMMA
END

The HOLD file follows. The first field represents the original data, and the second field contains
the converted values with display options:

510
16. Format Conversion Functions

"19910627024500000","1991/06/27 02:45"
"19910620051500000","1991/06/20 05:15"
"19910621071100000","1991/06/21 07:11"
"19910621011000000","1991/06/21 01:10"
"19910619071800000","1991/06/19 07:18"
"19910619041100000","1991/06/19 04:11"
"19910625011900000","1991/06/25 01:19"
"19910624044300000","1991/06/24 04:43"
"19910624020800000","1991/06/24 02:08"
"19910625011700000","1991/06/25 01:17"
"19910627011700000","1991/06/27 01:17"
"19911117112800000","1991/11/17 11:28"
"19910624102700000","1991/06/24 10:27"

FTOA: Converting a Number to Alphanumeric Format


Available Languages: reporting, Maintain

The FTOA function converts a number up to 16 digits long from numeric format to alphanumeric
format. It retains the decimal positions of the number and right-justifies it with leading spaces.
You can also add edit options to a number converted by FTOA.

When using FTOA to convert a number containing decimals to a character string, you must
specify an alphanumeric format large enough to accommodate both the integer and decimal
portions of the number. For example, a D12.2 format is converted to A14. If the output format
is not large enough, decimals are truncated.

Syntax: How to Convert a Number to Alphanumeric Format

FTOA(number, '(format)', output)

where:
number
Numeric F or D (single and double precision floating-point)

Is the number to be converted, or the name of the field that contains the number.
format
Alphanumeric

Is the format of the number to be converted enclosed in parentheses. Only floating point
single-precision and double-precision formats are supported. Include any edit options that
you want to appear in the output. The D (floating-point double-precision) format
automatically supplies commas.

If you use a field name for this argument, specify the name without quotation marks or
parentheses. If you specify a format, the format must be enclosed in single quotation
marks and parentheses.

Using Functions 511


HEXBYT: Converting a Decimal Integer to a Character

output
Alphanumeric

Is the name of the field that contains the result, or the format of the output value enclosed
in single quotation marks. The length of this argument must be greater than the length of
number and must account for edit options and a possible negative sign.

Example: Converting From Numeric to Alphanumeric Format


FTOA converts the GROSS field from floating point double-precision to alphanumeric format and
stores the result in ALPHA_GROSS:

TABLE FILE EMPLOYEE


PRINT GROSS AND COMPUTE
ALPHA_GROSS/A15 = FTOA(GROSS, '(D12.2)', ALPHA_GROSS);
BY HIGHEST 1 PAY_DATE NOPRINT
BY LAST_NAME
WHERE (GROSS GT 800) AND (GROSS LT 2300);
END

The output is:


LAST_NAME GROSS ALPHA_GROSS
--------- ----- -----------
BLACKWOOD $1,815.00 1,815.00
CROSS $2,255.00 2,255.00
IRVING $2,238.50 2,238.50
JONES $1,540.00 1,540.00
MCKNIGHT $1,342.00 1,342.00
ROMANS $1,760.00 1,760.00
SMITH $1,100.00 1,100.00
STEVENS $916.67 916.67

HEXBYT: Converting a Decimal Integer to a Character


Available Languages: reporting, Maintain

The HEXBYT function obtains the ASCII, EBCDIC, or Unicode character equivalent of a decimal
integer, depending on your configuration and operating environment. The decimal value you
specify must be the value associated with the character on the configured code page. HEXBYT
returns a single alphanumeric character in the ASCII, EBCDIC, or Unicode character set. You
can use this function to produce characters that are not on your keyboard, similar to the
CTRAN function.

In Unicode configurations, this function uses values in the range:

0 to 255 for 1-byte characters.

256 to 65535 for 2-byte characters.

512
16. Format Conversion Functions

65536 to 16777215 for 3-byte characters.

16777216 to 4294967295 for 4-byte characters (primarily for EBCDIC).

The display of special characters depends on your software and hardware; not all special
characters may appear.

Syntax: How to Convert a Decimal Integer to a Character

HEXBYT(decimal_value, output)

where:
decimal_value
Integer

Is the decimal integer to be converted to a single character. In non-Unicode environments,


a value greater than 255 is treated as the remainder of decimal_value divided by 256. The
decimal value you specify must be the value associated with the character on the
configured code page.
output
Alphanumeric

Is the name of the field that contains the result, or the format of the output value enclosed
in single quotation marks (').

Example: Converting a Decimal Integer to a Character in ASCII and Unicode


The following request uses HEXBYT to convert the decimal integer value 130 to the comma
character on ASCII code page 1252. The comma is then concatenated between LAST_NAME
and FIRST_NAME to create the NAME field:
TABLE FILE EMPLOYEE
PRINT LAST_NAME AND
COMPUTE COMMA1/A1 = HEXBYT(130, COMMA1); NOPRINT
COMPUTE NAME/A40 = LAST_NAME || COMMA1| ' '| FIRST_NAME;
BY LAST_NAME NOPRINT
BY FIRST_NAME
WHERE DEPARTMENT EQ 'MIS';
ON TABLE SET PAGE NOLEAD
ON TABLE SET STYLE *
GRID=OFF,$
ENDSTYLE
END

Using Functions 513


HEXBYT: Converting a Decimal Integer to a Character

The output is shown in the following image.

To produce the same output in a Unicode environment configured for code page 65001,
replace the COMPUTE command for the field COMMA1 with the following syntax, in which the
call to HEXBYT converts the integer value 14844058 to the comma character:
COMPUTE COMMA1/A1 = HEXBYT(14844058, COMMA1); NOPRINT

Example: Converting a Decimal Integer to a Character


HEXBYT converts LAST_INIT_CODE to its character equivalent and stores the result in
LAST_INIT:
TABLE FILE EMPLOYEE
PRINT LAST_NAME AND
COMPUTE LAST_INIT_CODE/I3 = BYTVAL(LAST_NAME, 'I3');
COMPUTE LAST_INIT/A1 = HEXBYT(LAST_INIT_CODE, LAST_INIT);
WHERE DEPARTMENT EQ 'MIS';
END

The output for an ASCII platform is:


LAST_NAME LAST_INIT_CODE LAST_INIT
--------- -------------- ---------
SMITH 83 S
JONES 74 J
MCCOY 77 M
BLACKWOOD 66 B
GREENSPAN 71 G
CROSS 67 C

514
16. Format Conversion Functions

The output for an EBCDIC platform is:


LAST_NAME LAST_INIT_CODE LAST_INIT
--------- -------------- ---------
SMITH 226 S
JONES 209 J
MCCOY 212 M
BLACKWOOD 194 B
GREENSPAN 199 G
CROSS 195 C

ITONUM: Converting a Large Binary Integer to Double-Precision Format


Available Languages: reporting, Maintain

The ITONUM function converts a large binary integer in a non-FOCUS data source to double-
precision format.

Some programming languages and some non-FOCUS data storage systems use large binary
integer formats. However, large binary integers (more than 4 bytes in length) are not supported
in the Master File so they require conversion to double-precision format.

You must specify how many of the right-most bytes in the input field are significant. The result
is an 8-byte double-precision field.

Syntax: How to Convert a Large Binary Integer to Double-Precision Format

ITONUM(maxbytes, infield, output)

where:
maxbytes
Numeric

Is the maximum number of bytes in the 8-byte binary input field that have significant
numeric data, including the binary sign. Valid values are:

5 ignores the left-most 3 bytes.

6 ignores the left-most 2 bytes.

7 ignores the left-most byte.

infield
A8

Is the field that contains the binary number. Both the USAGE and ACTUAL formats of the
field must be A8.

Using Functions 515


ITOPACK: Converting a Large Binary Integer to Packed-Decimal Format

output
Double precision floating-point (Dn)

Is the name of the field that contains the result, or the format of the output value enclosed
in single quotation marks. The format must be Dn.

Example: Converting a Large Binary Integer to Double-Precision Format


Suppose a binary number in an external file has the following COBOL format:
PIC 9(8)V9(4) COMP

It is defined in the EUROCAR Master File as a field named BINARYFLD. Its field formats are
USAGE=A8 and ACTUAL=A8, since its length is greater than 4 bytes.

The following request converts the field to double-precision format:


DEFINE FILE EUROCAR
MYFLD/D14 = ITONUM(6, BINARYFLD, MYFLD);
END
TABLE FILE EUROCAR
PRINT MYFLD BY CAR
END

ITOPACK: Converting a Large Binary Integer to Packed-Decimal Format


Available Languages: reporting, Maintain

The ITOPACK function converts a large binary integer in a non-FOCUS data source to packed-
decimal format.

Some programming languages and some non-FOCUS data storage systems use double-word
binary integer formats. These are similar to the single-word binary integers used by FOCUS, but
they allow larger numbers. However, large binary integers (more than 4 bytes in length) are not
supported in the Master File so they require conversion to packed-decimal format.

You must specify how many of the right-most bytes in the input field are significant. The result
is an 8-byte packed-decimal field of up to 15 significant numeric positions (for example, P15 or
P16.2).

Limit: For a field defined as 'PIC 9(15) COMP' or the equivalent (15 significant digits), the
maximum number that can be converted is 167,744,242,712,576.

516
16. Format Conversion Functions

Syntax: How to Convert a Large Binary Integer to Packed-Decimal Format

ITOPACK(maxbytes, infield, output)

where:
maxbytes
Numeric

Is the maximum number of bytes in the 8-byte binary input field that have significant
numeric data, including the binary sign.

Valid values are:

5 ignores the left-most 3 bytes (up to 11 significant positions).

6 ignores the left-most 2 bytes (up to 14 significant positions).

7 ignores the left-most byte (up to 15 significant positions).


infield
A8

Is the field that contains the binary number. Both the USAGE and ACTUAL formats of the
field must be A8.
output
Numeric

Is the name of the field that contains the result, or the format of the output value enclosed
in single quotation marks. The format must be Pn or Pn.d.

Example: Converting a Large Binary Integer to Packed-Decimal Format


Suppose a binary number in an external file has the following COBOL format:
PIC 9(8)V9(4) COMP

It is defined in the EUROCAR Master File as a field named BINARYFLD. Its field formats are
USAGE=A8 and ACTUAL=A8, since its length is greater than 4 bytes.

Using Functions 517


ITOZ: Converting a Number to Zoned Format

The following request converts the field to packed-decimal format:


DEFINE FILE EUROCAR
PACKFLD/P14.4 = ITOPACK(6, BINARYFLD, PACKFLD);
END
TABLE FILE EUROCAR
PRINT PACKFLD BY CAR
END

ITOZ: Converting a Number to Zoned Format


Available Languages: reporting, Maintain

The ITOZ function converts a number in numeric format to zoned-decimal format. Although a
request cannot process zoned numbers, it can write zoned fields to an extract file for use by
an external program.

Syntax: How to Convert a Number to Zoned Format

ITOZ(length, in_value, output)

where:
length
Integer

Is the length of in_value in bytes. The maximum number of bytes is 15. The last byte
includes the sign.
in_value
Numeric

Is the number to be converted, or the field that contains the number. The number is
truncated to an integer before it is converted.
output
Alphanumeric

Is the name of the field that contains the result, or the format of the output value enclosed
in single quotation marks.

518
16. Format Conversion Functions

Example: Converting a Number to Zoned Format


The following request creates an extract file containing employee IDs and salaries in zoned
format for a COBOL program:
DEFINE FILE EMPLOYEE
ZONE_SAL/A8 = ITOZ(8, CURR_SAL, ZONE_SAL);
END

TABLE FILE EMPLOYEE


PRINT CURR_SAL ZONE_SAL BY EMP_ID
ON TABLE SAVE AS SALARIES
END

The resulting extract file is:


NUMBER OF RECORDS IN TABLE= 12 LINES= 12

ALPHANUMERIC RECORD NAMED SALARIES


FIELDNAME ALIAS FORMAT LENGTH
EMP_ID EID A9 9
CURR_SAL CSAL D12.2M 12
ZONE_SAL A8 8
TOTAL 29

PCKOUT: Writing a Packed Number of Variable Length


Available Languages: reporting, Maintain

The PCKOUT function writes a packed-decimal number of variable length to an extract file.
When a request saves a packed number to an extract file, it typically writes it as an 8- or 16-
byte field regardless of its format specification. With PCKOUT, you can vary the field's length
between 1 to 16 bytes.

Syntax: How to Write a Packed Number of Variable Length

PCKOUT(in_value, length, output)

where:
in_value
Numeric

Is the input field that contains the values. It can be in packed, integer, single- or double-
precision floating point format. If it is not in integer format, it is rounded to the nearest
whole number.
length
Numeric

Using Functions 519


PTOA: Converting a Packed-Decimal Number to Alphanumeric Format

Is the length of the output value, from 1 to 16 bytes.


output
Alphanumeric

Is the name of the field that contains the result, or the format of the output value enclosed
in single quotation marks. The function returns the field as alphanumeric although it
contains packed data.

Example: Writing a Packed Number of Variable Length


PCKOUT converts the CURR_SAL field to a 5-byte packed field and stores the result in
SHORT_SAL:
DEFINE FILE EMPLOYEE
SHORT_SAL/A5 = PCKOUT(CURR_SAL, 5, SHORT_SAL);
END
TABLE FILE EMPLOYEE
PRINT LAST_NAME SHORT_SAL HIRE_DATE
ON TABLE SAVE
END

The resulting extract file is:


NUMBER OF RECORDS IN TABLE= 12 LINES= 12

ALPHANUMERIC RECORD NAMED SAVE


FIELDNAME ALIAS FORMAT LENGTH
LAST_NAME LN A15 15
SHORT_SAL A5 5
HIRE_DATE HDT I6YMD 6
TOTAL 26

PTOA: Converting a Packed-Decimal Number to Alphanumeric Format


Available Languages: reporting, Maintain

The PTOA function converts a packed-decimal number from numeric format to alphanumeric
format. It retains the decimal positions of the number and right-justifies it with leading spaces.
You can also add edit options to a number converted by PTOA.

When using PTOA to convert a number containing decimals to a character string, you must
specify an alphanumeric format large enough to accommodate both the integer and decimal
portions of the number. For example, a P12.2C format is converted to A14. If the output
format is not large enough, the right-most characters are truncated.

520
16. Format Conversion Functions

Syntax: How to Convert a Packed-Decimal Number to Alphanumeric Format

PTOA(number, '(format)', output)

where:
number
Numeric P (packed-decimal)

Is the number to be converted, or the name of the field that contains the number.
format
Alphanumeric

Is the format of the number enclosed in both single quotation marks and parentheses.

Only packed-decimal format is supported. Include any edit options that you want to display
in the output.

The format value does not require the same length or number of decimal places as the
original field. If you change the number of decimal places, the result is rounded. If you
make the length too short to hold the integer portion of the number, asterisks appear
instead of the number.

If you use a field name for this argument, specify the name without quotation marks or
parentheses. However, parentheses must be included around the format stored in this
field. For example:
FMT/A10 = '(P12.2C)';

You can then use this field as the format argument when using the function in your
request:
COMPUTE ALPHA_GROSS/A20 = PTOA(PGROSS, FMT, ALPHA_GROSS);

output
Alphanumeric

Is the name of the field that contains the result, or the format of the output value enclosed
in single quotation marks. The length of this argument must be greater than the length of
number and must account for edit options and a possible negative sign.

Using Functions 521


TSTOPACK: Converting an MSSQL or Sybase Timestamp Column to Packed Decimal

Example: Converting From Packed to Alphanumeric Format


PTOA is called twice to convert the PGROSS field from packed-decimal to alphanumeric format.
The format specified in the first call to the function is stored in a virtual field named FMT. The
format specified in the second call to the function does not include decimal places, so the
value is rounded when it appears:

DEFINE FILE EMPLOYEE


PGROSS/P18.2=GROSS;
FMT/A10='(P14.2C)';
END
TABLE FILE EMPLOYEE PRINT PGROSS NOPRINT
COMPUTE AGROSS/A17 = PTOA(PGROSS, FMT, AGROSS); AS ''
COMPUTE BGROSS/A37 = '<- THIS AMOUNT IS' |
PTOA(PGROSS, '(P5C)', 'A6') |
' WHEN ROUNDED'; AS '' IN +1
BY HIGHEST 1 PAY_DATE NOPRINT
BY LAST_NAME NOPRINT
END

The output is:


2,475.00 <- THIS AMOUNT IS 2,475 WHEN ROUNDED
1,815.00 <- THIS AMOUNT IS 1,815 WHEN ROUNDED
2,255.00 <- THIS AMOUNT IS 2,255 WHEN ROUNDED
750.00 <- THIS AMOUNT IS 750 WHEN ROUNDED
2,238.50 <- THIS AMOUNT IS 2,239 WHEN ROUNDED
1,540.00 <- THIS AMOUNT IS 1,540 WHEN ROUNDED
1,540.00 <- THIS AMOUNT IS 1,540 WHEN ROUNDED
1,342.00 <- THIS AMOUNT IS 1,342 WHEN ROUNDED
1,760.00 <- THIS AMOUNT IS 1,760 WHEN ROUNDED
1,100.00 <- THIS AMOUNT IS 1,100 WHEN ROUNDED
791.67 <- THIS AMOUNT IS 792 WHEN ROUNDED
916.67 <- THIS AMOUNT IS 917 WHEN ROUNDED

TSTOPACK: Converting an MSSQL or Sybase Timestamp Column to Packed Decimal


This function applies to the Microsoft SQL Server and Sybase adapters only.

Microsoft SQL Server and Sybase have a data type called TIMESTAMP. Rather than containing
an actual timestamp, columns with this data type contain a number that is incremented for
each record inserted or updated in the data source. This timestamp comes from a common
area, so no two tables in the database have the same timestamp column value. The value is
stored in Binary(8) or Varbinary(8) format in the table, but is returned as a double wide
alphanumeric column (A16). You can use the TSTOPACK function to convert the timestamp
value to packed decimal.

Syntax: How to Convert an MSSQL or Sybase Timestamp Column to Packed Decimal

TSTOPACK(tscol, output);

522
16. Format Conversion Functions

where:
tscol
A16

Is the timestamp column to be converted.


output
P21

Is the name of the field that contains the result, or the format of the output value enclosed
in single quotation marks (‘).

Example: Converting a Microsoft SQL Server Timestamp Column to Packed Decimal


The following CREATE TABLE command creates a SQL Server table name TSTEST that contains
an integer counter column named I and a timestamp column named TS:
SQL SQLMSS
CREATE TABLE TSTEST (I INT, TS timestamp) ;
END

The Master File for the TSTEST data source follows. The field TS represents the TIMESTAMP
column:
FILENAME=TSTEST, SUFFIX=SQLMSS , $
SEGMENT=TSTEST, SEGTYPE=S0, $
FIELDNAME=I, ALIAS=I, USAGE=I11, ACTUAL=I4,
MISSING=ON, $
FIELDNAME=TS, ALIAS=TS, USAGE=A16, ACTUAL=A16, FIELDTYPE=R, $

Note: When you generate a synonym for a table with a TIMESTAMP column, the TIMESTAMP
column is created as read-only (FIELDTYPE=R).

TSTOPACK converts the timestamp column TS to packed decimal:


DEFINE FILE TSTEST
TSNUM/P21=TSTOPACK(TS,'P21');
END
TABLE FILE TEST64
PRINT I TS TSNUM
END

Using Functions 523


UFMT: Converting an Alphanumeric String to Hexadecimal

The output is:

UFMT: Converting an Alphanumeric String to Hexadecimal


Available Languages: reporting, Maintain

The UFMT function converts characters in an alphanumeric source string to their hexadecimal
representation. This function is useful for examining data of unknown format. As long as you
know the length of the data, you can examine its content.

Syntax: How to Convert an Alphanumeric String to Hexadecimal

UFMT(source_string, length, output)

where:
source_string
Alphanumeric

Is the alphanumeric string to convert enclosed in single quotation marks ('), or the field
that contains the string.
length
Integer

Is the number of characters in source_string.

524
16. Format Conversion Functions

output
Alphanumeric

Is the name of the field that contains the result, or the format of the output value enclosed
in single quotation marks ('). The format of output must be alphanumeric and its length
must be twice that of length.

Example: Converting an Alphanumeric String to Hexadecimal


UFMT converts each value in JOBCODE to its hexadecimal representation and stores the result
in HEXCODE:
DEFINE FILE JOBFILE
HEXCODE/A6 = UFMT(JOBCODE, 3, HEXCODE);
END
TABLE FILE JOBFILE
PRINT JOBCODE HEXCODE
END

The output is:


JOBCODE HEXCODE
------- -------
A01 C1F0F1
A02 C1F0F2
A07 C1F0F7
A12 C1F1F2
A14 C1F1F4
A15 C1F1F5
A16 C1F1F6
A17 C1F1F7
B01 C2F0F1
B02 C2F0F2
B03 C2F0F3
B04 C2F0F4
B14 C2F1F4

XTPACK: Writing a Packed Number With Up to 31 Significant Digits to an Output File


The XTPACK function stores packed numbers with up to 31 significant digits in an
alphanumeric field, retaining decimal data. This permits writing a short or long packed field of
any length, 1 to 16 bytes, to an output file.

Using Functions 525


XTPACK: Writing a Packed Number With Up to 31 Significant Digits to an Output File

Syntax: How to Store Packed Values in an Alphanumeric Field

XTPACK(in_value, outlength, outdec, output)

where:
infield
Numeric

Is the packed value.


outlength
Numeric

Is the length of the alphanumeric field that will hold the converted packed field. Can be
from 1 to 16.
outdec
Numeric

Is the number of decimal positions for output.


output
Alphanumeric

Is the name of the field to contain the result or the format of the field enclosed in single
quotation marks.

Example: Writing a Long Packed Number to an Output File


The following request creates a long packed decimal field named LONGPCK. ALPHAPCK (format
A13) is the result of applying XTPACK to the long packed field. PCT_INC, LONGPCK, and
ALPHAPCK are then written to a SAVE file named XTOUT.
DEFINE FILE EMPLOYEE
LONGPCK/P25.2 = PCT_INC + 11111111111111111111;
ALPHAPCK/A13 = XTPACK(LONGPCK,13,2,'A13');
END
TABLE FILE EMPLOYEE
PRINT PCT_INC LONGPCK ALPHAPCK
WHERE PCT_INC GT 0
ON TABLE SAVE AS XTOUT
END

526
16. Format Conversion Functions

The SAVE file has the following fields and formats:


ALPHANUMERIC RECORD NAMED XTOUT
FIELDNAME ALIAS FORMAT LENGTH
PCT_INC PI F6.2 6
LONGPCK P25.2 25
ALPHAPCK A13 13
TOTAL 44
SAVED...

Using Functions 527


XTPACK: Writing a Packed Number With Up to 31 Significant Digits to an Output File

528
Chapter 17 Maintain-specific Light Update Support
Functions
Light update support functions retrieve WebFOCUS parameter or variable data implicitly
from within a Maintain Data procedure.

In this chapter:

IWC.FindAppCGIValue: Retrieving a WebFOCUS Parameter or Variable Value

IWC.GetAppCGIValue: Importing a WebFOCUS Parameter or Variable

IWC.FindAppCGIValue: Retrieving a WebFOCUS Parameter or Variable Value

The IWC.FindAppCGIValue function retrieves WebFOCUS parameter or variable values by pairing


the WebFOCUS parameter or variable name with the Maintain Data variable name to which the
value is assigned.

Note: Unlike Maintain variables, WebFOCUS parameters and variables are case-sensitive.

Syntax: How to Retrieve a WebFOCUS Parameter or Variable Value

IWC.FindAppCGIValue(parm,mnt_var);

where:
parm
Is the WebFOCUS parameter or variable whose value you are retrieving. This value is case-
sensitive, and must be alphanumeric.
mnt_var
Is the Maintain Data variable that receives the value of the WebFOCUS parameter or
variable.

Using Functions 529


IWC.GetAppCGIValue: Importing a WebFOCUS Parameter or Variable

Example: Retrieving a WebFOCUS Variable Value From a Launch Form


IWC.findAppCgiValue retrieves the user name and password from the IBIC_user and IBIC_pass
variables, respectively:
Maintain
COMPUTE username/A8;
COMPUTE password/A8;
IWC.findAppCgiValue("IBIC_user", username);
IWC.findAppCgiValue("IBIC_pass", password);

Example: Retrieving Parameterized Data From Excel


IWC.findAppCgiValue retrieves the values for fields listed in an Excel file:
MAINTAIN FILE car
MODULE IMPORT (webbase2 errors);
Case Top
compute xlsRetail_Cost/a0;
Infer car.ORIGIN.COUNTRY car.COMP.CAR car.CARREC.MODEL
car.BODY.BODYTYPE car.BODY.RETAIL_COST into car_stack;
car_stack.FocCount=1;
car_stack.FocIndex=1;
iwc.findAppCgiValue("COUNTRY",car_stack.country);
iwc.findAppCgiValue("CAR",car_stack.car);
iwc.findAppCgiValue("MODEL",car_stack.model);
iwc.findAppCgiValue("BODYTYPE",car_stack.bodytype);
iwc.findAppCgiValue("RETAIL_COST",xlsRetail_Cost);
car_stack.retail_cost = xlsRetail_Cost;
update car.BODY.RETAIL_COST from car_stack;
EndCase
END

IWC.GetAppCGIValue: Importing a WebFOCUS Parameter or Variable

The IWC.GetAppCGIValue function imports the value of a WebFOCUS parameter or variable into
a Maintain Data variable. IWC.GetAppCGIValue returns a value from the HTTP request header if
the name of the variable or parameter is passed. If the name is not found, the function returns
a null value. Therefore, you can check for errors by looking for a null value, then handle the
error as needed.

Note: Unlike Maintain variables, WebFOCUS parameters and variables are case-sensitive.

530
17. Maintain-specific Light Update Support Functions

Syntax: How to Import a WebFOCUS Parameter

Declare mnt_var/type_length = IWC.GetAppCGIValue(parm);

where:
mnt_var
Is the Maintain Data variable that receives the ASCII return value of the WebFOCUS
parameter or variable. The value should be unescaped before being passed to the
Maintain variable.
type_length
Is the selected type and length of the Maintain Data variable.
parm
Is the WebFOCUS parameter or variable to import. This value is case-sensitive, and must
be alphanumeric.

Example: Importing a WebFOCUS Parameter


IWC.getAppCGIValue imports the WebFOCUS parameter PRODUCT_ID to Maintain Data:
Maintain File GGPRODS
Infer Product_ID into prodstk;
Declare pcode/a4=IWC.getAppCGIValue("PRODUCT_ID");
For 1 next Product_ID into prodstk where Product_ID eq
pcode;

Using Functions 531


IWC.GetAppCGIValue: Importing a WebFOCUS Parameter or Variable

532
Chapter 18 Simplified Numeric Functions
Numeric functions have been developed that make it easier to understand and enter the
required arguments. These functions have streamlined parameter lists, similar to those
used by SQL functions. In some cases, these simplified functions provide slightly
different functionality than previous versions of similar functions.

The simplified functions do not have an output argument. Each function returns a value
that has a specific data type.

When used in a request against a relational data source, these functions are optimized
(passed to the RDBMS for processing).
Note:

The simplified numeric functions are supported in Dialogue Manager.

The simplified numeric functions are not supported in Maintain Data.


In this chapter:

ASCII: Returning the ASCII Code for the Leftmost Character in a String

CEILING: Returning the Smallest Integer Value Greater Than or Equal to a Value

EXPONENT: Raising e to a Power

FLOOR: Returning the Largest Integer Less Than or Equal to a Value

LOG10: Calculating the Base 10 Logarithm

MOD: Calculating the Remainder From a Division

POWER: Raising a Value to a Power

ROUND: Rounding a Number to a Given Number of Decimal Places

SIGN: Returning the Sign of a Number

TRUNCATE: Truncating a Number to a Given Number of Decimal Places

Using Functions 533


ASCII: Returning the ASCII Code for the Leftmost Character in a String

ASCII: Returning the ASCII Code for the Leftmost Character in a String
ASCII takes a character string and returns the ASCII code in integer format for the leftmost
character in the string.

Syntax: How to Return the ASCII Code for the Leftmost Character in a String

ASCII(charexp)

where:
charexp
Is any character string.

Example: Returning the ASCII Code for the Leftmost Character in a String
In the following request, ASCII returns the ASCII code for the leftmost character in the
CATEGORY field.
TABLE FILE GGSALES
SUM DOLLARS NOPRINT
AND COMPUTE
ASCII_CODE/I9 = ASCII(CATEGORY);
BY CATEGORY
ON TABLE SET PAGE NOLEAD
ON TABLE SET STYLE *
GRID=OFF,$
ENDSTYLE
END

The output is shown in the following image.

CEILING: Returning the Smallest Integer Value Greater Than or Equal to a Value
CEILING returns the smallest integer value that is greater than or equal to a number.

Syntax: How to Return the Smallest Integer Greater Than or Equal to a Number

CEILING(number)

534
18. Simplified Numeric Functions

where:
number
Numeric

Is the number whose ceiling will be returned. The output data type is the same as the
input data type.

Example: Returning the Ceiling of a Number


In the following request, CEILING returns the smallest integer greater than or equal to the
GROSS_PROFIT_US value.
DEFINE FILE WF_RETAIL_LITE
CEIL1/D7.2= CEILING(GROSS_PROFIT_US);
END
TABLE FILE WF_RETAIL_LITE
PRINT GROSS_PROFIT_US/D9.2 CEIL1
ON TABLE SET PAGE NOPAGE
END

Using Functions 535


EXPONENT: Raising e to a Power

The partial output follows. Note that even though the value returned is an integer, it is returned
with the same format as the CEIL1 field (D7.2):

Gross Profit CEIL1


------------ -----
165.00 165.00
13.99 14.00
60.99 61.00
225.98 226.00
79.99 80.00
44.59 45.00
94.30 95.00
238.50 239.00
199.99 200.00
68.99 69.00
63.58 64.00
129.99 130.00
37.49 38.00
75.99 76.00
13.99 14.00
119.00 119.00
-30.01 -30.00
54.99 55.00
189.98 190.00
44.59 45.00
91.98 92.00
89.00 89.00
59.50 60.00
129.99 130.00
54.00 54.00
109.98 110.00
98.99 99.00
98.99 99.00
99.99 100.00
44.59 45.00

EXPONENT: Raising e to a Power


EXPONENT raises the constant e to a power.

Syntax: How to Raise the Constant e to a Power

EXPONENT(power)

where:
power
Numeric

Is the power to which to raise e. The output data type is numeric.

536
18. Simplified Numeric Functions

Example: Raising e to a Power


The following request prints the value of e and the value of e raised to the fifth power.
DEFINE FILE WF_RETAIL_LITE
EXP1/D12.5 = EXPONENT(1);
EXP2/D12.5 = EXPONENT(5);
END
TABLE FILE WF_RETAIL_LITE
PRINT EXP1 EXP2
BY BUSINESS_REGION AS Region
WHERE BUSINESS_REGION EQ 'EMEA'
WHERE RECORDLIMIT EQ 1
ON TABLE SET PAGE NOPAGE
END

The output is shown in the following image.

FLOOR: Returning the Largest Integer Less Than or Equal to a Value


FLOOR returns the largest integer value that is less than or equal to a number.

Syntax: How to Return the Largest Integer Less Than or Equal to a Number

FLOOR(number)

where:
number
Numeric

Is the number whose floor will be returned. The output data type is the same as the input
data type.

Using Functions 537


LOG10: Calculating the Base 10 Logarithm

Example: Returning the Floor of a Number


In the following request, FLOOR returns the largest integer less than or equal to the
GROSS_PROFIT_US value.

DEFINE FILE WF_RETAIL_LITE


FLOOR1/D7.2= FLOOR(GROSS_PROFIT_US);
END
TABLE FILE WF_RETAIL_LITE
PRINT GROSS_PROFIT_US/D9.2 FLOOR1
ON TABLE SET PAGE NOPAGE
END

Partial output follows. Note that even though the value returned is an integer, it is returned
with the same format as the FLOOR1 field (D7.2):

Gross Profit FLOOR1


------------ ------
165.00 165.00
13.99 13.00
60.99 60.00
225.98 225.00
79.99 79.00
44.59 44.00
94.30 94.00
238.50 238.00
199.99 199.00
68.99 68.00
63.58 63.00
129.99 129.00
37.49 37.00
75.99 75.00
13.99 13.00
119.00 119.00
-30.01 -31.00
54.99 54.00
189.98 189.00
44.59 44.00
91.98 91.00
89.00 89.00
59.50 59.00
129.99 129.00
54.00 54.00
109.98 109.00
98.99 98.00
98.99 98.00
99.99 99.00
44.59 44.00

LOG10: Calculating the Base 10 Logarithm


LOG10 returns the base-10 logarithm of a numeric expression.

538
18. Simplified Numeric Functions

Syntax: How to Calculate the Base 10 Logarithm

LOG10(num_exp)

where:
num_exp
Numeric

Is the numeric value for which to calculate the base 10 logarithm.

Example: Calculating the Base 10 Logarithm


The following request calculates the base 10 logarithm of current salaries.
TABLE FILE EMPLOYEE
PRINT CURR_SAL AND COMPUTE
LOG_CURR_SAL/D12.6 = LOG10(CURR_SAL);
BY LAST_NAME BY FIRST_NAME
WHERE DEPARTMENT EQ 'PRODUCTION';
ON TABLE SET PAGE NOLEAD
ON TABLE SET STYLE *
GRID=OFF,$
ENDSTYLE
END

The output is shown in the following image.

MOD: Calculating the Remainder From a Division


MOD calculates the remainder from a division. The output data type is the same as the input
data type.

Syntax: How to Calculate the Remainder From a Division

MOD(dividend, divisor)

Using Functions 539


MOD: Calculating the Remainder From a Division

where:
dividend
Numeric

Is the value to divide.

Note: The sign of the returned value will be the same as the sign of the dividend.
divisor
Numeric

Is the value to divide by.

If the divisor is zero (0), the dividend is returned.

Example: Calculating the Remainder From a Division


In the following request, MOD returns the remainder of PRICE_DOLLARS divided by
DAYSDELAYED:
DEFINE FILE WF_RETAIL_LITE
MOD1/D7.2= MOD(PRICE_DOLLARS, DAYSDELAYED);
END
TABLE FILE WF_RETAIL_LITE
PRINT PRICE_DOLLARS/D7.2 DAYSDELAYED/I5 MOD1
WHERE DAYSDELAYED GT 1
ON TABLE SET PAGE NOPAGE
ON TABLE PCHOLD FORMAT WP
END

Partial output follows:


Price Days
Dollars Delayed MOD1
------- ------- ----
399.00 3 .00
489.99 3 .99
786.50 2 .50
599.99 4 3.99
29.99 4 1.99
169.00 2 1.00
219.99 2 1.99
280.00 3 1.00
79.99 4 3.99
145.99 2 1.99
399.99 3 .99
349.99 3 1.99
169.00 3 1.00

540
18. Simplified Numeric Functions

POWER: Raising a Value to a Power


POWER raises a base value to a power.

Syntax: How to Raise a Value to a Power

POWER(base, power)

where:
base
Numeric

Is the value to raise to a power. The output value has the same data type as the base
value. If the base value is integer, negative power values will result in truncation.
power
Numeric

Is the power to which to raise the base value.

Example: Raising a Base Value to a Power


In the following request, POWER returns the value COGS_US/20.00 raised to the power stored
in DAYSDELAYED:
DEFINE FILE WF_RETAIL_LITE
BASE=COGS_US/20.00;
POWER1= POWER(COGS_US/20.00,DAYSDELAYED);
END
TABLE FILE WF_RETAIL_LITE
PRINT BASE IN 15 DAYSDELAYED POWER1
BY PRODUCT_CATEGORY
WHERE PRODUCT_CATEGORY EQ 'Computers'
WHERE DAYSDELAYED NE 0
ON TABLE SET PAGE NOPAGE
END

Using Functions 541


ROUND: Rounding a Number to a Given Number of Decimal Places

Partial output follows:


Product Days
Category BASE Delayed POWER1
-------- ---- ------- ------
Computers 12.15 3 1,793.61
16.70 2 278.89
8.35 1 8.35
8.10 2 65.61
4.05 1 4.05
4.05 2 16.40
4.05 4 269.04
8.35 1 8.35
16.70 1 16.70
8.35 3 582.18
8.35 1 8.35
4.05 1 4.05
4.05 1 4.05
8.35 4 4,861.23
8.35 -1 .12
8.35 1 8.35
8.35 3 582.18

ROUND: Rounding a Number to a Given Number of Decimal Places


Given a numeric expression and an integer count, ROUND returns the numeric expression
rounded to that number of decimal places. If the number of decimal places is negative, it
rounds to the left of the decimal point.

Syntax: How to Round a Number to a Given Number of Decimal Places

ROUND(num_exp, count)

where:
num_exp
Numeric

Is the numeric expression to be rounded.


count
Numeric

Is the number of decimal places to which the numeric expression is to be rounded. If the
number of decimal places is negative, ROUND rounds to the left of the decimal point.

542
18. Simplified Numeric Functions

Example: Rounding a Number to a Given Number of Decimal Places


The following request rounds the LISTPR field to zero decimal places and the NEWLISTPR field
1 decimal place and to -2 decimal places.
TABLE FILE MOVIES
PRINT LISTPR
AND COMPUTE
NEWLISTPR/D12.3 = LISTPR * 99;
ROUND_ZERO/D12.3 = ROUND(LISTPR, 0);
ROUND_PLUS1/D12.3 = ROUND(NEWLISTPR, 1);
ROUND_MINUS1/D12.3 = ROUND(NEWLISTPR, -2);
BY MOVIECODE
WHERE RECORDLIMIT EQ 3
ON TABLE SET PAGE NOLEAD
ON TABLE SET STYLE *
GRID=OFF,$
ENDSTYLE
END

The output is shown in the following image.

SIGN: Returning the Sign of a Number


SIGN takes a numeric argument and returns the value -1 if the number is negative, 0 (zero) if
the number is zero, and 1 if the number is positive.

Syntax: How to Return the Sign of a Number

SIGN(number)

where:
number
Is a field containing a numeric value or a number.

Using Functions 543


TRUNCATE: Truncating a Number to a Given Number of Decimal Places

Example: Returning the Sign of a Number


The following request returns the sign of positive numbers, negative numbers, and zero (0).
TABLE FILE GGSALES
SUM DOLLARS NOPRINT AND COMPUTE
PLUSDOLL/I9 = IF DOLLARS GT 12000000 THEN DOLLARS ELSE 0;
SIGN1/I5 = SIGN(PLUSDOLL);
NEGDOLL/I9 = IF DOLLARS LT 12000000 THEN 0 ELSE -DOLLARS;
SIGN2/I5 = SIGN(NEGDOLL);
BY CATEGORY
ON TABLE SET PAGE NOLEAD
ON TABLE SET STYLE *
GRID=OFF,$
ENDSTYLE
END

The output is shown in the following image.

TRUNCATE: Truncating a Number to a Given Number of Decimal Places


Given a numeric expression and an integer count, TRUNCATE returns the numeric expression
truncated to that number of decimal places. If the number of decimal places is negative, it
truncates to the left of the decimal point.

Syntax: How to Truncate a Number to a Given Number of Decimal Places

TRUNCATE(num_exp, count)

where:
num_exp
Numeric

Is the numeric expression to be truncated.


count
Numeric

Is the number of decimal places to which the numeric expression is to be truncated. If the
number of decimal places is negative, TRUNCATE truncates to the left of the decimal point.

544
18. Simplified Numeric Functions

Example: Truncating a Number to a Given Number of Decimal Places


The following request truncates the LISTPR field to 1 decimal place and to -1 decimal places.
TABLE FILE MOVIES
PRINT LISTPR
AND COMPUTE
TRUNCATE_PLUS1/D12.3 = TRUNCATE(LISTPR, 1);
TRUNCATE_MINUS1/D12.3 = TRUNCATE(LISTPR, -1);
BY MOVIECODE
WHERE RECORDLIMIT EQ 3
ON TABLE SET PAGE NOLEAD
ON TABLE SET STYLE *
GRID=OFF,$
ENDSTYLE
END

The output is shown in the following image.

Using Functions 545


TRUNCATE: Truncating a Number to a Given Number of Decimal Places

546
Chapter 19 Numeric Functions
Numeric functions perform calculations on numeric constants and fields.

For many functions, the output argument can be supplied either as a field name or as a
format enclosed in single quotation marks. However, if a function is called from a
Dialogue Manager command, this argument must always be supplied as a format, and if
a function is called from a Maintain Data procedure, this argument must always be
supplied as a field name. For detailed information about calling a function and supplying
arguments, see Accessing and Calling a Function on page 59.

Note: With CDN ON, numeric arguments must be delimited by a comma followed by a
space.
In this chapter:

ABS: Calculating Absolute Value FMLLIST: Returning an FML Tag List

ASIS: Distinguishing Between a Blank INT: Finding the Greatest Integer


and a Zero
LOG: Calculating the Natural Logarithm
BAR: Producing a Bar Chart
MAX and MIN: Finding the Maximum or
CHKPCK: Validating a Packed Field Minimum Value

DMOD, FMOD, and IMOD: Calculating MIRR: Calculating the Modified Internal
the Remainder From a Division Return Rate

EXP: Raising e to the Nth Power NORMSDST and NORMSINV: Calculating


Normal Distributions
EXPN: Evaluating a Number in Scientific
Notation PRDNOR and PRDUNI: Generating
Reproducible Random Numbers
FMLCAP: Retrieving FML Hierarchy
Captions RDNORM and RDUNIF: Generating
Random Numbers
FMLFOR: Retrieving FML Tag Values
SQRT: Calculating the Square Root
FMLINFO: Returning FOR Values
XIRR: Calculating the Modified Internal
Return Rate (Periodic or Non-Periodic)

Using Functions 547


ABS: Calculating Absolute Value

ABS: Calculating Absolute Value


Available Languages: reporting, Maintain

The ABS function returns the absolute value of a number.

Syntax: How to Calculate Absolute Value

ABS(in_value)

where:
in_value
Numeric

Is the value for which the absolute value is returned, the name of a field that contains the
value, or an expression that returns the value. If you use an expression, use parentheses
as needed to ensure the correct order of evaluation.

Example: Calculating Absolute Value


The COMPUTE command creates the DIFF field, then ABS calculates the absolute value of
DIFF:

TABLE FILE SALES


PRINT UNIT_SOLD AND DELIVER_AMT AND
COMPUTE DIFF/I5 = DELIVER_AMT - UNIT_SOLD; AND
COMPUTE ABS_DIFF/I5 = ABS(DIFF);BY PROD_CODE
WHERE DATE LE '1017';
END

The output is:


PROD_CODE UNIT_SOLD DELIVER_AMT DIFF ABS_DIFF
--------- --------- ----------- ---- --------
B10 30 30 0 0
B17 20 40 20 20
B20 15 30 15 15
C17 12 10 -2 2
D12 20 30 10 10
E1 30 25 -5 5
E3 35 25 -10 10

ASIS: Distinguishing Between a Blank and a Zero


Available Languages: reporting

The ASIS function distinguishes between a blank and a zero in Dialogue Manager. It
differentiates between a numeric string constant or variable defined as a numeric string, and a
field defined simply as numeric.

548
19. Numeric Functions

For details on ASIS, see ASIS: Distinguishing Between Space and Zero on page 197.

BAR: Producing a Bar Chart


Available Languages: reporting, Maintain

The BAR function produces a horizontal bar chart using repeating characters to form each bar.
Optionally, you can create a scale to clarify the meaning of a bar chart by replacing the title of
the column containing the bar with a scale.

Syntax: How to Produce a Bar Chart

BAR(barlength, infield, maxvalue, 'char', output)

where:
barlength
Numeric

Is the maximum length of the bar, in characters. If this value is less than or equal to 0, the
function does not return a bar.
infield
Numeric

Is the data field plotted as a bar chart.


maxvalue
Numeric

Is the maximum value of a bar. This value must be greater than the maximum value stored
in infield. If infield is larger than maxvalue, the function uses maxvalue and returns a bar of
maximum length.
'char'
Alphanumeric

Is the repeating character that creates the bars enclosed in single quotation marks. If you
specify more than one character, only the first character is used.
output
Alphanumeric

Is the name of the field that contains the result, or the format of the output value enclosed
in single quotation marks. The output field must be large enough to contain a bar of
maximum length as defined by barlength.

Using Functions 549


BAR: Producing a Bar Chart

Example: Producing a Bar Chart


BAR creates a bar chart for the CURR_SAL field, and stores the output in SAL_BAR. The bar
created can be no longer than 30 characters long, and the value it represents can be no
greater than 30,000.

TABLE FILE EMPLOYEE


PRINT CURR_SAL AND COMPUTE
SAL_BAR/A30 = BAR(30, CURR_SAL, 30000, '=', SAL_BAR);BY LAST_NAME BY
FIRST_NAME
WHERE DEPARTMENT EQ 'PRODUCTION';
END

The output is:


LAST_NAME FIRST_NAME CURR_SAL SAL_BAR
--------- ---------- -------- -------
BANNING JOHN $29,700.00 ===========================
IRVING JOAN $26,862.00 ==========================
MCKNIGHT ROGER $16,100.00 ================
ROMANS ANTHONY $21,120.00 =====================
SMITH RICHARD $9,500.00 ==========
STEVENS ALFRED $11,000.00 ===========

Example: Creating a Bar Chart With a Scale


BAR creates a bar chart for the CURR_SAL field. The request then replaces the field name
SAL_BAR with a scale using the AS phrase.

To run this request on a platform for which the default font is proportional, use a non-
proportional font or issue SET STYLE=OFF.

SET STYLE=OFF

TABLE FILE EMPLOYEE


HEADING
"CURRENT SALARIES OF EMPLOYEES IN PRODUCTION DEPARTMENT"
"GRAPHED IN THOUSANDS OF DOLLARS"
" "
PRINT CURR_SAL AS 'CURRENT SALARY'
AND COMPUTE
SAL_BAR/A30 = BAR(30, CURR_SAL, 30000, '=', SAL_BAR);
AS ' 5 10 15 20 25 30,----+----+----+----+----+----+'
BY LAST_NAME AS 'LAST NAME'
BY FIRST_NAME AS 'FIRST NAME'
WHERE DEPARTMENT EQ 'PRODUCTION';
ON TABLE SET PAGE-NUM OFF
ON TABLE SET STYLE * GRID=OFF, $
END

550
19. Numeric Functions

The output is:


CURRENT SALARIES OF EMPLOYEES IN PRODUCTION DEPARTMENT
GRAPHED IN THOUSANDS OF DOLLARS
5 10 15 20 25 30
LAST NAME FIRST NAME CURRENT SALARY ----+----+----+----+----+----+
--------- ---------- -------------- ------------------------------
BANNING JOHN $29,700.00 =============================
IRVING JOAN $26,862.00 ===========================
MCKNIGHT ROGER $16,100.00 ================
ROMANS ANTHONY $21,120.00 =====================
SMITH RICHARD $9,500.00 ==========
STEVENS ALFRED $11,000.00 ===========

CHKPCK: Validating a Packed Field


Available Languages: reporting, Maintain

The CHKPCK function validates the data in a field described as packed format (if available on
your platform). The function prevents a data exception from occurring when a request reads a
field that is expected to contain a valid packed number but does not.

To use CHKPCK:
1. Ensure that the Master File (USAGE and ACTUAL attributes) defines the field as
alphanumeric, not packed. This does not change the field data, which remains packed, but
it enables the request to read the data without a data exception.
2. Call CHKPCK to examine the field. The function returns the output to a field defined as
packed. If the value it examines is a valid packed number, the function returns the value; if
the value is not packed, the function returns an error code.

Syntax: How to Validate a Packed Field

CHKPCK(length, in_value, error, output)

where:
length
Numeric

Is the length of the packed field. It can be between 1 and 16 bytes.


infield
Alphanumeric

Is the name of the packed field or the value to be verified as packed decimal. The value
must be described as alphanumeric, not packed.

Using Functions 551


CHKPCK: Validating a Packed Field

error
Numeric

Is the error code that the function returns if a value is not packed. Choose an error code
outside the range of data. The error code is first truncated to an integer, then converted to
packed format. However, it may appear on a report with a decimal point depending on the
output format.
output
Packed-decimal

Is the name of the field that contains the result, or the format of the output value enclosed
in single quotation marks.

Example: Validating Packed Data

1. Prepare a data source that includes invalid packed data. The following example creates
TESTPACK, which contains the PACK_SAL field. PACK_SAL is defined as alphanumeric but
actually contains packed data. The invalid packed data is stored as AAA.
DEFINE FILE EMPLOYEE
PACK_SAL/A8 = IF EMP_ID CONTAINS '123'
THEN 'AAA' ELSE PCKOUT(CURR_SAL, 8, 'A8');
END

TABLE FILE EMPLOYEE


PRINT DEPARTMENT PACK_SAL BY EMP_ID
ON TABLE SAVE AS TESTPACK
END

The output is:


NUMBER OF RECORDS IN TABLE= 12 LINES= 12
ALPHANUMERIC RECORD NAMED TESTPACK
FIELDNAME ALIAS FORMAT LENGTH
EMP_ID EID A9 9
DEPARTMENT DPT A10 10
PACK_SAL A8 8
TOTAL 27
2. Create a Master File for the TESTPACK data source. Define the PACK_SAL field as
alphanumeric in the USAGE and ACTUAL attributes.
FILE = TESTPACK, SUFFIX = FIX
FIELD = EMP_ID ,ALIAS = EID,USAGE = A9 ,ACTUAL = A9 ,$
FIELD = DEPARTMENT,ALIAS = DPT,USAGE = A10,ACTUAL = A10,$
FIELD = PACK_SAL ,ALIAS = PS ,USAGE = A8 ,ACTUAL = A8 ,$

3. Create a request that uses CHKPCK to validate the values in the PACK_SAL field, and store
the result in the GOOD_PACK field. Values not in packed format return the error code -999.
Values in packed format appear accurately.

552
19. Numeric Functions

DEFINE FILE TESTPACK


GOOD_PACK/P8CM = CHKPCK(8, PACK_SAL, -999, GOOD_PACK);
END

TABLE FILE TESTPACK


PRINT DEPARTMENT GOOD_PACK BY EMP_ID
END

The output is:


EMP_ID DEPARTMENT GOOD_PACK
------ ---------- ---------
071382660 PRODUCTION $11,000
112847612 MIS $13,200
117593129 MIS $18,480
119265415 PRODUCTION $9,500
119329144 PRODUCTION $29,700
123764317 PRODUCTION -$999
126724188 PRODUCTION $21,120
219984371 MIS $18,480
326179357 MIS $21,780
451123478 PRODUCTION -$999
543729165 MIS $9,000
818692173 MIS $27,062

DMOD, FMOD, and IMOD: Calculating the Remainder From a Division


Available Languages: reporting, Maintain

The MOD functions calculate the remainder from a division. Each function returns the
remainder in a different format.

The functions use the following formula.


remainder = dividend - INT(dividend/divisor) * divisor

DMOD returns the remainder as a decimal number.

FMOD returns the remainder as a floating-point number.

IMOD returns the remainder as an integer.

Using Functions 553


DMOD, FMOD, and IMOD: Calculating the Remainder From a Division

Syntax: How to Calculate the Remainder From a Division

function(dividend, divisor, output)

where:
function
Is one of the following:

DMOD returns the remainder as a decimal number.

FMOD returns the remainder as a floating-point number.

IMOD returns the remainder as an integer.

dividend
Numeric

Is the number being divided.


divisor
Numeric

Is the number dividing the dividend.


output
Numeric

Is the result whose format is determined by the function used. Can be the name of the
field that contains the result, or the format of the output value enclosed in single quotation
marks.

If the divisor is zero (0), the dividend is returned.

Example: Calculating the Remainder From a Division


IMOD divides ACCTNUMBER by 1000 and returns the remainder to LAST3_ACCT:
TABLE FILE EMPLOYEE
PRINT ACCTNUMBER AND COMPUTE
LAST3_ACCT/I3L = IMOD(ACCTNUMBER, 1000, LAST3_ACCT);
BY LAST_NAME BY FIRST_NAME
WHERE (ACCTNUMBER NE 000000000) AND (DEPARTMENT EQ 'MIS');
END

554
19. Numeric Functions

The output is:


LAST_NAME FIRST_NAME ACCTNUMBER LAST3_ACCT
--------- ---------- ---------- ----------
BLACKWOOD ROSEMARIE 122850108 108
CROSS BARBARA 163800144 144
GREENSPAN MARY 150150302 302
JONES DIANE 040950036 036
MCCOY JOHN 109200096 096
SMITH MARY 027300024 024

EXP: Raising e to the Nth Power


Available Languages: reporting, Maintain

The EXP function raises the value "e" (approximately 2.72) to a specified power. This function
is the inverse of the LOG function, which returns the logarithm of the argument.

EXP calculates the result by adding terms of an infinite series. If a term adds less than .
000001 percent to the sum, the function ends the calculation and returns the result as a
double-precision number.

Syntax: How to Raise e to the Nth Power

EXP(power, output)

where:
power
Numeric

Is the power to which "e" is raised.


output
Double-precision floating-point

Is the name of the field that contains the result, or the format of the output value enclosed
in single quotation marks.

Using Functions 555


EXPN: Evaluating a Number in Scientific Notation

Example: Raising e to the Nth Power


EXP raises "e" to the power designated by the &POW variable, specified here as 3. The result
is then rounded to the nearest integer with the .5 rounding constant and returned to the
variable &RESULT. The format of the output value is D15.3.

-SET &POW = '3';


-SET &RESULT = EXP(&POW, 'D15.3') + 0.5;-HTMLFORM BEGIN
<HTML>
<BODY>
E TO THE &POW POWER IS APPROXIMATELY &RESULT
</BODY>
</HTML>
-HTMLFORM END

The output is:


E TO THE 3 POWER IS APPROXIMATELY 20

EXPN: Evaluating a Number in Scientific Notation


The EXPN function evaluates a numeric literal or Dialogue Manager variable expressed in
scientific notation.

Syntax: How to Evaluate a Number in Scientific Notation

EXPN(n.nn {E|D} {+|-} p)

where:
n.nn
Numeric

Is a numeric literal that consists of a whole number component, followed by a decimal


point, followed by a fractional component.
E, D

Denotes scientific notation. E and D are interchangeable.


+, -

Indicates if p is positive or negative.


p
Integer

Is the power of 10 to which to raise n.nn.

556
19. Numeric Functions

Note: EXPN does not use an output argument. The format of the result is floating-point double
precision.

Example: Evaluating a Number in Scientific Notation


EXPN evaluates 1.03E+2.
EXPN(1.03E+2)

The result is 103.

FMLCAP: Retrieving FML Hierarchy Captions


Available Languages: reporting

The FMLCAP function returns the caption value for each row in an FML hierarchy request. In
order to retrieve caption values, the Master File must define an FML hierarchy and the request
must use the GET CHILDREN, ADD, or WITH CHILDREN option to retrieve hierarchy data. If the
FOR field in the request does not have a caption field defined, FMLCAP returns a blank string.

FMLCAP is supported for COMPUTE but is not recommended for use with DEFINE.

Syntax: How to Retrieve Captions in an FML Request Using the FMLCAP Function

FMLCAP(fieldname|'format')

where:
fieldname
Is the name of the caption field.
'format'

Is the format of the caption field enclosed in single quotation marks.

Example: Retrieving FML Hierarchy Captions Using FMLCAP


The following request retrieves and aggregates the FML hierarchy that starts with the parent
value 2000. FMLCAP retrieves the captions, while the actual account numbers appear as the
FOR values.
SET FORMULTIPLE = ON
TABLE FILE CENTSTMT
SUM ACTUAL_AMT
COMPUTE CAP1/A30= FMLCAP(GL_ACCOUNT_CAPTION);
FOR GL_ACCOUNT
2000 WITH CHILDREN 2 ADD
END

Using Functions 557


FMLFOR: Retrieving FML Tag Values

The output is:


Actual CAP1
------ ----
2000 313,611,852. Gross Margin
2100 187,087,470. Sales Revenue
2200 98,710,368. Retail Sales
2300 13,798,832. Mail Order Sales
2400 12,215,780. Internet Sales
2500 100,885,159. Cost Of Goods Sold
2600 54,877,250. Variable Material Costs
2700 6,176,900. Direct Labor
2800 3,107,742. Fixed Costs

FMLFOR: Retrieving FML Tag Values


Available Languages: reporting

FMLFOR retrieves the tag value associated with each row in an FML request. If the FML row
was generated as a sum of data records using the OR phrase, FMLFOR returns the first value
specified in the list. If the OR phrase was generated by an FML Hierarchy ADD command,
FMLFOR returns the tag value associated with the parent specified in the ADD command.

The FMLFOR function is supported for COMPUTE but not for DEFINE. Attempts to use it in a
DEFINE result in blank values.

Syntax: How to Retrieve FML Tag Values

FMLFOR(output)

where:
output
Is name of the field that will contain the result, or the format of the output value enclosed
in single quotation marks.

Example: Retrieving FML Tag Values With FMLFOR


SET FORMULTIPLE = ON
TABLE FILE LEDGER
SUM AMOUNT
COMPUTE RETURNEDFOR/A8 = FMLFOR('A8');
FOR ACCOUNT
1010 OVER
1020 OVER
1030 OVER
BAR OVER
1030 OR 1020 OR 1010
END

558
19. Numeric Functions

The output is:


AMOUNT RETURNEDFOR
------ -----------
1010 8,784 1010
1020 4,494 1020
1030 7,961 1030
------ --------
1010 21,239 1030

FMLINFO: Returning FOR Values


Available Languages: reporting

The FMLINFO function returns the FOR value associated with each row in an FML report. With
FMLINFO, you can use the appropriate FOR value in a COMPUTE command to do drill-downs
and sign changes for each row in the report, even when the row is a summary row created
using an OR list or a Financial Modeling Language (FML) Hierarchy ADD command.

Note: You can use the SET parameter FORMULTIPLE=ON to enable an incoming record to be
used on more than one line in an FML report.

Syntax: How to Retain FOR Values in an FML Request

FMLINFO('FORVALUE', output)

where:
'FORVALUE'
Alphanumeric

Returns the FOR value associated with each row in an FML report. If the FML row was
generated as a sum of data records using the OR phrase, FMLINFO returns the first FOR
value specified in the list of values. If the OR phrase was generated by an FML Hierarchy
ADD command, FMLINFO returns the FOR value associated with the parent specified in the
ADD command.
output
Alphanumeric

Is the name of the field that contains the result, or the format of the output value enclosed
in single quotation marks.

Using Functions 559


FMLINFO: Returning FOR Values

Example: Retrieving FOR Values for FML Hierarchy Rows


The following request creates a field called PRINT_AMT that is the negative of the
NAT_AMOUNT field for account numbers less than 2500 in the CENTSYSF data source. The
CENTGL data source contains the hierarchy information for CENTSYSF. Therefore, CENTGL is
joined to CENTSYSF for the request:
SET FORMULTIPLE = ON
JOIN SYS_ACCOUNT IN CENTGL TO ALL SYS_ACCOUNT IN CENTSYSF
TABLE FILE CENTGL
SUM NAT_AMOUNT/D10 IN 30
COMPUTE PRINT_AMT/D10 = IF FMLINFO('FORVALUE','A7') LT '2500'
THEN 0-NAT_AMOUNT ELSE NAT_AMOUNT;
COMPUTE FORV/A4 = FMLINFO('FORVALUE', 'A4');
COMPUTE ACTION/A9 = IF FORV LT '2500'
THEN 'CHANGED' ELSE 'UNCHANGED';
FOR GL_ACCOUNT
2000 WITH CHILDREN 2 ADD AS CAPTION
END

Note: The parent value specified in the WITH CHILDREN ADD command (2000) is returned for
the first row on the report. Each subsequent row is also a consolidated subsection of the
hierarchy with a parent value that is returned by FMLINFO:
Month
Actual PRINT_AMT FORV ACTION
------ --------- ---- ------
Gross Margin -25,639,223 25,639,223 2000 CHANGED
Sales Revenue -62,362,490 62,362,490 2100 CHANGED
Retail Sales -49,355,184 49,355,184 2200 CHANGED
Mail Order Sales -6,899,416 6,899,416 2300 CHANGED
Internet Sales -6,107,890 6,107,890 2400 CHANGED
Cost Of Goods Sold 36,723,267 36,723,267 2500 UNCHANGED
Variable Material Costs 27,438,625 27,438,625 2600 UNCHANGED
Direct Labor 6,176,900 6,176,900 2700 UNCHANGED
Fixed Costs 3,107,742 3,107,742 2800 UNCHANGED

Example: Using FMLINFO With an OR Phrase


The FOR value printed for the summary line is 1010, but FMLINFO returns the first value
specified in the OR list, 1030:
SET FORMULTIPLE = ON
TABLE FILE LEDGER
SUM AMOUNT
COMPUTE RETURNEDFOR/A8 = FMLINFO('FORVALUE','A8');
FOR ACCOUNT
1010 OVER
1020 OVER
1030 OVER
BAR OVER
1030 OR 1020 OR 1010
END

560
19. Numeric Functions

The output is:

AMOUNT RETURNEDFOR
1010 8,784 1010
1020 4,494 1020
1030 7,961 1030
------ ---------
1010 21,239 1030

FMLLIST: Returning an FML Tag List


Available Languages: reporting

FMLLIST returns a string containing the complete tag list for each row in an FML request. If a
row has a single tag value, that value is returned.

The FMLLIST function is supported for COMPUTE but not for DEFINE. Attempts to use it in a
DEFINE result in blank values.

Syntax: How to Retrieve an FML Tag List

FMLLIST('A4096V')

where:
'A4096V'

Is the required argument.

Example: Retrieving an FML Tag List With FMLLIST


SET FORMULTIPLE=ON
TABLE FILE LEDGER
HEADING
"TEST OF FMLLIST"
" "
SUM AMOUNT
COMPUTE LIST1/A36 = FMLLIST('A4096V');
FOR ACCOUNT
'1010' OVER
'1020' OVER
'1030' OVER
BAR OVER
'1030' OR '1020' OR '1010'
END

Using Functions 561


INT: Finding the Greatest Integer

The output is:


TEST OF FMLLIST
AMOUNT LIST1
------ -----
1010 8,784 1010
1020 4,494 1020
1030 7,961 1030
------ ------------------------------------
1010 21,239 1010 OR 1020 OR 1030

INT: Finding the Greatest Integer


Available Languages: reporting, Maintain

The INT function returns the integer component of a number.

Syntax: How to Find the Greatest Integer

INT(in_value)

where:
in_value
Numeric

Is the value for which the integer component is returned, the name of a field that contains
the value, or an expression that returns the value. If you supply an expression, use
parentheses as needed to ensure the correct order of evaluation.

Example: Finding the Greatest Integer


INT finds the greatest integer in the DED_AMT field and stores it in INT_DED_AMT:
TABLE FILE EMPLOYEE
SUM DED_AMT AND COMPUTE
INT_DED_AMT/I9 = INT(DED_AMT);BY LAST_NAME BY FIRST_NAME
WHERE (DEPARTMENT EQ 'MIS') AND (PAY_DATE EQ 820730);
END

The output is:


LAST_NAME FIRST_NAME DED_AMT INT_DED_AMT
--------- ---------- ------- -----------
BLACKWOOD ROSEMARIE $1,261.40 1261
CROSS BARBARA $1,668.69 1668
GREENSPAN MARY $127.50 127
JONES DIANE $725.34 725
SMITH MARY $334.10 334

LOG: Calculating the Natural Logarithm


Available Languages: reporting, Maintain

562
19. Numeric Functions

The LOG function returns the natural logarithm of a number.

Syntax: How to Calculate the Natural Logarithm

LOG(in_value)

where:
in_value
Numeric

Is the value for which the natural logarithm is calculated, the name of a field that contains
the value, or an expression that returns the value. If you supply an expression, use
parentheses as needed to ensure the correct order of evaluation. If in_value is less than or
equal to 0, LOG returns 0.

Example: Calculating the Natural Logarithm


LOG calculates the logarithm of the CURR_SAL field:
TABLE FILE EMPLOYEE
PRINT CURR_SAL AND COMPUTE
LOG_CURR_SAL/D12.2 = LOG(CURR_SAL);BY LAST_NAME BY FIRST_NAME
WHERE DEPARTMENT EQ 'PRODUCTION';
END

The output is:


LAST_NAME FIRST_NAME CURR_SAL LOG_CURR_SAL
--------- ---------- -------- ------------
BANNING JOHN $29,700.00 10.30
IRVING JOAN $26,862.00 10.20
MCKNIGHT ROGER $16,100.00 9.69
ROMANS ANTHONY $21,120.00 9.96
SMITH RICHARD $9,500.00 9.16
STEVENS ALFRED $11,000.00 9.31

MAX and MIN: Finding the Maximum or Minimum Value


Available Languages: reporting, Maintain

The MAX and MIN functions return the maximum or minimum value, respectively, from a list of
values.

Using Functions 563


MIRR: Calculating the Modified Internal Return Rate

Syntax: How to Find the Maximum or Minimum Value

{MAX|MIN}(value1, value2, ...)

where:
MAX

Returns the maximum value.


MIN

Returns the minimum value.


value1, value2
Numeric

Are the values for which the maximum or minimum value is returned, the name of a field
that contains the values, or an expression that returns the values. If you supply an
expression, use parentheses as needed to ensure the correct order of evaluation.

Example: Determining the Minimum Value


MIN returns either the value of the ED_HRS field or the constant 30, whichever is lower:
TABLE FILE EMPLOYEE
PRINT ED_HRS AND COMPUTE
MIN_EDHRS_30/D12.2 = MIN(ED_HRS, 30);BY LAST_NAME BY FIRST_NAME
WHERE DEPARTMENT EQ 'MIS';
END

The output is:


LAST_NAME FIRST_NAME ED_HRS MIN_EDHRS_30
--------- ---------- ------ ------------
BLACKWOOD ROSEMARIE 75.00 30.00
CROSS BARBARA 45.00 30.00
GREENSPAN MARY 25.00 25.00
JONES DIANE 50.00 30.00
MCCOY JOHN .00 .00
SMITH MARY 36.00 30.00

MIRR: Calculating the Modified Internal Return Rate


Available languages: reporting

The MIRR function calculates the modified internal rate of return for a series of periodic cash
flows.

564
19. Numeric Functions

Syntax: How to Calculate the Modified Internal Rate of Return

TABLE FILE ...


{PRINT|SUM} field ...COMPUTE rrate/fmt = MIRR(cashflow, finrate,
reinvrate, output);
WITHIN {sort_field|TABLE}

where:
field ...
Are fields that appear in the report output.
rrate
Is the field that contains the calculated return rate.
fmt
Is the format of the return rate. The data type must be D.
cashflow
Is a numeric field. Each value represents either a payment (negative value) or income
(positive value) for one period. The values must be in the correct sequence in order for the
sequence of cash flows to be calculated correctly. The dates corresponding to each cash
flow should be equally spaced and sorted in chronological order. The calculation requires
at least one negative value and one positive value in the cashflow field. If the values are all
positive or all negative, a zero result is returned.
finrate
Is a finance rate for negative cash flows. This value must be expressed as a non-negative
decimal fraction between 0 and 1. It must be constant within each sort group for which a
return rate is calculated, but it can change between sort groups.
reinvrate
Is the reinvestment rate for positive cash flows. This value must be expressed as a non-
negative decimal fraction between 0 and 1. It must be constant within each sort group but
can change between sort groups. It must be constant within each sort group for which a
return rate is calculated, but it can change between sort groups.
output
Is the name of the field that contains the return rate, or its format enclosed in single
quotation marks.

Using Functions 565


MIRR: Calculating the Modified Internal Return Rate

sort_field
Is a field that sorts the report output and groups it into subsets of rows on which the
function can be calculated separately. To calculate the function using every row of the
report output, use the WITHIN TABLE phrase. A WITHIN phrase is required.

Reference: Usage Notes for the MIRR Function


This function is only supported in a COMPUTE command with the WITHIN phrase.

The cash flow field must contain at least one negative value and one positive value.

Dates must be equally spaced.

Missing cash flows or dates are not supported.

Example: Calculating the Modified Internal Rate of Return


The following request calculates modified internal return rates for categories of products. It
assumes a finance charge of ten percent and a reinvestment rate of ten percent. The request
is sorted by date so that the correct cash flows are calculated. The rate returned by the
function is multiplied by 100 in order to express it as a percent rather than a decimal value.
Note that the format includes the % character. This causes a percent symbol to display, but it
does not calculate a percent.

In order to create one cash flow value per date, the values are summed. NEWDOLL is defined
in order to create negative values in each category as required by the function:
DEFINE FILE GGSALES
SDATE/YYM = DATE;
SYEAR/Y = SDATE;
NEWDOLL/D12.2 = IF DATE LT '19970401' THEN -1 * DOLLARS ELSE DOLLARS;
END
TABLE FILE GGSALES
SUM NEWDOLL
COMPUTE RRATE/D7.2% = MIRR(NEWDOLL, .1, .1, RRATE) * 100;
WITHIN CATEGORY
BY CATEGORY
BY SDATE
WHERE SYEAR EQ 97
END

566
19. Numeric Functions

A separate rate is calculated for each category because of the WITHIN CATEGORY phrase. A
portion of the output is shown:
Category SDATE NEWDOLL RRATE
-------- ----- ------- -----
Coffee 1997/01 -801,123.00 15.11%
1997/02 -682,340.00 15.11%
1997/03 -765,078.00 15.11%
1997/04 691,274.00 15.11%
1997/05 720,444.00 15.11%
1997/06 742,457.00 15.11%
1997/07 747,253.00 15.11%
1997/08 655,896.00 15.11%
1997/09 730,317.00 15.11%
1997/10 724,412.00 15.11%
1997/11 620,264.00 15.11%
1997/12 762,328.00 15.11%
Food 1997/01 -672,727.00 16.24%
1997/02 -699,073.00 16.24%
1997/03 -642,802.00 16.24%
1997/04 718,514.00 16.24%
1997/05 660,740.00 16.24%
1997/06 734,705.00 16.24%
1997/07 760,586.00 16.24%
To calculate one modified internal return rate for all of the report data, use the WITHIN TABLE
phrase. In this case, the data does not have to be sorted by CATEGORY:
DEFINE FILE GGSALES
SDATE/YYM = DATE;
SYEAR/Y = SDATE;
NEWDOLL/D12.2 = IF DATE LT '19970401' THEN -1 * DOLLARS ELSE DOLLARS;
END

TABLE FILE GGSALES


SUM NEWDOLL
COMPUTE RRATE/D7.2% = MIRR(NEWDOLL, .1, .1, RRATE) * 100;
WITHIN TABLE
BY SDATE
WHERE SYEAR EQ 97
END

Using Functions 567


NORMSDST and NORMSINV: Calculating Normal Distributions

The output is:


SDATE NEWDOLL RRATE
----- ------- -----
1997/01 -1,864,129.00 15.92%
1997/02 -1,861,639.00 15.92%
1997/03 -1,874,439.00 15.92%
1997/04 1,829,838.00 15.92%
1997/05 1,899,494.00 15.92%
1997/06 1,932,630.00 15.92%
1997/07 2,005,402.00 15.92%
1997/08 1,838,863.00 15.92%
1997/09 1,893,944.00 15.92%
1997/10 1,933,705.00 15.92%
1997/11 1,865,982.00 15.92%
1997/12 2,053,923.00 15.92%

NORMSDST and NORMSINV: Calculating Normal Distributions


The NORMSDST and NORMSINV functions perform calculations on a standard normal
distribution curve. NORMSDST calculates the percentage of data values that are less than or
equal to a normalized value; NORMSINV is the inverse of NORMSDST, calculates the
normalized value that forms the upper boundary of a percentile in a standard normal
distribution curve.

NORMSDST: Calculating Standard Cumulative Normal Distribution


The NORMSDST function performs calculations on a standard normal distribution curve,
calculating the percentage of data values that are less than or equal to a normalized value. A
normalized value is a point on the X-axis of a standard normal distribution curve in standard
deviations from the mean. This is useful for determining percentiles in normally distributed
data.

The NORMSINV function is the inverse of NORMSDST. For information about NORMSINV, see
NORMSINV: Calculating Inverse Cumulative Normal Distribution on page 571.

The results of NORMSDST are returned as double-precision and are accurate to 6 significant
digits.

A standard normal distribution curve is a normal distribution that has a mean of 0 and a
standard deviation of 1. The total area under this curve is 1. A point on the X-axis of the
standard normal distribution is called a normalized value. Assuming that your data is normally
distributed, you can convert a data point to a normalized value to find the percentage of scores
that are less than or equal to the raw score.

You can convert a value (raw score) from your normally distributed data to the equivalent
normalized value (z-score) as follows:
z = (raw_score - mean)/standard_deviation

568
19. Numeric Functions

To convert from a z-score back to a raw score, use the following formula:
raw_score = z * standard_deviation + mean

The mean of data points xi, where i is from 1 to n is:

The standard deviation of data points xi, where i is from 1 to n is:

The following diagram illustrates the results of the NORMSDST and NORMSINV functions.

Reference: Characteristics of the Normal Distribution


Many common measurements are normally distributed. A plot of normally distributed data
values approximates a bell-shaped curve. The two measures required to describe any normal
distribution are the mean and the standard deviation:

The mean is the point at the center of the curve.

The standard deviation describes the spread of the curve. It is the distance from the mean
to the point of inflection (where the curve changes direction).

Using Functions 569


NORMSDST and NORMSINV: Calculating Normal Distributions

Syntax: How to Calculate the Cumulative Standard Normal Distribution Function

NORMSDST(value, 'D8');

where:
value
Is a normalized value.
D8

Is the required format for the result. The value returned by the function is double-precision.
You can assign it to a field with any valid numeric format.

Example: Using the NORMSDST Function


NORMSDST calculates the Z value and finds its percentile:
DEFINE FILE GGPRODS
-* CONVERT SIZE FIELD TO DOUBLE PRECISION
X/D12.5 = SIZE;
END
TABLE FILE GGPRODS
SUM X NOPRINT CNT.X NOPRINT
-* CALCULATE MEAN AND STANDARD DEVIATION
COMPUTE NUM/D12.5 = CNT.X; NOPRINT
COMPUTE MEAN/D12.5 = AVE.X; NOPRINT
COMPUTE VARIANCE/D12.5 = ((NUM*ASQ.X) - (X*X/NUM))/(NUM-1); NOPRINT
COMPUTE STDEV/D12.5 = SQRT(VARIANCE); NOPRINT
PRINT SIZE X NOPRINT
-* COMPUTE NORMALIZED VALUES AND USE AS INPUT TO NORMSDST FUNCTION
COMPUTE Z/D12.5 = (X - MEAN)/STDEV;
COMPUTE NORMSD/D12.5 = NORMSDST(Z, 'D8');
BY PRODUCT_ID NOPRINT
END

The output is:


Size Z NORMSD
---- - ------
16 -.07298 .47091
12 -.80273 .21106
12 -.80273 .21106
20 .65678 .74434
24 1.38654 .91721
20 .65678 .74434
24 1.38654 .91721
16 -.07298 .47091
12 -.80273 .21106
8 -1.53249 .06270

570
19. Numeric Functions

NORMSINV: Calculating Inverse Cumulative Normal Distribution


The NORMSINV function performs calculations on a standard normal distribution curve, finding
the normalized value that forms the upper boundary of a percentile in a standard normal
distribution curve. This is the inverse of NORMSDST. For information about NORMSDST, see
NORMSDST: Calculating Standard Cumulative Normal Distribution on page 568.

The results of NORMSINV are returned as double-precision and are accurate to 6 significant
digits.

Syntax: How to Calculate the Inverse Cumulative Standard Normal Distribution Function

NORMSINV(value, 'D8');

where:
value
Is a number between 0 and 1 (which represents a percentile in a standard normal
distribution).
D8

Is the required format for the result. The value returned by the function is double-precision.
You can assign it to a field with any valid numeric format.

Using Functions 571


PRDNOR and PRDUNI: Generating Reproducible Random Numbers

Example: Using the NORMSINV Function


NORMSDST finds the percentile for the Z field. NORMSINV then returns this percentile to a
normalized value:
DEFINE FILE GGPRODS
-* CONVERT SIZE FIELD TO DOUBLE PRECISION
X/D12.5 = SIZE;
END
TABLE FILE GGPRODS
SUM X NOPRINT CNT.X NOPRINT
-* CALCULATE MEAN AND STANDARD DEVIATION
COMPUTE NUM/D12.5 = CNT.X; NOPRINT
COMPUTE MEAN/D12.5 = AVE.X; NOPRINT
COMPUTE VARIANCE/D12.5 = ((NUM*ASQ.X) - (X*X/NUM))/(NUM-1); NOPRINT
COMPUTE STDEV/D12.5 = SQRT(VARIANCE); NOPRINT
PRINT SIZE X NOPRINT
-* COMPUTE NORMALIZED VALUES AND USE AS INPUT TO NORMSDST FUNCTION
-* THEN USE RETURNED VALUES AS INPUT TO NORMSINV FUNCTION
-* AND CONVERT BACK TO DATA VALUES
COMPUTE Z/D12.5 = (X - MEAN)/STDEV;
COMPUTE NORMSD/D12.5 = NORMSDST(Z, 'D8');
COMPUTE NORMSI/D12.5 = NORMSINV(NORMSD, 'D8');
COMPUTE DSIZE/D12 = NORMSI * STDEV + MEAN;
BY PRODUCT_ID NOPRINT
END

The output shows that NORMSINV is the inverse of NORMSDST and returns the original values:
Size Z NORMSD NORMSI DSIZE
---- - ------ ------ -----
16 -.07298 .47091 -.07298 16
12 -.80273 .21106 -.80273 12
12 -.80273 .21106 -.80273 12
20 .65678 .74434 .65678 20
24 1.38654 .91721 1.38654 24
20 .65678 .74434 .65678 20
24 1.38654 .91721 1.38654 24
16 -.07298 .47091 -.07298 16
12 -.80273 .21106 -.80273 12
8 -1.53249 .06270 -1.53249 8

PRDNOR and PRDUNI: Generating Reproducible Random Numbers


Available Languages: reporting, Maintain

The PRDNOR and PRDUNI functions generate reproducible random numbers:

PRDNOR generates reproducible double-precision random numbers normally distributed


with an arithmetic mean of 0 and a standard deviation of 1.

572
19. Numeric Functions

If PRDNOR generates a large set of numbers, they have the following properties:

The numbers lie roughly on a bell curve, as shown in the following figure. The bell curve
is highest at the 0 mark, meaning that there are more numbers closer to 0 than farther
away.

The average of the numbers is close to 0.

The numbers can be any size, but most are between 3 and -3.

PRDUNI generates reproducible double-precision random numbers uniformly distributed


between 0 and 1 (that is, any random number it generates has an equal probability of being
anywhere between 0 and 1).

Syntax: How to Generate Reproducible Random Numbers

{PRDNOR|PRDUNI}(seed, output)

where:
PRDNOR

Generates reproducible double-precision random numbers normally distributed with an


arithmetic mean of 0 and a standard deviation of 1.
PRDUNI

Generates reproducible double-precision random numbers uniformly distributed between 0


and 1.

Using Functions 573


PRDNOR and PRDUNI: Generating Reproducible Random Numbers

seed
Numeric

Is the seed or the field that contains the seed, up to 9 digits. The seed is truncated to an
integer.

On z/OS, the numbers do not reproduce.


output
Double-precision

Is the name of the field that contains the result, or the format of the output value enclosed
in single quotation marks.

Example: Generating Reproducible Random Numbers


PRDNOR assigns random numbers and stores them in RAND. These values are then used to
randomly pick five employee records identified by the values in the LAST NAME and FIRST
NAME fields. The seed is 40. To produce a different set of numbers, change the seed.
DEFINE FILE EMPLOYEE
RAND/D12.2 WITH LAST_NAME = PRDNOR(40, RAND);END

TABLE FILE EMPLOYEE


PRINT LAST_NAME AND FIRST_NAME
BY HIGHEST 5 RAND
END

The output is:


RAND LAST_NAME FIRST_NAME
---- --------- ----------
1.38 STEVENS ALFRED
1.12 MCCOY JOHN
.55 SMITH RICHARD
.21 JONES DIANE
.01 IRVING JOAN

574
19. Numeric Functions

RDNORM and RDUNIF: Generating Random Numbers


Available Languages: reporting, Maintain

The RDNORM and RDUNIF functions generate random numbers:

RDNORM generates double-precision random numbers normally distributed with an


arithmetic mean of 0 and a standard deviation of 1.

If RDNORM generates a large set of numbers (between 1 and 32768), they have the
following properties:

The numbers lie roughly on a bell curve, as shown in the following figure. The bell curve
is highest at the 0 mark, meaning that there are more numbers closer to 0 than farther
away.

The average of the numbers is close to 0.

The numbers can be any size, but most are between 3 and -3.

RDUNIF generates double-precision random numbers uniformly distributed between 0 and 1


(that is, any random number it generates has an equal probability of being anywhere
between 0 and 1).

Using Functions 575


SQRT: Calculating the Square Root

Syntax: How to Generate Random Numbers

{RDNORM|RDUNIF}(output)

where:
RDNORM

Generates double-precision random numbers normally distributed with an arithmetic mean


of 0 and a standard deviation of 1.
RDUNIF

Generates double-precision random numbers uniformly distributed between 0 and 1.


output
Double-precision

Is the name of the field that contains the result, or the format of the output value enclosed
in single quotation marks.

Example: Generating Random Numbers


RDNORM assigns random numbers and stores them in RAND. These numbers are then used
to randomly choose five employee records identified by the values in the LAST NAME and
FIRST NAME fields.

DEFINE FILE EMPLOYEE


RAND/D12.2 WITH LAST_NAME = RDNORM(RAND);END
TABLE FILE EMPLOYEE
PRINT LAST_NAME AND FIRST_NAME
BY HIGHEST 5 RAND
END

The request produces output similar to the following:


RAND LAST_NAME FIRST_NAME
---- --------- ----------
.65 CROSS BARBARA
.20 BANNING JOHN
.19 IRVING JOAN
.00 BLACKWOOD ROSEMARIE
-.14 GREENSPAN MARY

SQRT: Calculating the Square Root


Available Languages: reporting, Maintain

The SQRT function calculates the square root of a number.

576
19. Numeric Functions

Syntax: How to Calculate the Square Root

SQRT(in_value)

where:
in_value
Numeric

Is the value for which the square root is calculated, the name of a field that contains the
value, or an expression that returns the value. If you supply an expression, use
parentheses as needed to ensure the correct order of evaluation. If you supply a negative
number, the result is zero.

Example: Calculating the Square Root


SQRT calculates the square root of LISTPR:
TABLE FILE MOVIES
PRINT LISTPR AND COMPUTE
SQRT_LISTPR/D12.2 = SQRT(LISTPR);BY TITLE
WHERE CATEGORY EQ 'MUSICALS';
END

The output is:


TITLE LISTPR SQRT_LISTPR
----- ------ -----------
ALL THAT JAZZ 19.98 4.47
CABARET 19.98 4.47
CHORUS LINE, A 14.98 3.87
FIDDLER ON THE ROOF 29.95 5.47

XIRR: Calculating the Modified Internal Return Rate (Periodic or Non-Periodic)


Available languages: reporting

The XIRR function calculates the internal rate of return for a series of cash flows that can be
periodic or non-periodic.

Using Functions 577


XIRR: Calculating the Modified Internal Return Rate (Periodic or Non-Periodic)

Syntax: How to Calculate the Internal Rate of Return

TABLE FILE ...


{PRINT|SUM} field ...
COMPUTE rrate/fmt = XIRR (cashflow, dates,guess, maxiterations, output);
WITHIN {sort_field|TABLE}

where:
field ...
Are fields that appear in the report output.
rrate
Is the field that contains the calculated return rate.
fmt
Is the format of the return rate. The data type must be D.
cashflow
Is a numeric field. Each value of this field represents either a payment (negative value) or
income (positive value) for one period. The values must be in the correct sequence in order
for the sequence of cash flows to be calculated correctly. The dates corresponding to each
cash flow should be equally spaced and sorted in chronological order. The calculation
requires at least one negative value and one positive value in the cashflow field. If the
values are all positive or all negative, a zero result is returned.
dates
Is a date field containing the cash flow dates. The dates must be full component dates
with year, month, and day components. Dates cannot be stored in fields with format A, I,
or P. They must be stored in date fields (for example, format YMD, not AYMD). There must
be the same number of dates as there are cash flow values. The number of dates must be
the same as the number of cash flows.
guess
Is an (optional) initial estimate of the expected return rate expressed as a decimal. The
default value is .1 (10%). To accept the default, supply the value 0 (zero) for this
argument.

578
19. Numeric Functions

maxiterations
Is an (optional) number specifying the maximum number of iterations that can be used to
resolve the rate using Newton's method. 50 is the default value. To accept the default,
supply the value 0 (zero) for this argument. The rate is considered to be resolved when
successive iterations do not differ by more than 0.0000003. If this level of accuracy is
achieved within the maximum number of iterations, calculation stops at that point. If it is
not achieved after reaching the maximum number of iterations, calculation stops and the
value calculated by the last iteration is returned.
output
D

Is the name of the field that contains the return rate, or its format enclosed in single
quotation marks.
sort_field
Is a field that sorts the report output and groups it into subsets of rows on which the
function can be calculated separately. To calculate the function using every row of the
report output, use the WITHIN TABLE phrase. A WITHIN phrase is required.

Reference: Usage Notes for the XIRR Function


This function is only supported in a COMPUTE command with the WITHIN phrase.

The cash flow field must contain at least one negative value and one positive value.

Dates cannot be stored in fields with format A, I, or P. They must be stored in date fields
(for example, format YMD, not AYMD).

Cash flows or dates with missing values are not supported.

Example: Calculating the Internal Rate of Return


The following request creates a FOCUS data source with cash flows and dates and calculates
the internal return rate.

The Master File for the data source is:


FILENAME=XIRR01,SUFFIX=FOC
SEGNAME=SEG1,SEGTYPE=S1
FIELDNAME=DUMMY,FORMAT=A2,$
FIELDNAME=DATES,FORMAT=YYMD,$
FIELDNAME=CASHFL,FORMAT=D12.4,$
END

Using Functions 579


XIRR: Calculating the Modified Internal Return Rate (Periodic or Non-Periodic)

The procedure to create the data source is:


CREATE FILE XIRR01
MODIFY FILE XIRR01
FREEFORM DUMMY DATES CASHFL
DATA
AA,19980101,-10000. ,$
BB,19980301,2750. ,$
CC,19981030,4250. ,$
DD,19990215,3250. ,$
EE,19990401,2750. ,$
END

The request is sorted by date so that the correct cash flows can be calculated. The rate
returned by the function is multiplied by 100 in order to express it as a percent rather than a
decimal value. Note that the format includes the % character. This causes a percent symbol to
display, but it does not calculate a percent:
TABLE FILE XIRR01
PRINT CASHFL
COMPUTE RATEX/D12.2%=XIRR(CASHFL, DATES, 0., 0., RATEX) * 100;
WITHIN TABLE
BY DATES
END

One rate is calculated for the entire report because of the WITHIN TABLE phrase:
DATES CASHFL RATEX
---- ------ -----
1998/01/01 -10,000.0000 37.49%
1998/03/01 2,750.0000 37.49%
1998/10/30 4,250.0000 37.49%
1999/02/15 3,250.0000 37.49%
1999/04/01 2,750.0000 37.49%

580
Chapter 20 Maintain-specific Script Functions
Script functions enable you to integrate JavaScript and VBScripts into your Maintain Data
applications and perform client-side execution without returning to the WebFOCUS
Server.

In this chapter:

IWCLink: Displaying a URL in a Browser or Frame

IWCSwitchToSecure and IWCSwitchToUnsecure: Turning the Secure Sockets Layer On


and Off

IWCTrigger: Calling a Maintain Function From a Script Handler

IWC.FindAppCGIValue: Finding a WebFOCUS Parameter or Variable Value

IWC.GetAppCGIValue: Retrieving a WebFOCUS Parameter or Variable

IWCLink: Displaying a URL in a Browser or Frame


The IWCLink function displays a URL in a new browser window or in a frame within your current
form. You can use IWCLink as part of a technique that enables you to invoke an external
procedure without the EXEC command.

Syntax: How to Display a URL in a Browser or Frame

IWCLink(url, [target], [newwindow], [options])

where:
url
Is the URL of the webpage to display.
target
Is the window or frame to send the URL request output to.
newwindow
Determines if the URL appears in a new browser window. When this parameter is a non-
zero or true, a new browser window is created.

Using Functions 581


IWCLink: Displaying a URL in a Browser or Frame

options
Are new window parameters. This includes, but is not limited to, the following:

screenX=distance, which is the distance the new window is placed from the left side of
the screen.

screenY=distance, which is the distance the new window is placed from the top of the
screen.

scrollbars={yes|no}, which determines whether horizontal and vertical scrollbars are


created when the document grows larger than the window dimensions. When set to
yes, scrollbars are created. When set to no, scrollbars are not created.

status={yes|no}, which determines whether a status bar appears at the bottom of the
window. When set to yes, a status bar is created. When set to no, a status bar is not
created.

titlebar={yes|no}, which determines whether a title bar appears at the bottom of the
window. When set to yes, a title bar is created. When set to no, a title bar is not
created.

toolbar={yes|no}, which determines whether a standard browser toolbar appears in the


window. When set to yes, a toolbar is created. When set to no, a toolbar is not created.

height=pixels, which is the height of the window in pixels.

resizable={yes|no}, which determines whether a user is able to resize the window. If


set to yes, a user can resize the window. If set to no, a user cannot resize the window.

Example: Displaying a URL in a Frame


The following JavaScript code is called from a script event handler and uses IWCLink as part of
a technique to invoke an external procedure, and to supply the procedure's parameter
dynamically at run time.
1. var theReport = "http://172.19.81.107/ibi_apps/WFServlet?
IBIF_focexec=rsales4&IBIF_parms=STCD%3D"

2. theReport = theReport + document.Form1.EditBox1.value;

3. IWCLink(theReport,"MyFrame",0,menubar=no,resizable=no,scrollbars=no,
status=no,toolbar=no,height=600,width=600");

582
20. Maintain-specific Script Functions

These commands accomplish the following:

1. Defines a variable named theReport and initializes it to the target URL.


When you use IWCLink as part of a technique to invoke an external procedure, the target
URL must identify the WebFOCUS script (IPAddress/ibi_apps/WFServlet), the target
external procedure (?IBIF_focexec=ProcedureName), and the procedure's parameters
(&IBIF_parms=ParameterName%3D). A parameter name is the name of the target
procedure's corresponding Dialogue Manager variable without the initial ampersand. Note
that %3D is the HTML code for an equal sign; the next statement will append the
parameter's value to the equal sign.

In this case, the target procedure is named rsales4, and rsales4 has a parameter named
STCD.
2. Assigns a value from an edit box (Form1.EditBox1) to the target procedure's STCD
parameter by appending the value to the URL string in theReport.
3. Invokes IWCLink to display a WebFOCUS reporting session, running the external procedure
rsales4, in the frame named MyFrame.
If you want the reporting session to appear in a new browser window, you would leave the
second argument blank and change the third argument from 0 to 1.

The remaining arguments beginning with "menubar" customize the appearance of the
browser window; for information about browser settings, see your browser documentation.

IWCSwitchToSecure and IWCSwitchToUnsecure: Turning the Secure Sockets Layer On and


Off
The IWCSwitchToSecure and IWCSwitchToUnsecure functions turn Secure Sockets Layer on
and off, respectively. Use these two functions when configuring an application that requires
certain transmissions be private, such as transferring credit card information. Currently, these
functions are most commonly used in e-commerce applications.

Before using IWCSwitchToSecure and IWCSwitchToUnsecure, you must do the following:

Obtain secure certification for your Web server.

For an e-commerce application, purchase an e-commerce product from a vendor. The


product verifies your credit card number and completes the transaction.

IWCSwitchToSecure and IWCSwitchToUnsecure are JavaScript functions. JavaScript is case-


sensitive. Ensure you enter these functions exactly as they appear. You can reference these
functions in a VBScript or JavaScript.

Using Functions 583


IWCTrigger: Calling a Maintain Function From a Script Handler

Syntax: How to Turn Secure Sockets Layer On

IWCSwitchToSecure();

Syntax: How to Turn Secure Sockets Layer Off

IWCSwitchToUnsecure();

IWCTrigger: Calling a Maintain Function From a Script Handler


The IWCTrigger function can be used in two ways:

To control the Maintain Data application when a local validation test succeeds. If the
validation test fails, the function returns the end user to the form from which they executed
it.

If you use IWCTrigger in a script library, ensure the Maintain function you are calling is in
the same procedure in which you are using the script library.

To retrieve the value of an ActiveX Control property in a Maintain function. In an event


handler for an ActiveX control event, use a script function for the handler and then call the
Maintain function using IWCTrigger.

IWCTrigger can be used in JavaScript or VBScript. IWCTrigger is a Maintain Data-supplied script


function for use in any Maintain Data application.

Syntax: How to Call a Maintain Function From a Script Handler

IWCTrigger("functionname"[, "parm"]

where:
functionname
Is the Maintain function to call. Scripts are case-sensitive, so you must specify the name
using the same uppercase and lowercase letters that you used to name the function in the
Maintain Data procedure.
parm
Is a parameter being passed to the function.

584
20. Maintain-specific Script Functions

Syntax: How to Retrieve a Parameter From the Called Function

formname.Triggervalue

where:
formname
Is the name of the form in the Maintain Data application.

Syntax: How to Pass the Value of an ActiveX Control Property to a Maintain Function

IWCTrigger ("function",document.form.control.property);

where:
function
Is the Maintain function you are calling.
form
Is the name of the form on which the ActiveX control is located.
control
Is the name of the ActiveX control.
property
Is the name of the ActiveX control property (look for ActiveX control properties in the
ActiveX tab of the property sheet for the ActiveX control).

Example: Passing an ActiveX Control Value to a Maintain Function


If you have an ActiveX calendar control on Form1 that has a property called Month, you can
use IWCTrigger to send the value of Month to a Maintain function called UpdateDate, via either
JavaScript or VBScript:
IWCTrigger("UpdateDate",document.Form1.CalendarControl.Month);

IWC.FindAppCGIValue: Finding a WebFOCUS Parameter or Variable Value


The IWC.FindAppCGIValue function finds WebFOCUS parameter or variable values by pairing the
parameter or variable name with the Maintain Data variable name to which the value is
assigned.

Note: IWC.FindAppCGIValue retrieves values, but cannot directly assign the values to a
Maintain Data variable as IWC.GetAppCGIValue does.

Using Functions 585


IWC.FindAppCGIValue: Finding a WebFOCUS Parameter or Variable Value

Syntax: How to Find a WebFOCUS Parameter Value

IWC.FindAppCGIValue(name,value);

where:
name
Is the WebFOCUS parameter or variable whose value you are finding.
value
Is the Maintain Data variable that receives the value of the WebFOCUS parameter or
variable.

Example: Finding a Variable Value From a Launch Form


IWC.findAppCgiValue finds the user name and password values of the IBIC_user and IBIC_pass
variables, respectively:
Maintain
COMPUTE username/A8;
COMPUTE password/A8;
IWC.findAppCgiValue("IBIC_user", username);
IWC.findAppCgiValue("IBIC_pass", password);

Example: Finding Parameterized Data From Excel


IWC.findAppCgiValue finds the values for fields listed in an Excel file:
MAINTAIN FILE car
MODULE IMPORT (webbase2 errors);
Case Top
compute xlsRetail_Cost/a0;
Infer car.ORIGIN.COUNTRY car.COMP.CAR car.CARREC.MODEL
car.BODY.BODYTYPE car.BODY.RETAIL_COST into car_stack;
car_stack.FocCount=1;
car_stack.FocIndex=1;
iwc.findAppCgiValue("COUNTRY",car_stack.country);
iwc.findAppCgiValue("CAR",car_stack.car);
iwc.findAppCgiValue("MODEL",car_stack.model);
iwc.findAppCgiValue("BODYTYPE",car_stack.bodytype);
iwc.findAppCgiValue("RETAIL_COST",xlsRetail_Cost);
car_stack.retail_cost = xlsRetail_Cost;
update car.BODY.RETAIL_COST from car_stack;
EndCase
END

586
20. Maintain-specific Script Functions

IWC.GetAppCGIValue: Retrieving a WebFOCUS Parameter or Variable


The IWC.GetAppCGIValue function retrieves the value of a WebFOCUS parameter or variable
and imports it into a Maintain Data variable. IWC.GetAppCGIValue returns the value from the
HTTP request header if the name of the parameter or variable is passed.

If the passed parameter or variable name is not found, the function returns a null value.
Therefore, you can check for errors by looking for a null value, then handle the error as
needed.

Both the IWC.FindAppCGIValue and IWC.GetAppCGIValue functions are supported, but it is


recommended you use IWC.GetAppCGIValue. This function allows the parameter or variable
value to be directly assigned to a Maintain Data variable, while IWC.FindAppCGIValue does not.

Note: Unlike Maintain Data variables, WebFOCUS parameters and variables are case-sensitive.

Syntax: How to Retrieve a WebFOCUS Parameter

Declare mnt_var/type_length = IWC.GetAppCGIValue(parm);

where:
mnt_var
Is the Maintain Data variable that receives the ASCII return value of the WebFOCUS
parameter or variable. The value is unescaped before being passed to the Maintain Data
variable.
type_length
Is the selected type and length format of the Maintain Data variable.
parm
Is the WebFOCUS parameter or variable to import. This value is case-sensitive.

Example: Retrieving a WebFOCUS Parameter


IWC.getAppCGIValue retrieves the PRODUCT_ID WebFOCUS parameter:
Maintain File GGPRODS
Infer Product_ID into prodstk;
Declare pcode/a4=IWC.getAppCGIValue("PRODUCT_ID");
For 1 next Product_ID into prodstk where Product_ID eq
pcode;

Using Functions 587


IWC.GetAppCGIValue: Retrieving a WebFOCUS Parameter or Variable

588
Chapter 21 Simplified Statistical Functions
Simplified statistical functions can be called in a COMPUTE command to perform
statistical calculations on the internal matrix that is generated during TABLE request
processing. The STDDEV and CORRELATION functions can also be called as a verb object
in a display command. Prior to calling a statistical function, you need to establish the
size of the partition on which these functions will operate, if the request contains sort
fields.

Note: It is recommended that all numbers and fields used as parameters to these
functions be double-precision.
In this chapter:

Specify the Partition Size for Simplified Statistical Functions

CORRELATION: Calculating the Degree of Correlation Between Two Sets of Data

KMEANS_CLUSTER: Partitioning Observations Into Clusters Based on the Nearest Mean


Value

MULTIREGRESS: Creating a Multivariate Linear Regression Column

OUTLIER: Identifying Outliers in Numeric Data

RSERVE: Running an R Script

STDDEV: Calculating the Standard Deviation for a Set of Data Values

Specify the Partition Size for Simplified Statistical Functions


SET PARTITION_ON = {FIRST|PENULTIMATE|TABLE}

where:
FIRST
Uses the first (also called the major) sort field in the request to partition the values.
PENULTIMATE
Uses the next to last sort field where the COMPUTE is evaluated to partition the values.
This is the default value.

Using Functions 589


CORRELATION: Calculating the Degree of Correlation Between Two Sets of Data

TABLE
Uses the entire internal matrix to calculate the statistical function.

CORRELATION: Calculating the Degree of Correlation Between Two Sets of Data


The CORRELATION function calculates the correlation coefficient between two numeric fields.
The function returns a numeric value between zero (-1.0) and 1.0.

Syntax: How to Calculate the Correlation Coefficient Between Two Fields

CORRELATION(field1, field2)

where:
field1
Numeric

Is the first set of data for the correlation.


field2
Numeric

Is the second set of data for the correlation.

Note: Arguments for CORRELATION cannot be prefixed fields. If you need to work with fields
that have a prefix operator applied, apply the prefix operators to the fields in COMPUTE
commands and save the results in a HOLD file. Then, run the correlation against the HOLD file.

Example: Calculating a Correlation


The following request calculates the correlation between the DOLLARS and BUDDOLLARS
fields converted to double precision.
DEFINE FILE ibisamp/ggsales
DOLLARS/D12.2 = DOLLARS;
BUDDOLLARS/D12.2 = BUDDOLLARS;
END
TABLE FILE ibisamp/ggsales
SUM DOLLARS BUDDOLLARS
CORRELATION(DOLLARS, BUDDOLLARS)
ON TABLE SET PAGE NOLEAD
ON TABLE SET STYLE *
GRID=OFF,$
ENDSTYLE
END

590
21. Simplified Statistical Functions

The output is shown in the following image.

KMEANS_CLUSTER: Partitioning Observations Into Clusters Based on the Nearest Mean


Value
The KMEANS_CLUSTER function partitions observations into a specified number of clusters
based on the nearest mean value. The function returns the cluster number assigned to the
field value passed as a parameter.

Note: If there are not enough points to create the number of clusters requested, the value -10
is returned for any cluster that cannot be created.

Syntax: How to Partition Observations Into Clusters Based on the Nearest Mean Value

KMEANS_CLUSTER(number, percent, iterations, tolerance,


[prefix1.]field1[, [prefix1.]field2 ...])

where:
number
Integer

Is number of clusters to extract.


percent
Numeric

Is the percent of training set size (the percent of the total data to use in the calculations).
The default value is AUTO, which uses the internal default percent.
iterations
Integer

Is the maximum number of times to recalculate using the means previously generated. The
default value is AUTO, which uses the internal default number of iterations.
tolerance
Numeric

Is a weight value between zero (0) and 1.0. The value AUTO uses the internal default
tolerance.

Using Functions 591


KMEANS_CLUSTER: Partitioning Observations Into Clusters Based on the Nearest Mean Value

prefix1, prefix2
Defines an optional aggregation operator to apply to the field before using it in the
calculation. Valid operators are:

SUM. which calculates the sum of the field values. SUM is the default value.

CNT. which calculates a count of the field values.

AVE. which calculates the average of the field values.

MIN. which calculates the minimum of the field values.

MAX. which calculates the maximum of the field values.

FST. which retrieves the first value of the field.

LST. which retrieves the last value of the field.

Note: The operators PCT., RPCT., TOT., MDN., MDE., RNK., and DST. are not supported.
field1
Numeric

Is the set of data to be analyzed.


field2
Numeric

Is an optional set of data to be analyzed.

592
21. Simplified Statistical Functions

Example: Partitioning Data Values Into Clusters


The following request partitions the DOLLARS field values into four clusters and displays the
result as a scatter chart in which the color represents the cluster. The request uses the
default values for the percent, iterations, and tolerance parameters by passing them as the
value 0 (zero).
SET PARTITION_ON = PENULTIMATE
GRAPH FILE GGSALES
PRINT UNITS DOLLARS
COMPUTE KMEAN1/D20.2 TITLE 'K-MEANS'= KMEANS_CLUSTER(4, AUTO, AUTO, AUTO,
DOLLARS);
ON GRAPH SET LOOKGRAPH SCATTER
ON GRAPH PCHOLD FORMAT JSCHART
ON GRAPH SET STYLE *
INCLUDE=IBFS:/FILE/IBI_HTML_DIR/ibi_themes/Warm.sty,$
type = data, column = N2, bucket=y-axis,$
type=data, column= N1, bucket=x-axis,$
type=data, column=N3, bucket=color,$
GRID=OFF,$
*GRAPH_JS_FINAL
colorScale: {
colorMode: 'discrete',
colorBands: [{start: 1, stop: 1.99, color: 'red'}, {start: 2, stop:
2.99, color: 'green'},
{start: 3, stop: 3.99, color: 'yellow'}, {start: 3.99, stop:
4, color: 'blue'} ]
}
*END
ENDSTYLE
END

Using Functions 593


MULTIREGRESS: Creating a Multivariate Linear Regression Column

The output is shown in the following image.

MULTIREGRESS: Creating a Multivariate Linear Regression Column


MULTIREGRESS derives a linear equation that best fits a set of numeric data points, and uses
this equation to create a new column in the report output. The equation can be based on one
or more independent variables.

The equation generated is of the following form, where y is the dependent variable and x1, x2,
and x3 are the independent variables.
y = a1*x1 [+ a2*x2 [+ a3*x3] ...] + b

When there is one independent variable, the equation represents a straight line. When there
are two independent variables, the equation represents a plane, and with three independent
variables, it represents a hyperplane. You should use this technique when you have reason to
believe that the dependent variable can be approximated by a linear combination of the
independent variables.

Syntax: How to Create a Multivariate Linear Regression Column

MULTIREGRESS(input_field1, [input_field2, ...])

594
21. Simplified Statistical Functions

where:
input_field1, input_field2 ...
Are any number of field names to be used as the independent variables. They should be
independent of each other. If an input field is non-numeric, it will be categorized to
transform it to numeric values that can be used in the linear regression calculation.

Example: Creating a Multivariate Linear Regression Column


The following request uses the DOLLARS and BUDDOLLARS fields to generate a regression
column named Estimated_Dollars.
GRAPH FILE GGSALES
SUM BUDUNITS UNITS BUDDOLLARS DOLLARS
COMPUTE Estimated_Dollars/F8 = MULTIREGRESS(DOLLARS, BUDDOLLARS);
BY DATE
ON GRAPH SET LOOKGRAPH LINE
ON GRAPH PCHOLD FORMAT JSCHART
ON GRAPH SET STYLE *
INCLUDE=IBFS:/FILE/IBI_HTML_DIR/ibi_themes/Warm.sty,$
type=data, column = n1, bucket = x-axis,$
type=data, column= dollars, bucket=y-axis,$
type=data, column= buddollars, bucket=y-axis,$
type=data, column= Estimated_Dollars, bucket=y-axis,$
*GRAPH_JS
"series":[
{"series":2, "color":"orange"}]
*END
ENDSTYLE
END

Using Functions 595


OUTLIER: Identifying Outliers in Numeric Data

The output is shown in the following image. The orange line represents the regression
equation.

OUTLIER: Identifying Outliers in Numeric Data


The 1.5 * IQR (Inner Quartile Range) rule is a common way to identify outliers in data. This rule
defines an outlier as a value that is above or below 1.5 times the inner quartile range in the
data. The inner quartile range is based on sorting the data values, dividing it into equal
quarters, and calculating the range of values between the first quartile (the value one quarter
of the way through the sorted data) and third quartile (the value three quarters of the way
through the sorted data). The value that is 1.5 times below the inner quartile range is called
the lower fence, and the value that is 1.5 times above the inner quartile range is called the
upper fence.

OUTLIER is not supported in a DEFINE expression. It can be used in a COMPUTE expression or


a WHERE, WHERE TOTAL, or WHERE_GROUPED phrase.

Given a numeric field as input, OUTLIER returns one of the following values for each value of
the field, using the 1.5 * IQR rule:

0 (zero). The value is not an outlier.

-1. The value is below the lower fence.

1. The value is above the upper fence.

596
21. Simplified Statistical Functions

Syntax: How to Identify Outliers in Numeric Data

OUTLIER(input_field)

where:
input_field
Numeric

Is the numeric field to be analyzed.

Example: Identifying Outliers


The following request defines the SALES field to have different values depending on the store
code, and uses OUTLIER to determine whether each field value is an outlier.
DEFINE FILE GGSALES
SALES/D12 = IF ((CATEGORY EQ 'Coffee') AND (STCD EQ 'R1019')) THEN 19000
ELSE IF ((CATEGORY EQ 'Coffee') AND (STCD EQ 'R1020')) THEN 20000
ELSE IF ((CATEGORY EQ 'Coffee') AND (STCD EQ 'R1040')) THEN 7000
ELSE DOLLARS;
END
TABLE FILE GGSALES
SUM SALES
COMPUTE OUT1/I3 = OUTLIER(SALES);
BY CATEGORY
BY STCD
WHERE CATEGORY EQ 'Coffee'
ON TABLE SET PAGE NOLEAD
ON TABLE SET STYLE *
GRID=OFF,$
ENDSTYLE
END

Using Functions 597


RSERVE: Running an R Script

The output is shown in the following image. Values above 2 million are above the upper fence,
values below 1 million are below the lower fence, and other values are not outliers:

RSERVE: Running an R Script


You can use the RSERVE function in a COMPUTE command to run an R script that returns
vector output. This requires that you have a configured Adapter for Rserve.

Syntax: How to Run an R Script

RSERVE(rserve_mf, input_field1, ...input_fieldn, output)

where:
rserve_mf
Is the synonym for the R script.
input_field1, ...input_fieldn
Are the independent variables used by the R script.
output
Is the dependent variable returned by the R script. It must be a single column (vector) of
output.

598
21. Simplified Statistical Functions

Example: Using RSERVE to Run an R Script


The R script named wine_run_model.R predicts Bordeaux wine prices based on the average
growing season temperature, the amount of rain during the harvest season, the amount of rain
during the winter, and the age of the wine.

Using a configured connection (named MyRserve) for the Adapter for Rserve, and a sample
data file named wine_input_sample.csv, you create the following synonym for the R script, as
described in the Adapter Administration manual.

Master File
FILENAME=WINE_RUN_MODEL, SUFFIX=RSERVE , $
SEGMENT=INPUT_DATA, SEGTYPE=S0, $
FIELDNAME=AGST, ALIAS=AGST, USAGE=D9.4, ACTUAL=STRING,
MISSING=ON,
TITLE='AGST', $
FIELDNAME=HARVESTRAIN, ALIAS=HarvestRain, USAGE=I11, ACTUAL=STRING,
MISSING=ON,
TITLE='HarvestRain', $
FIELDNAME=WINTERRAIN, ALIAS=WinterRain, USAGE=I11, ACTUAL=STRING,
MISSING=ON,
TITLE='WinterRain', $
FIELDNAME=AGE, ALIAS=Age, USAGE=I11, ACTUAL=STRING,
MISSING=ON,
TITLE='Age', $
SEGMENT=OUTPUT_DATA, SEGTYPE=U, PARENT=INPUT_DATA, $
FIELDNAME=PRICE, ALIAS=Price, USAGE=D18.14, ACTUAL=STRING,
MISSING=ON,
TITLE='Price', $

Access File
SEGNAME=INPUT_DATA,
CONNECTION=MyRserve,
R_SCRIPT=/prediction/wine_run_model.r,
R_SCRIPT_LOCATION=WFRS,
R_INPUT_SAMPLE_DAT=prediction/wine_input_sample.csv, $

Using Functions 599


RSERVE: Running an R Script

Now that the synonym has been created for the model, the model will be used to run against
the following data file named wine_forecast.csv.

Year,Price,WinterRain,AGST,HarvestRain,Age,FrancePop
1952,7.495,600,17.1167,160,31,43183.569
1953,8.0393,690,16.7333,80,30,43495.03
1955,7.6858,502,17.15,130,28,44217.857
1957,6.9845,420,16.1333,110,26,45152.252
1958,6.7772,582,16.4167,187,25,45653.805
1959,8.0757,485,17.4833,187,24,46128.638
1960,6.5188,763,16.4167,290,23,46583.995
1961,8.4937,830,17.3333,38,22,47128.005
1962,7.388,697,16.3,52,21,48088.673
1963,6.7127,608,15.7167,155,20,48798.99
1964,7.3094,402,17.2667,96,19,49356.943
1965,6.2518,602,15.3667,267,18,49801.821
1966,7.7443,819,16.5333,86,17,50254.966
1967,6.8398,714,16.2333,118,16,50650.406
1968,6.2435,610,16.2,292,15,51034.413
1969,6.3459,575,16.55,244,14,51470.276
1970,7.5883,622,16.6667,89,13,51918.389
1971,7.1934,551,16.7667,112,12,52431.647
1972,6.2049,536,14.9833,158,11,52894.183
1973,6.6367,376,17.0667,123,10,53332.805
1974,6.2941,574,16.3,184,9,53689.61
1975,7.292,572,16.95,171,8,53955.042
1976,7.1211,418,17.65,247,7,54159.049
1977,6.2587,821,15.5833,87,6,54378.362
1978,7.186,763,15.8167,51,5,54602.193

The data file can be any type of file that R can read. In this case it is another .csv file. This file
needs a synonym in order to be used in a report request. You create the synonym for this file
using the Adapter for Delimited Files.

The following is the generated Master File, wine_forecast.mas.

FILENAME=WINE_FORECAST, SUFFIX=DFIX , CODEPAGE=1252,


DATASET=prediction/wine_forecast.csv, $
SEGMENT=WINE_FORECAST, SEGTYPE=S0, $
FIELDNAME=YEAR1, ALIAS=Year, USAGE=I6, ACTUAL=A5V,
MISSING=ON, TITLE='Year', $
FIELDNAME=PRICE, ALIAS=Price, USAGE=D8.4, ACTUAL=A7V,
MISSING=ON, TITLE='Price', $
FIELDNAME=WINTERRAIN, ALIAS=WinterRain, USAGE=I5, ACTUAL=A3V,
MISSING=ON, TITLE='WinterRain', $
FIELDNAME=AGST, ALIAS=AGST, USAGE=D9.4, ACTUAL=A8V,
MISSING=ON, TITLE='AGST', $
FIELDNAME=HARVESTRAIN, ALIAS=HarvestRain, USAGE=I5, ACTUAL=A3V,
MISSING=ON, TITLE='HarvestRain', $
FIELDNAME=AGE, ALIAS=Age, USAGE=I4, ACTUAL=A2V, MISSING=ON,
TITLE='Age', $
FIELDNAME=FRANCEPOP, ALIAS=FrancePop, USAGE=D11.3, ACTUAL=A11V,
MISSING=ON, TITLE='FrancePop', $

600
21. Simplified Statistical Functions

The following is the generated Access File, wine_forecast.acx.


SEGNAME=WINE_FORECAST, DELIMITER=',', ENCLOSURE=", HEADER=YES,
CDN=COMMAS_DOT, CONNECTION=<local>, $

The following request, wine_forecast_price_report.fex, uses the RSERVE bulit-in function to run
the script and return a report.
-*wine_forecast_price_report.fex
TABLE FILE PREDICTION/WINE_FORECAST
PRINT
YEAR
WINTERRAIN
AGST
HARVESTRAIN
AGE

COMPUTE PREDICTED_PRICE/D18.2 MISSING ON ALL=


RSERVE(prediction/wine_run_model, AGST, HARVESTRAIN, WINTERRAIN, AGE, Price); AS
'Predicted,Price'

ON TABLE SET PAGE NOLEAD


ON TABLE SET STYLE *
GRID=OFF,$
ENDSTYLE
END

Using Functions 601


RSERVE: Running an R Script

The output is shown in the following image.

602
21. Simplified Statistical Functions

STDDEV: Calculating the Standard Deviation for a Set of Data Values


The STDDEV function returns a numeric value that represents the amount of dispersion in the
data. The set of data can be specified as the entire population or a sample. The standard
deviation is the square root of the variance, which is a measure of how observations deviate
from their expected value (mean). If specified as a population, the divisor in the standard
deviation calculation (also called degrees of freedom) will be the total number of data points,
N. If specified as a sample, the divisor will be N-1.

If x¡ is an observation, N is the number of observations, and µ is the mean of all of the


observations, the formula for calculating the standard deviation for a population is:

To calculate the standard deviation for a sample, the mean is calculated using the sample
observations, and the divisor is N-1 instead of N.

Reference: Calculate the Standard Deviation in a Set of Data

STDDEV(field, sampling)

where:
field
Numeric

Is the set of observations for the standard deviation calculation.


sampling
Keyword

Indicates the origin of the data set. Can be one of the following values.

P Entire population.

S Sample of population.

Note: Arguments for STDDEV cannot be prefixed fields. If you need to work with fields that
have a prefix operator applied, apply the prefix operators to the fields in COMPUTE commands
and save the results in a HOLD file. Then, run the standard deviation against the HOLD file.

Using Functions 603


STDDEV: Calculating the Standard Deviation for a Set of Data Values

Example: Calculating a Standard Deviation


The following request calculates the standard deviation of the DOLLARS field converted to
double precision.
DEFINE FILE ibisamp/ggsales
DOLLARS/D12.2 = DOLLARS;
END
TABLE FILE ibisamp/ggsales
SUM DOLLARS STDDEV(DOLLARS,S)
ON TABLE SET PAGE NOLEAD
ON TABLE SET STYLE *
GRID=OFF,$
ENDSTYLE
END

The output is shown in the following image.

604
Chapter 22 Machine Learning (Python-based)
Functions
The Machine Learning (Python-based) functions are FOCUS functions implemented as
Python scripts. These Python scripts take advantage of Python packages such as scipy,
numpy, scikit-learn, and pandas, which extends the Python capabilities to machine
learning.

The Machine Learning (Python-based) functions perform regression, classification,


extreme gradient boosting, and outlier detection using a variety of machine learning
methods. The Python scripts perform a sequence of conventional machine learning tasks
including scaling of the data where appropriate. They are built around a grid search with
cross-validation. That is, some hyper-parameters (parameters that influence the learning
process, but that are not model parameters) are identified, and models are built using a
number of values for each hyper-parameter, in order to determine the optimal values. To
determine optimality, cross-validation is used, which ensures that the performance is
measured on a validation-subset of the data that is distinct from the training-subset.
Rows with missing values in the target-column are not used for training and validation,
but a predicted value is computed by the trained model.

You can generate the .csv file and accompanying Master File used in the examples by
running the WebFOCUS - Machine Learning Demo tutorial in the server Web Console.
In this chapter:

ANOMALY_IF: Detecting Outliers

CLASSIFY_BLR: Binary Logistic Regression

CLASSIFY_KNN: K-Nearest Neighbors Classification

CLASSIFY_RF: Random Forest Classification

CLASSIFY_XGB: Extreme Gradient Boosting Classification

REGRESS_KNN: K-Nearest Neighbors Regression

REGRESS_POLY: Polynomial Regression

REGRESS_RF: Random Forest Regression

REGRESS_XGB: Extreme Gradient Boosting Regression

RUN_MODEL and RUN_MODEL2: Running a Saved Python Model

Using Functions 605


ANOMALY_IF: Detecting Outliers

ANOMALY_IF: Detecting Outliers


ANOMALY_IF detects outliers using an Isolation Forest. An Isolation Forest uses decision trees
to randomly and recursively split the space spanned by the observations X0, X1, . . . into
hyper-rectangles, each containing one or a small number of samples. Outlier samples can be
isolated in a hyper-rectangle with fewer splits than samples that are in close vicinity of many
other samples. The number of splits needed to reach a sample translates into the anomaly
score of the sample.

Syntax: How to Calculate an Anomaly Score

ANOMALY_IF('options' predictor_field1[, predictor_field2, ...])

where:
'options'
Is a dictionary of advanced parameters that control the model attributes, enclosed in
single quotation marks. Most of these parameters have a default value, so you can omit
them from the request, if you want to use the default values. Even with no advanced
parameters, the single quotation marks are required. The format of the advanced
parameter dictionary is:
'{"parm_name1": "parm_value1", ... ,"parm_namei": "parm_valuei"}'

The following advanced parameters are supported:


"trees"
Is the number of decision trees in the forest. Allowed values are integers greater than
10. The default value is "100".
"score"
Defines the type of value returned by the function. If score is "binary", the function
returns -1 for anomalous samples and +1 for normal samples. If score is "grade", a
continuous anomaly score between -1.0 and 1.0 is returned, in which the more
negative the number returned is, the more of an outlier the point is. Valid values are
"binary" and "grade". The default value is "binary".
"max_samples"
Is the fraction of the rows in the training set used per tree. Allowed values are
fractions between 0 (zero) and 1. The default value is "0.5".
"contamination"
Only applies when score is "binary". Is the fraction of the samples in the training
set that will be marked anomalous. Allowed values are fractions between 0 (zero)
and 0.5. The default value is "0.1".

606
22. Machine Learning (Python-based) Functions

"train_ratio"
Is a value between 0 and 1 that specifies the fraction of data used for training the
model. The default value is "1.0".
predictor_field1[, predictor_field2, ...]
Numeric

Are one or more predictor field names.

Example: Detecting Outliers Using ANOMALY_IF


The following procedure uses ANOMALY_IF to detect outliers using binary mode and predictors
horsepower, peak RPM, city MPG, highway MPG, and price. Outliers are identified by the return
value -1.00.
TABLE FILE imports85
PRINT horsepower peakRpm cityMpg highwayMpg price
COMPUTE AnomalyBinaryScore/D5.2 =
ANOMALY_IF('{"trees":"123","score":"binary","contamination":"0.2"}',
horsepower, peakRpm, cityMpg, highwayMpg, price);
ON TABLE SET PAGE NOLEAD
ON TABLE SET STYLE *
GRID=OFF,$
ENDSTYLE
END

Using Functions 607


ANOMALY_IF: Detecting Outliers

Partial output is shown in the following image.

The following version of the request uses grade mode with the same advanced parameters and
predictors.
TABLE FILE imports85
PRINT horsepower peakRpm cityMpg highwayMpg price
COMPUTE AnomalyGradeScore/D5.2 =
ANOMALY_IF('{"trees":"123","score":"grade","contamination":"0.2"}',
horsepower, peakRpm, cityMpg,
highwayMpg, price);
ON TABLE SET PAGE NOLEAD
ON TABLE SET STYLE *
GRID=OFF,$
ENDSTYLE
END

608
22. Machine Learning (Python-based) Functions

Partial output is shown in the following image.

CLASSIFY_BLR: Binary Logistic Regression


Binary logistic regression finds the best linear separation between two classes in the space
spanned by the predictors. The target variable has two values (0 and 1), corresponding to the
two classes. The predicted value is either a class assignment (0 or 1) or the probability of
belonging to class 1.

Using Functions 609


CLASSIFY_BLR: Binary Logistic Regression

Syntax: How to Calculate a Binary Logistic Regression

CLASSIFY_BLR('options',
predictor_field1[, predictor_field2, ...] target_field)

where:
'options'
Is a dictionary of advanced parameters that control the model attributes, enclosed in
single quotation marks. Most of these parameters have a default value, so you can omit
them from the request, if you want to use the default values. Even with no advanced
parameters, the single quotation marks are required. The format of the advanced
parameter dictionary is:
'{"parm_name1": "parm_value1", ... ,"parm_namei": "parm_valuei"}'

The following advanced parameters are supported:


"proba"
Indicates whether to display a probability of being in the target class or a class
prediction. Valid values are "no" (the default) or "yes". The value "yes" produces a
probability Y (0<=Y<=1) for belonging to the class specified by "pos_label" (default is
"1") for each point in the space. The value "no" produces a class-prediction Y (0 or 1)
for each point in the space.
"pos_label"
Is relevant when "proba" is "yes". Specifies the class for which the probability of
belonging is to be computed. Valid values are "0" and "1". The default is "1".
"train_ratio"
Is a value between 0 and 1 that specifies the fraction of data used for training the
model. The default value is "0.8".
"test_ratio"
Is a value between 0 and 1 that specifies the fraction of data used for testing the
model. The default value is "0.2".
"l2_grid"
Is a grid consisting of comma-separated positive numbers to be used as L2-
regularization strengths. The default value is "0,1,1,10". The optimal value is chosen
by cross-validation.
"kfold"
Is the number of folds used for cross-validation. Suggested values are integers
between "2" and "10". The default value is "4".

610
22. Machine Learning (Python-based) Functions

predictor_field1[, predictor_field2, ...]


Numeric

Are one or more predictor field names.


target_field
Numeric

Is the target field.

Example: Using CLASSIFY_BLR to Predict Number of Doors


The following TABLE request uses CLASSIFY_BLR to compute the number of doors in a car,
using the default values for the advanced parameters and predictors price, height, horsepower,
peak RPM, city MPG, and highway MPG.
TABLE FILE imports85
PRINT numOfDoors
COMPUTE predictedNumOfDoors/A6 =
CLASSIFY_BLR('{"proba":"no","kfold":"4","test_ratio":"0.2"}',
price, height, horsepower, peakRpm,
cityMpg, highwayMpg, numOfDoors);
WHERE price LT 30000
ON TABLE SET PAGE NOPAGE
ON TABLE SET STYLE *
GRID=OFF,$
ENDSTYLE
END

Using Functions 611


CLASSIFY_BLR: Binary Logistic Regression

The partial output is shown in the following image.

612
22. Machine Learning (Python-based) Functions

CLASSIFY_KNN: K-Nearest Neighbors Classification


K-nearest neighbors classification is a method for assigning a class membership to a data
point in the space spanned by the predictors. The classification is done by assigning the class
most common among its k nearest neighbors. This method requires having a distance
definition in this space.

Reference: Calculate a K-Nearest Neighbors Classification

CLASSIFY_KNN('options',
predictor_field1[, predictor_field2, ...] target_field)

where:
'options'
Is a dictionary of advanced parameters that control the model attributes, enclosed in
single quotation marks. Most of these parameters have a default value, so you can omit
them from the request, if you want to use the default values. Even with no advanced
parameters, the single quotation marks are required. The format of the advanced
parameter dictionary is:
'{"parm_name1": "parm_value1", ... ,"parm_namei": "parm_valuei"}'

The following advanced parameters are supported:


"k"
Is the number of nearest neighbors. Allowed values are integers greater than 1. The
default value is "5".
"p"
Is the power (p) of the L^p-distance. Allowed values are positive integers. Suggested
values are "1" and "2". The default value is "2".

power=1 calculates the distance as the sum of the absolute values of the
differences between the coordinates (Manhattan distance).

power=2 calculates the distance as the square root of the sum of the squares of
the differences between the coordinates (Euclidean distance). This is the default
value.
"prediction_ratio"
Is the fraction of the data that will be used for prediction. Allowed values are numbers
between 0 and 1. The default value is "0.8".
"test_ratio"
Is a value between 0 and 1 that specifies the fraction of data used for testing the
model. The default value is "0.2".

Using Functions 613


CLASSIFY_KNN: K-Nearest Neighbors Classification

"kfold"
Is the number of folds used in the grid-search with cross-validation. A grid-search of
the nearest neighbors grid K/2, K, 2K is done. Suggested values are integers between
"2" and "10". The default value is "4".
predictor_field1[, predictor_field2, ...]
Numeric

Are one or more predictor field names.


target_field
Numeric

Is the target field.

Example: Predicting Class Assignment Using CLASSIFY_KNN


The following request creates classes 0 (zero) and 1 based on the number of doors in a car,
then uses CLASSIFY_KNN to predict the class assignment using the 10 nearest neighbors and
Euclidean distance, with predictors price, height, horsepower, peak RPM, city MPG, and
highway MPG. The data set includes rows with missing target values.
DEFINE FILE imports85
OUR_CLASSES/I2 MISSING ON = IF numOfDoors EQ MISSING THEN MISSING
ELSE IF numOfDoors EQ 'two' THEN 0
ELSE 1;
END
TABLE FILE imports85
PRINT numOfDoors BODYSTYLE OUR_CLASSES
COMPUTE predictedCLASSES/I2 =
CLASSIFY_KNN('{"K":"10","p":"2","kfold":"4","test_ratio":"0.2"}',
price, height, horsepower, peakRpm,
cityMpg, highwayMpg, OUR_CLASSES);

ON TABLE SET PAGE NOPAGE


ON TABLE SET STYLE *
GRID=OFF,$
ENDSTYLE
END

614
22. Machine Learning (Python-based) Functions

Partial output is shown in the following image.

CLASSIFY_RF: Random Forest Classification


CLASSIFY_RF creates a random forest, which is an ensemble of decision trees. Each decision
tree produces an independent classification prediction, and the prediction of the forest is the
majority vote of the individual predictions.

Syntax: How to Calculate a Random Forest Classification

CLASSIFY_RF('options', number_of_trees,
predictor_field1[, predictor_field2, ...] target_field)

Using Functions 615


CLASSIFY_RF: Random Forest Classification

where:
'options'
Is a dictionary of advanced parameters that control the model attributes, enclosed in
single quotation marks. Most of these parameters have a default value, so you can omit
them from the request, if you want to use the default values. Even with no advanced
parameters, the single quotation marks are required. The format of the advanced
parameter dictionary is:
'{"parm_name1": "parm_value1", ... ,"parm_namei": "parm_valuei"}'

The following advanced parameters are supported:


"trees"
Is the number of decision trees in the forest. Allowed values are integers greater than
10. The default value is "100".
"feature_importances"
Specifies whether to compute feature importances. Valid values are "yes" and "no".
The default value is "yes".
"train_ratio"
Is a value between 0 and 1 that specifies the fraction of data used for training the
model. The default value is "0.8".
"test_ratio"
Is a value between 0 and 1 that specifies the fraction of data used for testing the
model. The default value is "0.2".
"scoring"
The training optimizes either for F1 Score (weighted average of Precision and
Recall) or accuracy (ratio of correctly predicted observations to total observations).
Allowed values are "f1_score" and "accuracy". The default value is "f1_score".
"min_values_leaf_grid"
Is a grid of the minimum number of samples required in a node in order to split the
node, or a single value. The optimal value is chosen by cross-validation. The default
value is "1,3,5".
predictor_field1[, predictor_field2, ...]
Numeric or alphanumeric

Are one or more predictor field names.

616
22. Machine Learning (Python-based) Functions

target_field
Numeric or alphanumeric

Is the target field.

Example: Predicting Class Assignment Using CLASSIFY_RF


The following procedure uses CLASSIFY_RF to predict a class assignment for the number of
doors in a car, using a random forest with 100 decision trees, with predictors price, body style,
height, horsepower, peak RPM, city MPG, highway MPG.
TABLE FILE imports85
PRINT numOfDoors
COMPUTE predictedNumOfDoors/A6 =
CLASSIFY_RF('{"trees":"100","kfold":"4","test_ratio":"0.2"}',
price, bodyStyle, height, horsepower,
peakRpm, cityMpg, highwayMpg, numOfDoors);
WHERE price LT 30000
ON TABLE SET PAGE NOLEAD
ON TABLE SET STYLE *
GRID=OFF,$
ENDSTYLE
END

Using Functions 617


CLASSIFY_RF: Random Forest Classification

Partial output is shown in the following image.

618
22. Machine Learning (Python-based) Functions

CLASSIFY_XGB: Extreme Gradient Boosting Classification


CLASSIFY_XGB creates a series of decision trees, where each new tree attempts to improve
on the predictive capabilities of the prior trees. Each decision tree arrives at its prediction
based on a partitioning of the space spanned by the predictors, using a recursive succession
of binary splits. At each split, the values of one predictor are partitioned into two sets. The
splits are guided (choice of the split-predictor and the split-point) by the greedy maximization of
an objective function that involves measuring how much alike the y-values of the data points in
each partition are (called purity), as well as a regularization term. Early stopping is applied
when the predictive capability starts to deteriorate.

Syntax: How to Calculate an Extreme Gradient Boosting Classification

CLASSIFY_XGB('options',
predictor_field1[, predictor_field2, ...] target_field)

where:
'options'
Is a dictionary of advanced parameters that control the model attributes, enclosed in
single quotation marks. Most of these parameters have a default value, so you can omit
them from the request, if you want to use the default values. Even with no advanced
parameters, the single quotation marks are required. The format of the advanced
parameter dictionary is:

'{"parm_name1": "parm_value1", ... ,"parm_namei": "parm_valuei"}'

The following advanced parameters are supported:


"trees"
Is the number of decision trees in the forest. Allowed values are integers greater than
10. The default value is "300".
"train_ratio"
Is a value between 0 and 1 that specifies the fraction of data used for training the
model. The default value is "0.8".
"test_ratio"
Is a value between 0 and 1 that specifies the fraction of data used for testing the
model. The default value is "0.2".
"early_stopping_rounds"
Specifies the number of added trees such that if the algorithm has not improved its
performance in this many added trees, it stops. Allowed values are integers between 1
and the number of trees. The default value is "10".

Using Functions 619


CLASSIFY_XGB: Extreme Gradient Boosting Classification

"l2_grid"
Is a grid consisting of comma-separated positive numbers to be used as L2-
regularization strengths. The default value is "0,1,1,10". The optimal value is chosen
by cross-validation.
"kfold"
Is the number of folds used in the grid-search with cross-validation. Suggested values
are integers between "2" and "10". The default value is "4".
"max_depth_grid"
Is the maximum depth of each decision tree. A grid in the form "4,5,6" is allowed. The
default value is "5". The optimal value is chosen by cross-validation.
"scoring"
The training optimizes either for F1 Score (weighted average of Precision and
Recall) or accuracy (ratio of correctly predicted observations to total observations).
Allowed values are "f1_score" and "accuracy". The default value is "f1_score".
predictor_field1[, predictor_field2, ...]
Numeric or Alphanumeric

Are one or more predictor field names.


target_field
Numeric or alphanumeric

Is the target field.

Example: Predicting Price Using CLASSIFY_XGB


The following procedure uses CLASSIFY_XGB to predict number, using 400 trees, 10 early
stopping rounds, and default values for the other advanced parameters, and with predictors
price, body style, height, horsepower, peak RPM, city MPG, and highway MPG.
TABLE FILE imports85
PRINT numOfDoors
COMPUTE predictedNumOfDoors/A6 =
CLASSIFY_XGB('{"trees":"400","early_stopping_rounds":"10",
"kfold":"4","test_ratio":"0.2"}',
price, bodyStyle, height, horsepower,
peakRpm, cityMpg, highwayMpg, numOfDoors);
WHERE price LT 30000
ON TABLE SET PAGE NOLEAD
ON TABLE SET STYLE *
GRID=OFF,$
ENDSTYLE
END

620
22. Machine Learning (Python-based) Functions

Partial output is shown in the following image.

Using Functions 621


REGRESS_KNN: K-Nearest Neighbors Regression

REGRESS_KNN: K-Nearest Neighbors Regression


K-nearest neighbors regression is a method for predicting a target value for a data point in the
space spanned by the predictors. The prediction is the average of the target values of its K
nearest neighbors. This method requires having a distance definition in this space.

Reference: Calculate a K-Nearest Neighbors Regression

REGRESS_KNN('options',
predictor_field1[, predictor_field2, ...] target_field)

where:
'options'
Is a dictionary of advanced parameters that control the model attributes, enclosed in
single quotation marks. Most of these parameters have a default value, so you can omit
them from the request, if you want to use the default values. Even with no advanced
parameters, the single quotation marks are required. The format of the advanced
parameter dictionary is:
'{"parm_name1": "parm_value1", ... ,"parm_namei": "parm_valuei"}'

The following advanced parameters are supported:


"K"
Is the number of nearest neighbors to participate in the prediction. Allowed values are
integers greater than 1. The default value is "5".
"p"
Is the power (p) of the L^p-distance. Allowed values are integers. Suggested values
are "1" and "2". The default value is "2".

power=1 calculates the distance as the sum of the absolute values of the
differences between the coordinates (Manhattan distance).

power=2 calculates the distance as the square root of the sum of the squares of
the differences between the coordinates (Euclidean distance).
"prediction_ratio"
Is the fraction of the data that will be used for prediction. Allowed values are numbers
between 0 and 1. The default value is "0.8".
"test_ratio"
Is a value between 0 and 1 that specifies the fraction of data used for testing the
model. The default value is "0.2".

622
22. Machine Learning (Python-based) Functions

"kfold"
Is the number of folds used in the grid-search with cross-validation. A grid-search of
the nearest neighbors grid K/2, K, 2K is done. Suggested values are integers between
"2" and "10". The default value is "4".
predictor_field1[, predictor_field2, ...]
Numeric

Are one or more predictor field names.


target_field
Numeric

Is the target field.

Example: Predicting Price Using REGRESS_KNN


The following request uses REGRESS_KNN to predict price using the default advanced
parameters (10 nearest neighbors and Euclidean distance), with predictors height,
horsepower, peak RPM, city MPG, and highway MPG.
TABLE FILE imports85
PRINT price
COMPUTE predictedPrice/I5 =
REGRESS_KNN('{"K":"10","p":"2","kfold":"4","prediction_ratio":"0.8","test_ra
tio":"0.2"}',
height, horsepower, peakRpm,
cityMpg, highwayMpg, price);
WHERE price LT 30000
ON TABLE SET PAGE NOLEAD
ON TABLE SET STYLE *
GRID=OFF,$
ENDSTYLE
END

Using Functions 623


REGRESS_POLY: Polynomial Regression

Partial output is shown in the following image.

REGRESS_POLY: Polynomial Regression


Polynomial regression fits the target column to a polynomial expression of the predictor
columns. The degree of the polynomial is specified as an input argument to the function.

624
22. Machine Learning (Python-based) Functions

Reference: Calculate a Polynomial Regression Column

REGRESS_POLY('options',
predictor_field1, predictor_field2, [...,] target_field)

where:
'options'
Is a dictionary of advanced parameters that control the model attributes, enclosed in
single quotation marks. Most of these parameters have a default value, so you can omit
them from the request, if you want to use the default values. Even with no advanced
parameters, the single quotation marks are required. The format of the advanced
parameter dictionary is:
'{"parm_name1": "parm_value1", ... ,"parm_namei": "parm_valuei"}'

The following advanced parameters are supported:


"degree"
Optional. Is the degree of the polynomial. Low degree polynomials are
recommended. The default value is "1".
"interaction_only"
Optional. Controls the terms that are generated in the polynomial equation. The
default value is "no". Allowed values are:

"no", which generates the most general polynomial of degree degree based on the
predictor fields.

"yes", which uses only terms linear in each predictor X0, X1, . . ., and cross-terms
of the form X0*X1*X2 of at most degree predictors.
"train_ratio"
Optional. Is a value between 0 and 1 that specifies the fraction of data used for
training the model. The default value is "0.8".
"test_ratio"
Optional. Is a value between 0 and 1 that specifies the fraction of data used for
testing the model. The default value is "0.2".
"l2_grid"
Optional. Is a grid consisting of comma-separated positive numbers to be used as L2-
regularization strengths. The default value is "0,1,1,10". The optimal value is chosen
by cross-validation.

Using Functions 625


REGRESS_POLY: Polynomial Regression

"kfold"
Optional. Is the number of folds used for cross-validation. Suggested values are
integers between "2" and "10". The default value is "4".
predictor_field1, predictor_field2, [...,]
Numeric

Are at least two predictor field names.


target_field
Numeric

Is the target field.

Example: Using REGRESS_POLY to Predict Price


The following request uses REGRESS_POLY to compute the predicted price using a polynomial
regression of degree 4 and predictors height, horsepower, peak RPM, city MPG, and highway
MPG.
TABLE FILE imports85
PRINT price
COMPUTE predictedPrice/I5 = REGRESS_POLY('{"degree":"4"}',
height, horsepower, peakRpm,
cityMpg, highwayMpg, price);
WHERE price LT 30000
ON TABLE SET PAGE NOLEAD
ON TABLE SET STYLE *
GRID=OFF,$
ENDSTYLE
END

626
22. Machine Learning (Python-based) Functions

The partial output is shown in the following image.

Using Functions 627


REGRESS_RF: Random Forest Regression

REGRESS_RF: Random Forest Regression


REGRESS_RF creates a random forest, which is an ensemble of decision trees. Each decision
tree produces an independent regression prediction, and the prediction of the forest is the
average of the individual predictions.

Syntax: How to Calculate a Random Forest Regression

REGRESS_RF('options',
predictor_field1[, predictor_field2, ...] target_field)

where:
'options'
Is a dictionary of advanced parameters that control the model attributes, enclosed in
single quotation marks. Most of these parameters have a default value, so you can omit
them from the request, if you want to use the default values. Even with no advanced
parameters, the single quotation marks are required. The format of the advanced
parameter dictionary is:
'{"parm_name1": "parm_value1", ... ,"parm_namei": "parm_valuei"}'

The following advanced parameters are supported:


"trees"
Is the number of decision trees in the forest. Allowed values are integers greater than
10. The default value is "100".
"feature_importances"
Specifies whether to compute feature importances. Valid values are "yes" and "no".
The default value is "yes".
"train_ratio"
Is a value between 0 and 1 that specifies the fraction of data used for training the
model. The default value is "0.8".
"test_ratio"
Is a value between 0 and 1 that specifies the fraction of data used for testing the
model. The default value is "0.2".
"min_values_leaf_grid"
Is a grid of the minimum number of samples required in a node in order to split the
node, or a single value. The optimal value is chosen by cross-validation. The default
value is "1,3,5".

628
22. Machine Learning (Python-based) Functions

predictor_field1[, predictor_field2, ...]


Numeric or Alphanumeric

Are one or more predictor field names.


target_field
Numeric

Is the target field.

Example: Predicting Price Using REGRESS_RF


The following procedure uses REGRESS_RF to predict price, using a random forest with default
values for all of the advanced parameters, and with predictors number of doors, body style,
height, horsepower, peak RPM, city MPG, and highway MPG.
TABLE FILE imports85
PRINT price
COMPUTE predictedPrice/I5 = REGRESS_RF('',
numOfDoors, bodyStyle, height, horsepower,
peakRpm, cityMpg, highwayMpg, price);
WHERE price LT 30000
ON TABLE SET PAGE NOLEAD
ON TABLE SET STYLE *
GRID=OFF,$
ENDSTYLE
END

Using Functions 629


REGRESS_RF: Random Forest Regression

Partial output is shown in the following image.

630
22. Machine Learning (Python-based) Functions

REGRESS_XGB: Extreme Gradient Boosting Regression


REGRESS_XGB creates a series of decision trees, where each new tree attempts to improve
on the predictive capabilities of the prior trees. Each decision tree arrives at its prediction
based on a partitioning of the space spanned by the predictors, using a recursive succession
of binary splits. At each split, the values of one predictor are partitioned into two sets. The
splits are guided (choice of the split-predictor and the split-point) by the greedy maximization of
an objective function that involves measuring how much alike the y-values of the data points in
each partition are (called purity), as well as a regularization term. Early stopping is applied
when the predictive capability starts to deteriorate.

Syntax: How to Calculate an Extreme Gradient Boosting Regression

REGRESS_XGB('options',
predictor_field1[, predictor_field2, ...] target_field)

where:
'options'
Is a dictionary of advanced parameters that control the model attributes, enclosed in
single quotation marks. Most of these parameters have a default value, so you can omit
them from the request, if you want to use the default values. Even with no advanced
parameters, the single quotation marks are required. The format of the advanced
parameter dictionary is:

'{"parm_name1": "parm_value1", ... ,"parm_namei": "parm_valuei"}'

The following advanced parameters are supported:


"trees"
Is the number of decision trees in the forest. Allowed values are integers greater than
10. The default value is "300".
"train_ratio"
Is a value between 0 and 1 that specifies the fraction of data used for training the
model. The default value is "0.8".
"test_ratio"
Is a value between 0 and 1 that specifies the fraction of data used for testing the
model. The default value is "0.2".
"early_stopping_rounds"
Specifies the number of added trees such that if the algorithm has not improved its
performance in this many added trees, it stops. Allowed values are integers between 1
and the number of trees. The default value is "10".

Using Functions 631


REGRESS_XGB: Extreme Gradient Boosting Regression

"l2_grid"
Is a grid consisting of comma-separated positive numbers to be used as L2-
regularization strengths. The default value is "0,1,1,10". The optimal value is chosen
by cross-validation.
"kfold"
Is the number of folds used in the grid-search with cross-validation. Suggested values
are integers between "2" and "10". The default value is "4".
"max_depth_grid"
Is the maximum depth of each decision tree. A grid in the form "4,5,6" is allowed. The
default value is "5". The optimal value is chosen by cross-validation.
predictor_field1[, predictor_field2, ...]
Numeric or Alphanumeric

Are one or more predictor field names.


target_field
Numeric

Is the target field.

Example: Predicting Price Using REGRESS_XGB


The following procedure uses REGRESS_XGB to predict price, using 400 trees and default
values for the other advanced parameters, and with predictors number of doors, body style,
height, horsepower, peak RPM, city MPG, and highway MPG.
TABLE FILE imports85
PRINT price
COMPUTE predictedPrice/I5 =
REGRESS_XGB('{"trees":"400","early_stopping_rounds":"10",
"kfold":"4","test_ratio":"0.2"}',
numOfDoors, bodyStyle, height, horsepower,
peakRpm, cityMpg, highwayMpg, price);
WHERE price LT 30000
ON TABLE SET PAGE NOLEAD
ON TABLE SET STYLE *
GRID=OFF,$
ENDSTYLE
END

632
22. Machine Learning (Python-based) Functions

Partial output is shown in the following image.

Using Functions 633


RUN_MODEL and RUN_MODEL2: Running a Saved Python Model

RUN_MODEL and RUN_MODEL2: Running a Saved Python Model


Once you create a model using one of the machine learning functions by running it on a set of
training and test data, you can save the model and run it against new data. The type of data
source used to run the model can be any data source that WebFOCUS can read.

Use the RUN_MODEL function when the predictor fields in the new data source have the same
field names as the predictor fields used to generate the model. Use the RUN_MODEL2
function If the field names are different. The data types and lengths must be the same in
either case.

Syntax: How to Save a Python-Based Model

ON TABLE HOLD FORMAT PYTHON MODEL fieldname AS app/modelname

where:
fieldname
Is the fieldname in the COMPUTE command that returned the value from the machine
learning function.
app
Is the application folder in which to save the model.
modelname
Is the name of the saved model.

Example: Saving a Python Model


The following request uses the REGRESS_RF function to return a field named predictedPrice
and save it as a model named model5 in application folder app1/mymodels.
TABLE FILE imports85
PRINT price
COMPUTE predictedPrice/I5 =
REGRESS_RF('{"trees":"123","test_ratio":"0.20","train_ratio":"0.80"}',
make, numOfDoors, bodyStyle, height, horsepower,
peakRpm, cityMpg, highwayMpg, price);
WHERE OUTPUTLIMIT EQ 1
ON TABLE HOLD FORMAT PYTHON MODEL predictedPrice
AS app1/mymodels/model5
END

Syntax: How to Run a Saved Python Model


Use the RUN_MODEL function if the predictor field names are the same in the model and the
data.
COMPUTE fieldname/fmt = RUN_MODEL('app/modelname');

634
22. Machine Learning (Python-based) Functions

Use the RUN_MODEL2 function if the predictor field names are not the same in the model and
the data.
COMPUTE fieldname/fmt = RUN_MODEL2('app/modelname',
pfield1, ..., pfieldn);

where:
fieldname
Is the name of the field that will contain the returned predictions from the model.
app/modelname'
Is the application folder and saved model name.
pfield1, ..., pfieldn
Are the field names in the data used for running the model that match the predictor fields
used to generate the model.

Example: Running a Model Using RUN_MODEL


In the following request, the data source in the request has the same field names that were
used to create the model.
TABLE FILE imports85
PRINT price COMPUTE predictedPriceFromSaved/I5 =
RUN_MODEL('app1/mymodels/model5');
ON TABLE SET PAGE NOLEAD
ON TABLE SET STYLE *
GRID=OFF,$
ENDSTYLE
END

Using Functions 635


RUN_MODEL and RUN_MODEL2: Running a Saved Python Model

Partial output is shown in the following image.

Example: Running a Model Using RUN_MODEL2


In the following version of the request, the data source in the request is assumed to have field
names that are different from the ones that were used to create the model.
TABLE FILE imports2
PRINT price COMPUTE predictedPriceFromSaved/I5 =
RUN_MODEL2('app1/mymodels/model5',
type, doors, Style, ht, hpower, Rpm, CMpg, HMpg);
ON TABLE SET PAGE NOLEAD
ON TABLE SET STYLE *
GRID=OFF,$
ENDSTYLE
END

636
22. Machine Learning (Python-based) Functions

Partial output is shown in the following image.

Using Functions 637


RUN_MODEL and RUN_MODEL2: Running a Saved Python Model

638
Chapter 23 Simplified System Functions
Simplified system functions have streamlined parameter lists, similar to those used by
SQL functions. In some cases, these simplified functions provide slightly different
functionality than previous versions of similar functions.

The simplified functions do not have an output argument. Each function returns a value
that has a specific data type.

When used in a request against a relational data source, these functions are optimized
(passed to the RDBMS for processing).
In this chapter:

EDAPRINT: Inserting a Custom Message in the EDAPRINT Log File

ENCRYPT: Encrypting a Password

GETENV: Retrieving the Value of an Environment Variable

PUTENV: Assigning a Value to an Environment Variable

SLACK: Posting a Message to a Slack Channel

EDAPRINT: Inserting a Custom Message in the EDAPRINT Log File


The EDAPRINT function enables you to add a text message into the EDAPRINT log file and
assign it a message type. The returned value of the function is zero (0).

Syntax: How to Insert a Message in the EDAPRINT Log File

EDAPRINT(message_type, 'message')

where:
message_type
Keyword

Can be one of the following message types.

I. Informational message.

W. Warning message.

Using Functions 639


ENCRYPT: Encrypting a Password

E. Error message.
'message'
Is the message to insert, enclosed in single quotation marks.

Example: Inserting a Custom Message in the EDAPRINT Log File


The following procedure inserts three messages in the EDAPRINT log file.
-SET &I = EDAPRINT(I, 'This is a test informational message');
-SET &W = EDAPRINT(W, 'This is a test warning message');
-SET &E = EDAPRINT(E, 'This is a test error message');

The output is shown in the following image.

ENCRYPT: Encrypting a Password


The ENCRYPT function encrypts an alphanumeric input value using the encryption algorithm
configured in the server. The result is returned as variable length alphanumeric.

Syntax: How to Encrypt a Password

ENCRYPT(password)

where:
password
Fixed length alphanumeric

Is the value to be encrypted.

640
23. Simplified System Functions

Example: Encrypting a Password


The following request encrypts the value guestpassword using the encryption algorithm
configured in the server.

-SET &P1 = ENCRYPT('guestpassword');


-TYPE &P1

The returned encrypted value is {AES}963AFA754E1763ABE697E8C5E764115E.

GETENV: Retrieving the Value of an Environment Variable


The GETENV function takes the name of an environment variable and returns its value as a
variable length alphanumeric value.

Syntax: How to Retrieve the Value of an Environment Variable

GETENV(var_name)

where:
var_name
fixed length alphanumeric

Is the name of the environment variable whose value is being retrieved.

Example: Retrieving the Value of an Environment Variable


The following request retrieves the value of the server variable EDAEXTSEC.

-SET &E1 = GETENV('EDAEXTSEC');


-TYPE &E1

The value returned is ON if the server was started with security on or OFF if the server was
started with security off.

PUTENV: Assigning a Value to an Environment Variable


The PUTENV function assigns a value to an environment variable. The function returns an
integer return code whose value is 1 (one) if the assignment is not successful or 0 (zero) if it is
successful.

Syntax: How to Assign a Value to an Environment Variable

PUTENV(var_name, var_value)

Using Functions 641


SLACK: Posting a Message to a Slack Channel

where:
var_name
Fixed length alphanumeric

Is the name of the environment variable to be set.


var_value
Alphanumeric

Is the value you want to assign to the variable.

Example: Assigning a Value to the UNIX PS1 Variable


The following request assigns the value FOCUS/Shell: to the UNIX PS1 variable.
-SET &P1 = PUTENV('PS1','FOCUS/Shell:');

This causes UNIX to display the following prompt when the user issues the UNIX shell
command SH:
FOCUS/Shell:

The following request creates a variable named xxxx and sets it to the value this is a test. It
then retrieves the value using GETENV.
-SET &XXXX=PUTENV(xxxx,'this is a test');
-SET &YYYY=GETENV(xxxx);
-TYPE Return Code: &XXXX, Variable value: &YYYY

The output is:


Return Code: 0, Variable value: this is a test

SLACK: Posting a Message to a Slack Channel


SLACK posts a message to a Slack channel from a WebFOCUS procedure:

If the message is sent successfully, the function returns the value true.

If the message is not sent successfully, the function returns a blank.

Syntax: How to Post a Message to a Slack Channel

SLACK(workspace, channel, message)

where:
workspace
Is a Workspace name.

642
23. Simplified System Functions

channel
Is a Channel name.
message
Is an alphanumeric field containing the message.

Example: Sending a Slack Message From a WebFOCUS Request


The Adapter for Slack has been configured to have a connection to the devibi workspace, as
shown in the following image.

Using Functions 643


SLACK: Posting a Message to a Slack Channel

The following request sends a Slack message to the general channel of the devibi Workspace,
when the department is MIS.
TABLE FILE ibisamp/EMPLOYEE
SUM
CURR_SAL
AND COMPUTE SLACK_MESSAGE/A200 = 'Salary for Department ' | DEPARTMENT ||
' is ' | LJUST(20, FPRINT(CURR_SAL,'D12.2M'), 'A20');
AND COMPUTE CURR_SAL_SLACK/A20=IF DEPARTMENT EQ 'MIS'
THEN SLACK('devibi', 'general', SLACK_MESSAGE) ELSE 'false';
AS 'Message Sent,to Slack highlighting,Salary'
BY DEPARTMENT
HEADING
"Slack"
"Slack Function Example"
ON TABLE SET PAGE-NUM NOLEAD
ON TABLE NOTOTAL
ON TABLE SET STYLE *
INCLUDE=IBFS:/FILE/IBI_HTML_DIR/javaassist/intl/EN/ENIADefault_combine.sty,
$
ENDSTYLE
END

The output is shown in the following image.

The message in the Slack channel is shown in the following image.

644
Chapter 24 System Functions
System functions call the operating system to obtain information about the operating
environment or to use a system service.

For many functions, the output argument can be supplied either as a field name or as a
format enclosed in single quotation marks. However, if a function is called from a
Dialogue Manager command, this argument must always be supplied as a format, and if
a function is called from a Maintain Data procedure, this argument must always be
supplied as a field name. For detailed information about calling a function and supplying
arguments, see Accessing and Calling a Function on page 59.
In this chapter:

CHECKPRIVS: Retrieving the Privilege GETUSER: Retrieving a User ID


State for the Connected User
GRPLIST: Retrieving the Group List of the
CLSDDREC: Closing All Files Opened by Connected User
the PUTDDREC Function
JOBNAME: Retrieving the Current
FEXERR: Retrieving an Error Message Process Identification String

FGETENV: Retrieving the Value of an MVSDYNAM: Passing a DYNAM


Environment Variable Command to the Command Processor

FINDMEM: Finding a Member of a PUTCOOKI: Submitting a Value to a


Partitioned Data Set Browser Cookie

FPUTENV: Assigning a Value to an PUTDDREC: Writing a Character String as


Environment Variable a Record in a Sequential File

GETCOOKI: Retrieving a Browser Cookie SLEEP: Suspending Execution for a


Value Given Number of Seconds

GETHEADR: Retrieving an HTTP Header SPAWN: Creating a Subprocess From a


Variable Procedure

GETPDS: Determining If a Member of a SYSTEM: Calling a System Program


Partitioned Data Set Exists
SYSVAR: Retrieving the Value of a z/OS
System Variable

Using Functions 645


CHECKPRIVS: Retrieving the Privilege State for the Connected User

CHECKPRIVS: Retrieving the Privilege State for the Connected User


Given a privilege code, CHECKPRIVS returns the value Y, if the connected user has that
privilege, or N if the user does not have the privilege or the privilege does not exist.

Note: You can see your list of general privileges by clicking the Console (C) button at the top
left corner of the window and selecting My Console/Show My General Privileges. A user with
Server Administrator privileges can also see the list of general privileges on the Access Control
page by right-clicking a user ID, selecting Properties from the context menu and clicking the
General Privileges tab on the Properties page.

Syntax: How to Retrieve the Privilege State for the Connected User

CHECKPRIVS(privcode, output)

where:
privcode
Is the privilege code for which to retrieve the status.
output
Alphanumeric

Is the name of the field that contains the result, or the format of the output value enclosed
in single quotation marks.

Example: Retrieving the Privilege State for the Connected User


The following request retrieves the privilege state for privilege ADPTP (Configure Data Adapter):

-SET &PRIVSTATE = CHECKPRIVS(ADPTP,'A1');


-TYPE Privilege State is: &PRIVSTATE

The output is:

Privilege State is: Y

CLSDDREC: Closing All Files Opened by the PUTDDREC Function


The CLSDDREC function closes all files opened by the PUTDDREC function. If PUTDDREC is
called in a Dialogue Manager -SET command, the files opened by PUTDDREC are not closed
automatically until the end of a request or connection. In this case, you can close the files and
free the memory used to store information about open file by calling the CLSDDREC function.

646
24. System Functions

Syntax: How to Close All Files Opened by the PUTDDREC Function

CLSDDREC(output)

where:
output
Integer

Is the return code, which can be one of the following values:

0, which indicates that the files are closed.

1, which indicates an error while closing the files.

Example: Closing Files Opened by the PUTDDREC Function


This example closes files opened by the PUTDDREC function:
CLSDDREC('I1')

FEXERR: Retrieving an Error Message


Available Languages: reporting, Maintain

The FEXERR function retrieves an Information Builders error message. It is especially useful in
a procedure using a command that suppresses the display of output messages.

An error message consists of up to four lines of text. The first line contains the message and
the remaining three contain a detailed explanation, if one exists. FEXERR retrieves the first line
of the error message.

Syntax: How to Retrieve an Error Message

FEXERR(error, 'A72')

where:
error
Numeric

Is the error number, up to 5 digits long.

Using Functions 647


FGETENV: Retrieving the Value of an Environment Variable

'A72'

Is the format of the output value enclosed in single quotation marks. The format is A72,
the maximum length of an Information Builders error message.

Example: Retrieving an Error Message


FEXERR retrieves the error message whose number is contained in the &ERR variable, in this
case 650. The result is returned to the variable &&MSGVAR and has the format A72.
-SET &ERR = 650;
-SET &&MSGVAR = FEXERR(&ERR, 'A72');
-TYPE &&MSGVAR

The output is:


(FOC650) THE DISK IS NOT ACCESSED

FGETENV: Retrieving the Value of an Environment Variable


Available Languages: reporting

The FGETENV function retrieves the value of an environment variable and returns it as an
alphanumeric string.

Syntax: How to Retrieve the Value of an Environment Variable

FGETENV(length, 'varname', outlen, output)

where:
length
Integer

Is the number of characters in the environment variable name.


varname
Alphanumeric

Is the name of the environment variable whose value is being retrieved.


outlen
Integer

Is the length of the environment variable value returned or a field in which the environment
variable value is stored.

648
24. System Functions

output
Alphanumeric

Is the format of the field in which the environment variable's value is stored.

FINDMEM: Finding a Member of a Partitioned Data Set


Available Languages: reporting, Maintain

The FINDMEM function, available only on z/OS, determines if a specific member of a


partitioned data set (PDS) exists. This function is used primarily in Dialogue Manager
procedures.

To use this function, allocate the PDS to a ddname because the ddname is required in the
function call. You can search multiple PDSs with one function call if they are concatenated to
one ddname.

Syntax: How to Find a Member of a Partitioned Data Set

FINDMEM(ddname, member, output)

where:
ddname
A8

Is the ddname to which the PDS is allocated. This value must be an eight-character literal
enclosed in single quotation marks, or a variable that contains the ddname. If you supply a
literal less than eight characters long, pad it with trailing spaces.
member
A8

Is the member for which you are searching. This value must be eight characters long. If you
supply a literal that has less than eight characters, pad it with trailing spaces.
output
A1

Using Functions 649


FPUTENV: Assigning a Value to an Environment Variable

Is the name of the field that contains the result, or the format of the output value enclosed
in single quotation marks. The result is one of the following:

Y indicates the member exists in the PDS.

N indicates the member does not exist in the PDS.

E indicates an error occurred. Either the data set is not allocated to the ddname, or the
data set allocated to the ddname is not a PDS (and may be a sequential file).

Example: Finding a Member of a Partitioned Data Set


FINDMEM searches for the EMPLOYEE Master File in the PDS allocated to ddname MASTER,
and returns the result to the variable &FINDCODE. The result has the format A1:

-SET &FINDCODE = FINDMEM('MASTER ', 'EMPLOYEE', 'A1');-IF &FINDCODE EQ 'N'


GOTO NOMEM;
-IF &FINDCODE EQ 'E' GOTO NOPDS;
-TYPE MEMBER EXISTS, RETURN CODE = &FINDCODE
TABLE FILE EMPLOYEE
PRINT CURR_SAL BY LAST_NAME BY FIRST_NAME
WHERE RECORDLIMIT EQ 4;
END
-EXIT
-NOMEM
-TYPE EMPLOYEE NOT FOUND IN MASTER FILE PDS
-EXIT
-NOPDS
-TYPE ERROR OCCURRED IN SEARCH
-TYPE CHECK IF FILE IS A PDS ALLOCATED TO DDNAME MASTER
-EXIT

The output is:

LAST_NAME FIRST_NAME CURR_SAL


-------- ---------- --------
JONES DIANE $18,480.00
SMITH MARY $13,200.00
STEVENS RICHARD $9,500.00
ALFRED $11,000.00

FPUTENV: Assigning a Value to an Environment Variable


Available Operating Systems: IBM i (formerly referred to as i5/OS), Tandem, UNIX, Windows

Available Languages: reporting

The FPUTENV function assigns a character string to an environment variable. Use FPUTENV to
set values that are used elsewhere in the system.

650
24. System Functions

Limit: You cannot use FPUTENV to set or change FOCPRINT, FOCPATH, or USERPATH. Once
started, these variables are held in memory and not reread from the environment.

Syntax: How to Assign a Value to an Environment Variable

FPUTENV (varname_length,'varname',value_length, 'value', output)

where:
varname_length
Integer

Is the maximum number of characters in the name of the environment variable.


varname
Alphanumeric

Is the name of the environment variable enclosed in single quotation marks. The name
must be right-justified and padded with blanks to the maximum length specified by
varname_length.
value_length
Is the maximum length of the environment variable value.

Note: The sum of varname_length and value_length cannot exceed 64.


value
Alphanumeric

Is the value you wish to assign to the environment variable. The string must be right-
justified and contain no embedded blanks. Strings that contain embedded blanks are
truncated at the first blank.
output
Integer

Is the return code. It can be the name of the field that contains the result, or the format of
the output value enclosed in single quotation marks. If the variable is set successfully, the
return code is 0. Any other value indicates a failure occurred.

Example: Assigning a Value to an Environment Variable


FPUTENV assigns the value FOCUS/Shell to the PS1 variable and stores it in a field with the
format A12:
-SET &RC = FPUTENV(3,'PS1', 12, 'FOCUS/Shell:', 'I4');

Using Functions 651


GETCOOKI: Retrieving a Browser Cookie Value

The request displays the following prompt when the user issues the UNIX shell command SH:
FOCUS/Shell:

GETCOOKI: Retrieving a Browser Cookie Value


Security credentials can come from many sources and be provided in several different formats.
Some security credentials from third-party single sign-on products are passed in the form of a
browser cookie. The Reporting Server can use the GETCOOKI function to retrieve the value of a
browser cookie passed to it by the client.

Syntax: How to Retrieve a Cookie Value

GETCOOKI('cookie_name', length)

where:
cookie_name
Alphanumeric

Is the name of the browser cookie whose value is being retrieved. The maximum length of
the cookie name is 80 bytes. If the cookie is not set or its name exceeds 80 characters,
the function will return Invalid Cookie Name.
length
Alphanumeric (An)

Is the length of the cookie. It can be the name of the field that contains the result, or the
format of the output value enclosed in single quotation marks. If the length n specified is
greater than the actual length of the retrieved cookie, the result will be padded with
blanks. It is always recommended to use the function TRUNCATE(arg1) on return from
GETCOOKI to remove extra trailing blanks.

Example: Retrieving the Value of a Browser Cookie


The following function call retrieves the value of the ObSSOCookie created by Oracle Access
manager (formerly Oblix):
GETCOOKI('ObSSOCookie', 'A400')

652
24. System Functions

GETHEADR: Retrieving an HTTP Header Variable


The HTTP header contains variables whose values describe the Web Server environment and
can specify credentials coming from the Web Server or a third-party single sign-on product. The
Reporting Server can use the GETHEADR function to retrieve the value of an HTTP Header
variable from an HTTP header passed to it by the client.

Syntax: How to Retrieve an HTTP Header Variable

GETHEADR('varname', output)

where:
varname
Alphanumeric

Is the name of the HTTP header variable whose value is being retrieved.
output
Alphanumeric

Is the name of the field that contains the result, or the format of the output value enclosed
in single quotation marks.

Example: Retrieving the Value of an HTTP Header Variable


The following is a sample HTTP header:

The following function call retrieves the value application/x-www-form-urlencoded from the HTTP
Header:
GETHEADR('content-type', 'A150')

Using Functions 653


GETPDS: Determining If a Member of a Partitioned Data Set Exists

The following function call retrieves the value en-us from the HTTP Header:
GETHEADR('accept-language', 'A10')

GETPDS: Determining If a Member of a Partitioned Data Set Exists


Available Operating Systems: z/OS

Available Languages: reporting, Maintain

The GETPDS function determines if a specific member of a partitioned data set (PDS) exists,
and if it does, returns the PDS name. This function is used primarily in Dialogue Manager
procedures.

To use this function, allocate the PDS to a ddname because the ddname is required in the
function call. You can search multiple PDSs with one function call if they are concatenated to
one ddname.

GETPDS is almost identical to FINDMEM, except that GETPDS provides either the PDS name or
returns a different set of status codes.

Syntax: How to Determine If a PDS Member Exists

GETPDS(ddname, member, output)

where:
ddname
A8

Is the ddname to which the PDS is allocated. This value must be an eight-character literal
enclosed in single quotation marks, or a variable that contains the ddname. If you supply a
literal less than eight characters long, pad it with trailing spaces.
member
A8

Is the member for which the function searches. This value must be eight characters long. If
you supply a literal with less than eight characters, pad it with trailing spaces.
output
A44

654
24. System Functions

Is the name of the field that contains the result, or the format of the output value enclosed
in single quotation marks. The maximum length of a PDS name is 44. The result is one of
the following:

PDS name is the name of the PDS that contains the member, if it exists.

*D indicates the ddname is not allocated to a data set.

*M indicates the member does not exist in the PDS.

*E indicates an error occurred. For example, the data set allocated to the ddname is not a
PDS (and may be a sequential file).

Example: Determining If a PDS Member Exists


GETPDS searches for the member specified by &MEMBER in the PDS allocated to &DDNAME,
and returns the result to &PNAME. The result has the format A44.

-SET &DDNAME = 'MASTER ';


-SET &MEMBER = 'EMPLOYEE';
-SET &PNAME = ' ';
-SET &PNAME = GETPDS(&DDNAME, &MEMBER, 'A44');
-IF &PNAME EQ '*D' THEN GOTO DDNOAL;
-IF &PNAME EQ '*M' THEN GOTO MEMNOF;
-IF &PNAME EQ '*E' THEN GOTO DDERROR;
-*
-TYPE MEMBER &MEMBER IS FOUND IN
-TYPE THE PDS &PNAME
-TYPE ALLOCATED TO &DDNAME
-*
-EXIT
-DDNOAL
-*
-TYPE DDNAME &DDNAME NOT ALLOCATED
-*
-EXIT
-MEMNOF
-*
-TYPE MEMBER &MEMBER NOT FOUND UNDER DDNAME &DDNAME
-*
-EXIT
-DDERROR
-*
-TYPE ERROR IN GETPDS; DATA SET PROBABLY NOT A PDS.
-*
-EXIT

The output is similar to the following:


MEMBER EMPLOYEE IS FOUND IN
THE PDS USER1.MASTER.DATA
ALLOCATED TO MASTER

Using Functions 655


GETUSER: Retrieving a User ID

Example: Displaying the Attributes of a PDS


To view the attributes of the PDS that contains a specific member, this Dialogue Manager
procedure can search for the EMPLOYEE member in the PDS allocated to the ddname MASTER
and, based on its existence, allocate the PDS to the ddname TEMPMAST. Dialogue Manager
system variables are used to display the attributes.

-SET &DDNAME = 'MASTER ';


-SET &MEMBER = 'EMPLOYEE';
-SET &PNAME = ' ';
-SET &PNAME = GETPDS(&DDNAME, &MEMBER, 'A44');
-IF &PNAME EQ '*D' OR '*M' OR '*E' THEN GOTO DDERROR;
-*
DYNAM ALLOC FILE TEMPMAST DA -
&PNAME SHR
-RUN
-? MVS DDNAME TEMPMAST
-TYPE The data set attributes include:
-TYPE Data set name is: &DSNAME
-TYPE Volume is: &VOLSER
-TYPE Disposition is: &DISP
-EXIT
-*
-DDERROR
-TYPE Error in GETPDS; Check allocation for &DDNAME for
-TYPE proper allocation.
-*
-EXIT

The sample output is:


THE DATA SET ATTRIBUTES INCLUDE:
DATA SET NAME IS: USER1.MASTER.DATA
VOLUME IS: USERM0
DISPOSITION IS: SHR

GETUSER: Retrieving a User ID


Available Languages: reporting, Maintain

The GETUSER function retrieves the ID of the connected user.

Syntax: How to Retrieve a User ID

GETUSER(output)

where:
output
Alphanumeric, at least A8

656
24. System Functions

Is the result field, whose length depends on the platform on which the function is issued.
Provide a length as long as required for your platform; otherwise the output will be
truncated.

Example: Retrieving a User ID


GETUSER retrieves the user ID of the person running the request:

DEFINE FILE EMPLOYEE


USERID/A8 WITH EMP_ID = GETUSER(USERID);
END

TABLE FILE EMPLOYEE


SUM CURR_SAL AS 'TOTAL SALARIES'
BY DEPARTMENT
HEADING
"SALARY REPORT RUN FROM USERID: <USERID"
" "
END

The output is:

SALARY REPORT RUN FROM USERID: doccar


DEPARTMENT TOTAL SALARIES
---------- --------------
MIS $108,002.00
PRODUCTION $114,282.00

GRPLIST: Retrieving the Group List of the Connected User


Available Languages: reporting

GRPLIST returns a group name or a list of group names (separated by colons) for the
connected user. This function is supported for LDAP security with all types of connections.

If the group list is empty or there is an error in the function parameters, the function returns
blanks.

Syntax: How to Retrieve a List of Group Memberships for the Connected User

GRPLIST(outputLength, outformat)

where:
outputLength
Is the length of the output string.

Using Functions 657


JOBNAME: Retrieving the Current Process Identification String

outformat
Is the format of the output string enclosed in single quotation marks (').

Example: Retrieving the Group List for the Connected User


The following request retrieves the group list for the connected user into a Dialogue Manager
variable named &LIST:
-SET &LIST = GRPLIST(300, 'A300');
-TYPE &LIST

The output is:


#All_Technical_Staff;#CTSS_ADV;#CTSS_ADV;#CTSS_ADVT;#DSEDA

Issuing the same request for user pgmuser1 shows that this user belongs to a single group:
pgmgrp1

JOBNAME: Retrieving the Current Process Identification String

The JOBNAME function retrieves the raw identification string of the current process from the
operating system. This is also commonly known as a process PID at the operating system
level. The function is valid in all environments, but is typically used in Dialogue Manager and
returns the value as an alphanumeric string (even though a PID is pure numeric on some
operating systems).

Note: JOBNAME strings differ between some operating systems in terms of look and length.
For example, Windows, UNIX, and z/OS job names are pure numeric (typically a maximum of 8
characters long), while an IBM i job name is a three-part string that has a 26 character
maximum length. Since an application may eventually be run in another (unexpected)
environment in the future, it is good practice to use the maximum length of 26 to avoid
accidental length truncation in the future. Applications using this function for anything more
than simple identification may also need to account for the difference in the application code.

Syntax: How to Retrieve the Current Process Identification String

JOBNAME(length, output)

where:
length
Integer

Is the maximum number of characters to return from the PID system call.

658
24. System Functions

output
Alphanumeric

Is the returned process identification string, whose length depends on the platform on
which the function is issued. Provide a length as long as required for your platform.
Otherwise, the output will be truncated.

Example: Retrieving a Process Identification String


The following example uses the JOBNAME function to retrieve the current process identification
string to an A26 string and then truncate it for use in a -TYPE statement:

-SET &JOBNAME = JOBNAME(26, 'A26');


-SET &JOBNAME = TRUNCATE(&JOBNAME);
-TYPE The Current system PID &JOBNAME is processing.

For example, on Windows, the output is similar to the following:

The Current system PID 2536 is processing.

MVSDYNAM: Passing a DYNAM Command to the Command Processor


Available Operating Systems: z/OS

Available Languages: reporting, Maintain

The MVSDYNAM function transfers a FOCUS DYNAM command to the DYNAM command
processor. It is useful in passing allocation commands to the processor in a compiled MODIFY
procedure after the CASE AT START command.

Syntax: How to Pass a DYNAM Command to the Command Processor

MVSDYNAM(command, length, outfield)

where:
command
Alphanumeric

Is the DYNAM command enclosed in single quotation marks, or a field or variable that
contains the command. The function converts lowercase input to uppercase.
length
Numeric

Is the maximum length of the command, in characters, between 1 and 256.

Using Functions 659


PUTCOOKI: Submitting a Value to a Browser Cookie

outfield
I4

Is the field that contains the result, or the format of the output value enclosed in single
quotation marks.

MVSDYNAM returns one of the following codes:

0 indicates the DYNAM command transferred and executed successfully.

positive number is the error number corresponding to a FOCUS error.

negative number is the FOCUS error number corresponding to a DYNAM failure.

In Dialogue Manager, you must specify the format.

PUTCOOKI: Submitting a Value to a Browser Cookie


The PUTCOOKI function allows you to submit a value to the cookie to be used by a third-party
single sign-on software product for connection to the server or an adapter. In order to retrieve
an appropriate cookie value, you must have an existing exit that calls an external procedure
which obtains the value of a single sign-on browser cookie. This feature was initially developed
for the MYSAPSSO2 cookie for the SAP RFC connection.

Syntax: How to Submit a Cookie Value

PUTCOOKI('cookie_name', 'cookie_value')

where:
cookie_name
Alphanumeric

Is the name of the browser cookie whose value is being set.


cookie_value
Alphanumeric

Is the value to submit for the cookie. It can be the name of the field that contains the
result, or the format of the output value enclosed in single quotation marks

660
24. System Functions

Example: Submitting a Value to a Browser Cookie


The following function call submits the value created by an external custom exit or set by SAP
EP configured for SSO with SAP Logon Tickets:

SET &COOKIE_VALUE=CUSTOM_EXIT();
PUTCOOKI('MYSAPSSO2', &COOKIE_VALUE )

PUTDDREC: Writing a Character String as a Record in a Sequential File


The PUTDDREC function writes a character string as a record in a sequential file. The file must
be identified with a FILEDEF (DYNAM on z/OS) command. If the file is defined as an existing
file (with the APPEND option), the new record is appended. If the file is defined as NEW and it
already exists, the new record overwrites the existing file.

PUTDDREC opens the file if it is not already open. Each call to PUTDDREC can use the same
file or a new one. All of the files opened by PUTDDREC remain open until the end of a request
or connection. At the end of the request or connection, all files opened by PUTDDREC are
automatically closed.

The open, close, and write operations are handled by the operating system. Therefore, the
requirements for writing to the file and the results of deviating from the instructions when
calling PUTDDREC are specific to your operating environment. Make sure you are familiar
with and follow the guidelines for your operating system when performing input/output
operations.

You can call PUTDDREC in a DEFINE FILE command or in a DEFINE in the Master File.
However, PUTDDREC does not open the file until its field name is referenced in a request.

If PUTDDREC is called in a Dialogue Manager -SET command, the files opened by PUTDDREC
are not closed automatically until the end of a request or connection. In this case, you can
close the files and free the memory used to store information about open file by calling the
CLSDDREC function.

Syntax: How to Write a Character String as a Record in a Sequential File

PUTDDREC(ddname, dd_len, record_string, record_len, output)

where:
ddname
Alphanumeric

Is the logical name assigned to the sequential file in a FILEDEF command.

Using Functions 661


PUTDDREC: Writing a Character String as a Record in a Sequential File

dd_len
Numeric

Is the number of characters in the logical name.


record_string
Alphanumeric

Is the character string to be added as the new record in the sequential file.
record_len
Numeric

Is the number of characters to add as the new record.

It cannot be larger than the number of characters in record_string. To write all of


record_string to the file, record_len should equal the number of characters in record_string
and should not exceed the record length declared in the FILEDEF command. If record_len is
shorter than the declared length declared, the resulting file may contain extraneous
characters at the end of each record. If record_string is longer than the declared length,
record_string may be truncated in the resulting file.
output
Integer

Is the return code, which can have one of the following values:

0 - Record is added.
-1 - FILEDEF statement is not found.
-2 - Error while opening the file.
-3 - Error while adding the record to the file.

662
24. System Functions

Example: Calling PUTDDREC in a TABLE Request


The following example defines a new file whose logical name is PUTDD1. The TABLE request
then calls PUTDDREC for each employee in the EMPLOYEE data source and writes a record to
the file composed of the employee's last name, first name, employee ID, current job code, and
current salary (converted to alphanumeric using the EDIT function). The return code of zero (in
OUT1) indicates that the calls to PUTDDREC were successful:
FILEDEF PUTDD1 DISK putdd1.datTABLE FILE EMPLOYEE
PRINT EMP_ID CURR_JOBCODE AS 'JOB' CURR_SAL
COMPUTE SALA/A12 = EDIT(CURR_SAL); NOPRINT
COMPUTE EMP1/A50= LAST_NAME|FIRST_NAME|EMP_ID|CURR_JOBCODE|SALA;
NOPRINT
COMPUTE OUT1/I1 = PUTDDREC('PUTDD1',6, EMP1, 50, OUT1);
BY LAST_NAME BY FIRST_NAME
END

The output is:


LAST_NAME FIRST_NAME EMP_ID JOB CURR_SAL OUT1
--------- ---------- --------- --- -------- ----
BANNING JOHN 119329144 A17 $29,700.00 0
BLACKWOOD ROSEMARIE 326179357 B04 $21,780.00 0
CROSS BARBARA 818692173 A17 $27,062.00 0
GREENSPAN MARY 543729165 A07 $9,000.00 0
IRVING JOAN 123764317 A15 $26,862.00 0
JONES DIANE 117593129 B03 $18,480.00 0
MCCOY JOHN 219984371 B02 $18,480.00 0
MCKNIGHT ROGER 451123478 B02 $16,100.00 0
ROMANS ANTHONY 126724188 B04 $21,120.00 0
SMITH MARY 112847612 B14 $13,200.00 0
RICHARD 119265415 A01 $9,500.00 0
STEVENS ALFRED 071382660 A07 $11,000.00 0
After running this request, the sequential file contains the following records:
BANNING JOHN 119329144A17000000029700
BLACKWOOD ROSEMARIE 326179357B04000000021780
CROSS BARBARA 818692173A17000000027062
GREENSPAN MARY 543729165A07000000009000
IRVING JOAN 123764317A15000000026862
JONES DIANE 117593129B03000000018480
MCCOY JOHN 219984371B02000000018480
MCKNIGHT ROGER 451123478B02000000016100
ROMANS ANTHONY 126724188B04000000021120
SMITH MARY 112847612B14000000013200
SMITH RICHARD 119265415A01000000009500
STEVENS ALFRED 071382660A07000000011000

Using Functions 663


SLEEP: Suspending Execution for a Given Number of Seconds

Example: Calling PUTDDREC and CLSDDREC in Dialogue Manager -SET Commands


The following example defines a new file whose logical name is PUTDD1. The first -SET
command creates a record to add to this file. The second -SET command calls PUTDDREC to
add the record. The last -SET command calls CLSDDREC to close the file. The return codes are
displayed to make sure operations were successful:
FILEDEF PUTDD1 DISK putdd1.dat -SET &EMP1 = 'SMITH'|'MARY'|'A07'|'27000';
-TYPE DATA = &EMP1
-SET &OUT1 = PUTDDREC('PUTDD1',6, &EMP1, 17, 'I1');
-TYPE PUT RESULT = &OUT1
-SET &OUT1 = CLSDDREC('I1');
-TYPE CLOSE RESULT = &OUT1

The output is:


DATA = SMITHMARYA0727000
PUT RESULT = 0
CLOSE RESULT = 0
After running this procedure, the sequential file contains the following record:
SMITHMARYA0727000

SLEEP: Suspending Execution for a Given Number of Seconds


Available Languages: reporting, Maintain

The SLEEP function suspends execution for the number of seconds you specify as its input
argument.

This function is most useful in Dialogue Manager when you need to wait to start a specific
procedure. For example, you can start a FOCUS Database Server and wait until the server is
started before initiating a client application.

Syntax: How to Suspend Execution for a Specified Number of Seconds

SLEEP(delay, output);

where:
delay
Numeric

Is the number of seconds to delay execution. The number can be specified down to the
millisecond.
output
Numeric

664
24. System Functions

Is the name of a field or a format enclosed in single quotation marks. The value returned
is the same value you specify for delay.

Example: Suspending Execution for Four Seconds


The following example computes the current date and time, suspends execution for 4 seconds,
and computes the current date and time after the delay:

TABLE FILE VIDEOTRK


PRINT TRANSDATE NOPRINT
COMPUTE
START_TIME/HYYMDSa = HGETC(8, START_TIME);
DELAY/I2 = SLEEP(4.0, 'I2');
END_TIME/HYYMDSa = HGETC(8, END_TIME);
IF RECORDLIMIT EQ 1
END

The output is:

START_TIME DELAY END_TIME


---------- ----- --------
2007/10/26 5:04:36pm 4 2007/10/26 5:04:40pm

SPAWN: Creating a Subprocess From a Procedure


Available Operating Systems: UNIX

Available Languages: reporting

The SPAWN function creates a subprocess from a procedure that executes a system command
without terminating the current procedure.

Limit: On UNIX, you can invoke this function only from a COMPUTE or DEFINE command.

Syntax: How to Create a Subprocess From a Procedure

SPAWN(inlength, command, outcode)

where:
inlength
Numeric

Is the length, in characters, of command.


command
Alphanumeric

Is a UNIX command, or a .COM file you want to execute.

Using Functions 665


SYSTEM: Calling a System Program

outcode
D

Is the return code specifying whether or not the spawn was successful, or the format of
the output value enclosed in single quotation marks. Zero indicates a successful spawn; a
non-zero value indicates an unsuccessful spawn.

Example: Creating a Subprocess From a Procedure on UNIX


SPAWN executes the echo command, which displays Hello at the beginning of the procedure:
MODIFY FILE EMPLOYEE
PROMPT EMP_ID
MATCH EMP_ID
ON MATCH DELETE
CASE AT START
COMPUTE
MESSAGE/38=SPAWN (10, 'echo Hello', 'D8');
ENDCASE
DATA

SYSTEM: Calling a System Program


Available Operating Systems: Windows

Available Languages: reporting

The SYSTEM function calls a DOS program, a batch program, or a Windows application from a
procedure. SYSTEM passes a command string to DOS or Windows and the program is
executed as if it had been entered at the DOS command line or the command line field in the
Windows Program Manager Run dialog box. When you exit the program, control returns to
WebFOCUS.

SYSTEM suspends FOCUS execution of subsequent commands until you exit the application. It
has an advantage over the FOCUS DOS command, which also enables you to call DOS
programs and Windows applications from a procedure.

When executing a command from SYSTEM, the command executes as follows:

If the command name in the string passed to SYSTEM contains a .COM or .EXE extension,
the command is called directly rather than using the DOS command interpreter.

If the command name in the string does not contain a suffix or contains a .BAT extension,
SYSTEM calls the DOS command interpreter COMMAND.COM to perform the specified
command and then exit.

666
24. System Functions

SYSTEM passes the following commands to FOCUS, not to the DOS command interpreter:
CD, CLS, COPY, DEL, DIR, drive:, REN, TYPE. As a result, these commands are interpreted
directly by FOCUS, not by DOS, and you may observe a slightly different behavior. If you
want SYSTEM to pass these commands to the DOS command interpreter instead, use the
following syntax:
SYSTEM(length, 'COMMAND /C string', returncode)

Syntax: How to Call a DOS or Windows Program

SYSTEM(length, 'string ', returncode)

where:
length
Integer

Is the length, in characters, of string.


string
Alphanumeric

Is a valid Windows or DOS command with command line parameters enclosed in single
quotation marks.
returncode
Double precision

Is the name or length of the variable that contains the value of the DOS error level.

Example: Executing the DIR Command


SYSTEM passes the DIR command to the DOS command interpreter to create a sorted
directory listing with no heading information or summary, and redirects the output to a file
named DIR.LIS:
-SET &RETCODE = SYSTEM(31,'COMMAND /C DIR /O-N /B >DIR.LIS','D4');

Example: Changing the Default Directory


SYSTEM changes the default directory and suspends processing until the operation is
complete:
-SET &ERRORLEVEL = SYSTEM(15,'CHDIR \CARDATA','D4');

Using Functions 667


SYSVAR: Retrieving the Value of a z/OS System Variable

Example: Running the Check Disk Program


SYSTEM runs the check disk program and redirects the output to a file called CHKDSK.TXT.
(Redirecting the output to a file makes it accessible to a program that might want to read it.)
-SET &RETCODE=SYSTEM(19,'CHKDSK > CHKDSK.TXT','D4');

SYSVAR: Retrieving the Value of a z/OS System Variable


Available Operating Systems: z/OS

The SYSVAR function populates a Dialogue Manager amper variable with the contents of any
z/OS system variable. System variables are in the format [&]name[.], where the dot is
optional. They can be provided by the operating system or can be user defined. The function
can be called in a -SET command.

Syntax: How to Retrieve the Value of a z/OS System Variable

-SET &dmvar = SYSVAR('length','[&]sysvar[.]','outfmt');

where:
&dmvar
Alphanumeric

Is the name of the Dialogue Manager variable to be populated with the value of the z/OS
system variable.
length
Alphanumeric

Is the length of the next parameter in the call. Do not include the escape character in the
length, if one is present in the sysvar argument.
[&|]sysvar[.]
Alphanumeric

Is the name of the system variable to be retrieved. Note that the ampersand (&) and the
dot (.) are optional. If the ampersand is included, it must be followed by the escape
character (|).
outfmt
Alphanumeric

Is the format of the returned value enclosed in single quotation marks.

668
24. System Functions

Example: Retrieving the Value of the z/OS SYSNAME Variable


The following example populates the Dialogue Manager variable named &MYSNAME2 with the
value of the z/OS SYSNAME variable:
-SET &MYSNAME2=SYSVAR('7','SYSNAME','A8');
-TYPE SYSNAME:&MYSNAME2

The output is similar to the following:


SYSNAME:IBI1

Using Functions 669


SYSVAR: Retrieving the Value of a z/OS System Variable

670
Chapter 25 Simplified Geography Functions
The simplified geography functions perform location-based calculations and retrieve
geocoded points for various types of location data. They are used by the WebFOCUS
location intelligence products that produce maps and charts. Some of the geography
functions use GIS services and require valid credentials for accessing Esri ArcGIS
proprietary data.
In this chapter:

Sample Geography Files GIS_GEOMETRY: Building a JSON


Geometry Object
GIS_DISTANCE: Calculating the Distance
Between Geometry Points GIS_IN_POLYGON: Determining if a Point
is in a Complex Polygon
GIS_DRIVE_ROUTE: Calculating the
Driving Directions Between Geometry GIS_LINE: Building a JSON Line
Points
GIS_POINT: Building a Geometry Point
GIS_GEOCODE_ADDR: Geocoding a
GIS_REVERSE_COORDINATE: Returning a
Complete Address
Geographic Component
GIS_GEOCODE_ADDR_CITY: Geocoding
GIS_SERVICE_AREA: Calculating a
an Address Line, City, and State
Geometry Area Around a Given Point
GIS_GEOCODE_ADDR_POSTAL:
GIS_SERV_AREA_XY: Calculating a
Geocoding an Address Line and Postal
Service Area Around a Given Coordinate
Code

Using Functions 671


Sample Geography Files

Sample Geography Files


Some of the examples for the geography functions use geography sample files. One file, esri-
citibke.csv has station names, latitudes and longitudes, and trip start times and end times.
The other file, esri-geo10036.ftm has geometry data. To run the examples that use these files,
create an application named esri, and place the following files into the application folder.

esri-citibike.mas
FILENAME=ESRI-CITIBIKE, SUFFIX=DFIX ,
DATASET=esri/esri-citibike.csv, $
SEGMENT=CITIBIKE_TRIPDATA, SEGTYPE=S0, $
FIELDNAME=TRIPDURATION, ALIAS=tripduration, USAGE=I7, ACTUAL=A5V,
TITLE='tripduration', $
FIELDNAME=STARTTIME, ALIAS=starttime, USAGE=HMDYYS, ACTUAL=A18,
TITLE='starttime', $
FIELDNAME=STOPTIME, ALIAS=stoptime, USAGE=HMDYYS, ACTUAL=A18,
TITLE='stoptime', $
FIELDNAME=START_STATION_ID, ALIAS='start station id', USAGE=I6, ACTUAL=A4V,
TITLE='start station id', $
FIELDNAME=START_STATION_NAME, ALIAS='start station name', USAGE=A79V,
ACTUAL=A79BV, TITLE='start station name', $
FIELDNAME=START_STATION_LATITUDE, ALIAS='start station latitude', USAGE=P20.15,
ACTUAL=A18V, TITLE='start station latitude',
GEOGRAPHIC_ROLE=LATITUDE, $
FIELDNAME=START_STATION_LONGITUDE, ALIAS='start station longitude', USAGE=P20.14,
ACTUAL=A18V, TITLE='start station longitude',
GEOGRAPHIC_ROLE=LONGITUDE, $
FIELDNAME=END_STATION_ID, ALIAS='end station id', USAGE=I6,
ACTUAL=A4V, TITLE='end station id', $

FIELDNAME=END_STATION_NAME, ALIAS='end station name', USAGE=A79V,


ACTUAL=A79BV, TITLE='end station name', $
FIELDNAME=END_STATION_LATITUDE, ALIAS='end station latitude', USAGE=P20.15,
ACTUAL=A18V, TITLE='end station latitude',
GEOGRAPHIC_ROLE=LATITUDE, $
FIELDNAME=END_STATION_LONGITUDE, ALIAS='end station longitude', USAGE=P20.14,
ACTUAL=A18V, TITLE='end station longitude',
GEOGRAPHIC_ROLE=LONGITUDE, $
FIELDNAME=BIKEID, ALIAS=bikeid, USAGE=I7, ACTUAL=A5,
TITLE='bikeid', $
FIELDNAME=USERTYPE, ALIAS=usertype, USAGE=A10V, ACTUAL=A10BV,
TITLE='usertype', $
FIELDNAME=BIRTH_YEAR, ALIAS='birth year', USAGE=I6, ACTUAL=A4,
TITLE='birth year', $
FIELDNAME=GENDER, ALIAS=gender, USAGE=I3, ACTUAL=A1,
TITLE='gender', $
SEGMENT=ESRIGEO, SEGTYPE=KU, SEGSUF=FIX, PARENT=CITIBIKE_TRIPDATA,
DATASET=esri/esri-geo10036.ftm (LRECL 80 RECFM V, CRFILE=ESRI-GEO10036, $

672
25. Simplified Geography Functions

esri-citibike.acx

SEGNAME=CITIBIKE_TRIPDATA,
DELIMITER=',',
ENCLOSURE=",
HEADER=NO,
CDN=OFF, $

esri-citibike.csv

Note: Each complete record must be on a single line. Therefore, you must remove any line
breaks that may have been inserted due to the page width in this document.

1094,11/1/2015 0:00,11/1/2015 0:18,537,Lexington Ave & E 24 St,


40.74025878,-73.98409214,531,Forsyth St & Broome St,
40.71893904,-73.99266288,23959,Subscriber,1980,1

520,11/1/2015 0:00,11/1/2015 0:08,536,1 Ave & E 30 St,


40.74144387,-73.97536082,498,Broadway & W 32 St,
40.74854862,-73.98808416,22251,Subscriber,1988,1

753,11/1/2015 0:00,11/1/2015 0:12,229,Great Jones St,


40.72743423,-73.99379025,328,Watts St & Greenwich St,
40.72405549,-74.00965965,15869,Subscriber,1981,1

353,11/1/2015 0:00,11/1/2015 0:06,285,Broadway & E 14 St,


40.73454567,-73.99074142,151,Cleveland Pl & Spring St,
40.72210379,-73.99724901,21645,Subscriber,1987,1

1285,11/1/2015 0:00,11/1/2015 0:21,268,Howard St & Centre St,


40.71910537,-73.99973337,476,E 31 St & 3 Ave,40.74394314,-73.97966069,14788,Customer,,0

477,11/1/2015 0:00,11/1/2015 0:08,379,W 31 St & 7 Ave,40.749156,-73.9916,546,E 30 St &


Park Ave S,40.74444921,-73.98303529,21128,Subscriber,1962,2

362,11/1/2015 0:00,11/1/2015 0:06,407,Henry St & Poplar St,


40.700469,-73.991454,310,State St & Smith St,40.68926942,-73.98912867,21016,Subscriber,
1978,1

2316,11/1/2015 0:00,11/1/2015 0:39,147,Greenwich St & Warren St,


40.71542197,-74.01121978,441,E 52 St & 2 Ave,40.756014,-73.967416,24117,Subscriber,
1988,2

627,11/1/2015 0:00,11/1/2015 0:11,521,8 Ave & W 31 St,


40.75096735,-73.99444208,285,Broadway & E 14 St,
40.73454567,-73.99074142,17048,Subscriber,1986,2

1484,11/1/2015 0:01,11/1/2015 0:26,281,Grand Army Plaza & Central Park S,


40.7643971,-73.97371465,367,E 53 St & Lexington Ave,
40.75828065,-73.97069431,16779,Customer,,0

Using Functions 673


Sample Geography Files

284,11/1/2015 0:01,11/1/2015 0:06,247,Perry St & Bleecker St,


40.73535398,-74.00483091,453,W 22 St & 8 Ave,40.74475148,-73.99915362,17272,Subscriber,
1976,1

886,11/1/2015 0:01,11/1/2015 0:16,492,W 33 St & 7 Ave,40.75019995,-73.99093085,377,6


Ave & Canal St,40.72243797,-74.00566443,23019,Subscriber,1982,1

1379,11/1/2015 0:01,11/1/2015 0:24,512,W 29 St & 9 Ave,40.7500727,-73.99839279,445,E


10 St & Avenue A,40.72740794,-73.98142006,23843,Subscriber,1962,2

179,11/1/2015 0:01,11/1/2015 0:04,319,Fulton St & Broadway,


40.711066,-74.009447,264,Maiden Ln & Pearl St,
40.70706456,-74.00731853,22538,Subscriber,1981,1

309,11/1/2015 0:01,11/1/2015 0:07,160,E 37 St & Lexington Ave,


40.748238,-73.978311,362,Broadway & W 37 St,40.75172632,-73.98753523,22042,Subscriber,
1988,1

616,11/1/2015 0:02,11/1/2015 0:12,479,9 Ave & W 45 St,40.76019252,-73.9912551,440,E 45


St & 3 Ave,40.75255434,-73.97282625,22699,Subscriber,1982,1

852,11/1/2015 0:02,11/1/2015 0:16,346,Bank St & Hudson St,


40.73652889,-74.00618026,375,Mercer St & Bleecker St,
40.72679454,-73.99695094,21011,Subscriber,1991,1

1854,11/1/2015 0:02,11/1/2015 0:33,409,DeKalb Ave & Skillman St,


40.6906495,-73.95643107,3103,N 11 St & Wythe Ave,
40.72153267,-73.95782357,22011,Subscriber,1992,1

1161,11/1/2015 0:02,11/1/2015 0:21,521,8 Ave & W 31 St,40.75096735,-73.99444208,461,E


20 St & 2 Ave,40.73587678,-73.98205027,19856,Subscriber,1957,1

917,11/1/2015 0:02,11/1/2015 0:17,532,S 5 Pl & S 4 St,40.710451,-73.960876,393,E 5 St


& Avenue C,40.72299208,-73.97995466,18598,Subscriber,1991,1

esri-geo10036.mas

FILENAME=ESRI-GEO10036, SUFFIX=FIX ,
DATASET=esri/esri-geo10036.ftm (LRECL 80 RECFM V, IOTYPE=STREAM, $
SEGMENT=ESRIGEO, SEGTYPE=S0, $
FIELDNAME=GEOMETRY, ALIAS=GEOMETRY, USAGE=TX80L, ACTUAL=TX80,
MISSING=ON, $

674
25. Simplified Geography Functions

esri-geo10036.ftm
{"rings":[[[-73.9803889998524,40.7541490002762],[-73.9808779999197,40.7534830001
404],[-73.9814419998484,40.7537140000011],[-73.9824040001445,40.7541199998382],[
-73.982461000075,40.7541434001978],[-73.9825620002361,40.7541850001377],[-73.983
2877000673,40.7544888999428],[-73.9833499997027,40.7545150000673],[-73.983644399
969,40.7546397998869],[-73.9836849998628,40.7546570003204],[-73.9841276003085,40
.7548161002829],[-73.984399700086,40.7544544999752],[-73.9846140004357,40.754165
0001147],[-73.984871999743,40.7542749997914],[-73.9866590003126,40.7550369998577
],[-73.9874449996869,40.7553720000178],[-73.9902640001834,40.756570999552],[-73.
9914340001789,40.7570449998269],[-73.9918260002697,40.7572149995726],[-73.992429
0001982,40.7574769999636],[-73.9927679996434,40.7576240004473],[-73.993069000034
3,40.7578009996165],[-73.9931059999419,40.7577600004237],[-73.9932120003335,40.7
576230004012],[-73.9933250001486,40.7576770001934],[-73.9935390001247,40.7577669

998472],[-73.993725999755,40.7578459998931],[-73.9939599997542,40.757937999639],
[-73.9940989998689,40.7579839999617],[-73.9941529996611,40.7579959996157],[-73.9
942220001452,40.7580159996387],[-73.9943040003293,40.7580300002843],[-73.9943650
004444,40.7580330004227],[-73.99446499966,40.7580369997078],[-73.9945560002591,4
0.7580300002843],[-73.9946130001898,40.7580209998693],[-73.9945689999594,40.7580
809999383],[-73.9945449997519,40.7581149997075],[-73.9944196999092,40.7582882001
404],[-73.9943810002829,40.7583400001909],[-73.9953849998179,40.7587409997973],[
-73.9959560000693,40.7589690004191],[-73.9960649996999,40.7590149998424],[-73.99
68730000888,40.7593419996336],[-73.996975000296,40.7593809996335],[-73.997314999
7874,40.7595379996789],[-73.9977009996014,40.7597030000935],[-73.998039999946,40
.7598479995856],[-73.998334000014,40.7599709998618],[-73.9987769997587,40.760157
0003453],[-73.9990089996656,40.7602540003219],[-74.0015059997021,40.761292999672

2],[-74.0016340002089,40.7613299995799],[-74.0015350001401,40.7614539999022],[-7
4.0014580001865,40.7615479997405],[-74.0013640003483,40.7616560002242],[-74.0013
050003255,40.7617199995784],[-74.0011890003721,40.7618369995779],[-74.0010579997
269,40.7619609999003],[-74.0009659999808,40.7620389999],[-74.0008649998198,40.76
21230001764],[-74.0008390004195,40.7621430001993],[-74.0006839995669,40.76226100
0245],[-74.000531999752,40.7623750001062],[-74.0003759997525,40.7624849997829],[
-74.0002840000066,40.7625510001286],[-73.9998659996161,40.762850999574],[-73.999
8279996624,40.7628779999198],[-73.9995749996864,40.7630590001727],[-73.999312000
1487,40.7632720001028],[-73.9991639996189,40.7633989996642],[-73.998941000127,40
.7636250001936],[-73.9987589998279,40.7638580001466],[-73.9986331999622,40.76402
77004181],[-73.9986084002574,40.7640632002565],[-73.9984819996445,40.76423400039
89],[-73.9983469997142,40.7644199999831],[-73.998171999738,40.7646669996823],[-7
3.9980319995771,40.7648580003964],[-73.9979881998955,40.7649204996813],[-73.9979
368000432,40.7649942000224],[-73.9978947999051,40.7650573998791],[-73.9977017001

Using Functions 675


GIS_DISTANCE: Calculating the Distance Between Geometry Points

733,40.7653310995507],[-73.9975810003629,40.765481000348],[-73.9975069996483,40.
7654519999099],[-73.9956019999323,40.7646519998899],[-73.9955379996789,40.764625
0004434],[-73.9954779996099,40.7646030003282],[-73.9949389999348,40.764369000329
1],[-73.9936289997785,40.7638200001929],[-73.9934620001711,40.7637539998473],[-7
3.9931520002646,40.7636270002859],[-73.992701000151,40.7634409998023],[-73.99244
19000736,40.7633312995998],[-73.9898629996777,40.7622390001298],[-73.98861200044
34,40.761714000201],[-73.988021000169,40.761460000179],[-73.987028000242,40.7610
439998808],[-73.9867690998141,40.7609346998765],[-73.9848240002274,40.7601130001
149],[-73.9841635003452,40.7598425002312],[-73.9813259998949,40.7586439998208],[
-73.9805479999902,40.7583159999834],[-73.9793569999256,40.757814000216],[-73.978
1150002071,40.7572939996184],[-73.9785670003668,40.7566709996669],[-73.979014000
2958,40.7560309998308],[-73.9794719998329,40.7554120000638],[-73.9799399998311,4
0.7547649999048],[-73.9802380000836,40.7543610001601],[-73.9803889998524,40.7541
490002762]]]}
%$

GIS_DISTANCE: Calculating the Distance Between Geometry Points


The GIS_DISTANCE function uses a GIS service to calculate the distance between two
geometry points.

Syntax: How to Calculate the Distance Between Geometry Points

GIS_DISTANCE(geo_point1,geo_point2)

where:
geo_point1,geo_point2
Fixed length alphanumeric, large enough to hold the JSON describing the point (for
example, A200).

Are the geometry points for which you want to calculate the distance.

Note: You can generate a geometry point using the GIS_POINT function.

676
25. Simplified Geography Functions

Example: Calculating the Distance Between Two Geometry Points


The following uses a citibike .csv file that contains station names, latitudes and longitudes,
and trip start times and end times. It uses the GIS_POINT function to define geometry points
for start stations and end stations. It then uses GIS_DISTANCE to calculate the distance
between them.
DEFINE FILE esri/esri-citibike
STARTPOINT/A200 = GIS_POINT('4326', START_STATION_LONGITUDE,
START_STATION_LATITUDE);
ENDPOINT/A200 = GIS_POINT('4326', END_STATION_LONGITUDE,
END_STATION_LATITUDE);
Distance/P10.2 = GIS_DISTANCE(ENDPOINT, STARTPOINT);
END
TABLE FILE esri/esri-citibike
PRINT END_STATION_NAME AS End Distance
BY START_STATION_NAME AS Start
ON TABLE SET PAGE NOLEAD
END

Using Functions 677


GIS_DRIVE_ROUTE: Calculating the Driving Directions Between Geometry Points

The output is shown in the following image.

GIS_DRIVE_ROUTE: Calculating the Driving Directions Between Geometry Points


The GIS_DRIVE_ROUTE function uses a GIS service to calculate the driving route between two
geometry points.

Note: This function uses GIS services and requires an Esri ArcGIS adapter connection with
named credentials.

Syntax: How to Calculate the Drive Route Between Geometry Points

GIS_DRIVE_ROUTE(geo_start_point,geo_end_point)

678
25. Simplified Geography Functions

where:
geo_start_point,geo_point2
Fixed length alphanumeric, large enough to hold the JSON describing the point (for
example, A200).

Is the starting point for which you want to calculate the drive route.

Note: You can generate a geometry point using the GIS_POINT function.
geo_end_point,geo_point2
Fixed length alphanumeric, large enough to hold the JSON describing the point (for
example, A200).

Is the ending point for which you want to calculate the drive route.

Note: You can generate a geometry point using the GIS_POINT function.

The format of the field to which the drive route will be returned is TX.

Example: Calculating the Drive Route Between Two Geometry Points


The following uses a citibike .csv file that contains station names, latitudes and longitudes,
and trip start times and end times. It uses the GIS_POINT function to define geometry points
for start stations and end stations. It then uses GIS_DRIVE_ROUTE to calculate the route to
get from the end point to the start point.
DEFINE FILE esri/esri-citibike
STARTPOINT/A200 = GIS_POINT('4326', START_STATION_LONGITUDE,
START_STATION_LATITUDE);
ENDPOINT/A200 = GIS_POINT('4326', END_STATION_LONGITUDE,
END_STATION_LATITUDE);
Route/TX140 (GEOGRAPHIC_ROLE=GEOMETRY_LINE) =
GIS_DRIVE_ROUTE(ENDPOINT, STARTPOINT);
END
TABLE FILE esri/esri-citibike
PRINT START_STATION_NAME AS Start END_STATION_NAME AS End Route
WHERE START_STATION_ID EQ 147
ON TABLE SET PAGE NOLEAD
ON TABLE SET STYLE *
TYPE=REPORT, GRID=OFF,SIZE-11,$
ENDSTYLE
END

Using Functions 679


GIS_DRIVE_ROUTE: Calculating the Driving Directions Between Geometry Points

The output is shown in the following image.

Example: Charting a Driving Route Between Two Geometry Points


The following request uses GIS_DRIVE_ROUTE to generate a driving route between a station
start point and end point and charts the route on an Esri map.
DEFINE FILE esri-citibike
STARTPOINT/A200 = GIS_POINT('4326', START_STATION_LONGITUDE,
START_STATION_LATITUDE);
ENDPOINT/A200 = GIS_POINT('4326', END_STATION_LONGITUDE,
END_STATION_LATITUDE);
Route/TX80 (GEOGRAPHIC_ROLE=GEOMETRY_LINE) =
GIS_DRIVE_ROUTE(ENDPOINT, STARTPOINT);
END

680
25. Simplified Geography Functions

GRAPH FILE ESRI-CITIBIKE


PRINT
START_STATION_NAME
END_STATION_NAME
WHERE START_STATION_ID EQ 147
ON TABLE PCHOLD FORMAT JSCHART
ON TABLE SET LOOKGRAPH CHOROPLETH
ON TABLE SET EMBEDHEADING ON
ON TABLE SET AUTOFIT ON
ON TABLE SET STYLE *
TYPE=REPORT, TITLETEXT='Map', PAGESIZE=E, CHART-LOOK=com.esri.map, $
TYPE=DATA, COLUMN=N1, /*START_STATION_NAME*/
BUCKET=tooltip, $
TYPE=DATA, COLUMN=N2, /*END_STATION_NAME*/

*GRAPH_JS_FINAL
"legend": {"visible": true},
"extensions" : { "com.esri.map" :
{ "scalebar" :
{
"scalebarUnit": "dual",
"attachTo" : "bottom-left"
},
"baseMapInfo": {
"drawBasemapControl" : false,
"showArcGISBasemaps" : false,
"customBaseMaps" : [
{"ibiBaseLayer" : "dark-gray"}
]
},
"overlayLayers":
[{
"ibiDataLayer": {"map-geometry" : {"map_by_field" : "Route"}}, "title" :
"Chart"}]
},
"introAnimation": "{\"enabled\":false}"
}

*END
ENDSTYLE
HEADING
"Chart Drive Route"
END

Using Functions 681


GIS_GEOCODE_ADDR: Geocoding a Complete Address

The output is shown in the following image.

GIS_GEOCODE_ADDR: Geocoding a Complete Address


GIS_GEOCODE_ADDR uses a GIS geocoding service to obtain the geometry point for a
complete address.

Note: This function uses GIS services and requires an Esri ArcGIS adapter connection with
named credentials.

Syntax: How to Geocode a Complete Address

GIS_GEOCODE_ADDR(address[, country])

where:
address
Fixed length alphanumeric

Is the complete address to be geocoded.


country
Fixed length alphanumeric

Is a country name, which is optional if the country is the United States.

682
25. Simplified Geography Functions

Example: Geocoding a Complete Address


The following request creates a complete address by concatenating the street address, city,
state, and ZIP code. It then uses GIS_GEOCODE_ADDR to create a GIS point for the address.

DEFINE FILE WF_RETAIL_LITE


GADDRESS/A200 =ADDRESS_LINE_1 || ' ' | CITY_NAME || ' ' | STATE_PROV_NAME
|| ' ' | POSTAL_CODE;
GEOCODE1/A200 = GIS_GEOCODE_ADDR(GADDRESS);
END
TABLE FILE WF_RETAIL_LITE
PRINT ADDRESS_LINE_1 AS Address GEOCODE1
BY POSTAL_CODE AS Zip
WHERE CITY_NAME EQ 'New York'
WHERE POSTAL_CODE FROM '10013' TO '10020'
ON TABLE SET PAGE NOPAGE
END

The output is shown in the following image.

GIS_GEOCODE_ADDR_CITY: Geocoding an Address Line, City, and State


GIS_GEOCODE_ADDR_CITY uses a GIS geocoding service to obtain the geometry point for an
address line, city, state, and optional country. The returned value is a fixed length
alphanumeric format, large enough to hold the JSON describing the geographic location (for
example, A200).

Note: This function uses GIS services and requires an Esri ArcGIS adapter connection with
named credentials.

Syntax: How to Geocode an Address Line, City, and State

GIS_GEOCODE_ADDR_CITY( street_addr, city , state [, country])

Using Functions 683


GIS_GEOCODE_ADDR_CITY: Geocoding an Address Line, City, and State

where:
street_addr
Fixed length alphanumeric

Is the street address to be geocoded.


city
Fixed length alphanumeric

Is the city name associated with the street address.


state
Fixed length alphanumeric

Is the state name associated with the street address.


country
fixed length alphanumeric

Is a country name, which is optional if the country is the United States.

Example: Geocoding a Street Address, City, and State


The following request geocodes a street address using GIS_GEOCODE_ADDR_CITY.
DEFINE FILE WF_RETAIL_LITE
GEOCODE1/A200 = GIS_GEOCODE_ADDR_CITY(ADDRESS_LINE_1, CITY_NAME ,
STATE_PROV_NAME);
END
TABLE FILE WF_RETAIL_LITE
PRINT ADDRESS_LINE_1 AS Address GEOCODE1
BY POSTAL_CODE AS Zip
WHERE CITY_NAME EQ 'New York'
WHERE POSTAL_CODE FROM '10013' TO '10020'
ON TABLE SET PAGE NOPAGE
END

684
25. Simplified Geography Functions

The output is shown in the following image.

GIS_GEOCODE_ADDR_POSTAL: Geocoding an Address Line and Postal Code


GIS_GEOCODE_ADDR_POSTAL uses a GIS geocoding service to obtain the geometry point for
an address line, postal code and optional country. The returned value is a fixed length
alphanumeric format, large enough to hold the JSON describing the geographic location (for
example, A200).

Note: This function uses GIS services and requires an Esri ArcGIS adapter connection with
named credentials.

Syntax: How to Geocode an Address Line and Postal Code

GIS_GEOCODE_ADDR_POSTAL( street_addr, postal_code [, country])

where:
street_addr
fixed length alphanumeric

Is the street address to be geocoded.


postal_code
fixed length alphanumeric

Is the postal code associated with the street address.


country
fixed length alphanumeric

Is a country name, which is optional if the country is the United States.

Using Functions 685


GIS_GEOMETRY: Building a JSON Geometry Object

Example: Geocoding a Street Address and Postal Code


The following request geocodes a street address using GIS_GEOCODE_ADDR_POSTAL.

DEFINE FILE WF_RETAIL_LITE


GEOCODE1/A200 = GIS_GEOCODE_ADDR_POSTAL(ADDRESS_LINE_1, POSTAL_CODE);
END
TABLE FILE WF_RETAIL_LITE
PRINT ADDRESS_LINE_1 AS Address GEOCODE1
BY POSTAL_CODE AS Zip
WHERE CITY_NAME EQ 'New York'
WHERE POSTAL_CODE FROM '10013' TO '10020'
ON TABLE SET PAGE NOPAGE
END

The output is shown in the following image.

GIS_GEOMETRY: Building a JSON Geometry Object


The GIS_GEOMETRY function builds a JSON Geometry object given a geometry type, WKID, and
a geometry.

Syntax: How to Build a JSON Geometry Object

GIS_GEOMETRY(geotype, wkid, geometry)

where:
geotype
Alphanumeric

Is a geometry type, for example, 'esriGeometryPolygon' ,esriGeometryPolyline,


'esriGeometryMultipoint', 'EsriGeometryPoint', 'EsriGeometryExtent'..

686
25. Simplified Geography Functions

wkid
Alphanumeric

Is a valid spatial reference ID. WKID is an abbreviation for Well-Known ID, which identifies
a projected or geographic coordinate system.
geometry
TX

A geometry in JSON.

The output is returned as TX.

Example: Building a JSON Geometry Object


The following request builds a polygon geometry of the area encompassing ZIP code 10036 in
Manhattan. The input geometry object is stored in a text (.ftm) file that is cross-referenced in
the esri-citibike Master File. The field containing the geometry object is GEOMETRY.
DEFINE FILE esri/esri-citibike
WKID/A10 = '4326';
MASTER_GEOMETRY/TX256 (GEOGRAPHIC_ROLE=GEOMETRY_AREA) =
GIS_GEOMETRY( 'esriGeometryPolygon', WKID , GEOMETRY );
END
TABLE FILE esri/esri-citibike
PRINT
START_STATION_NAME AS Station
START_STATION_LATITUDE AS Latitude
START_STATION_LONGITUDE AS Longitude
MASTER_GEOMETRY AS 'JSON Geometry Object'
WHERE START_STATION_ID EQ 479
ON TABLE SET PAGE NOLEAD
ON TABLE SET STYLE *
type=report, grid=off, size=10,$
ENDSTYLE
END

Using Functions 687


GIS_GEOMETRY: Building a JSON Geometry Object

The output is shown in the following image.

Example: Charting a Geometry Object


The following request uses GIS_GEOMETRY to build a geometry object and chart it on an Esri
map.
DEFINE FILE esri-citibike
WKID/A10 = '4326';
MASTER_GEOMETRY/TX256 (GEOGRAPHIC_ROLE=GEOMETRY_AREA) =
GIS_GEOMETRY( 'esriGeometryPolygon', WKID , GEOMETRY );
END

GRAPH FILE ESRI-CITIBIKE


PRINT
START_STATION_NAME
END_STATION_NAME
ON TABLE PCHOLD FORMAT JSCHART
ON TABLE SET LOOKGRAPH CHOROPLETH
ON TABLE SET EMBEDHEADING ON
ON TABLE SET AUTOFIT ON
ON TABLE SET STYLE *
TYPE=REPORT, TITLETEXT='Map', PAGESIZE=E, CHART-LOOK=com.esri.map, $
TYPE=DATA, COLUMN=N1, /*START_STATION_NAME*/
BUCKET=tooltip, $
TYPE=DATA, COLUMN=N2, /*END_STATION_NAME*/

688
25. Simplified Geography Functions

*GRAPH_JS_FINAL

"legend": {"visible": true},


"extensions" : { "com.esri.map" :
{ "scalebar" :
{
"scalebarUnit": "dual",
"attachTo" : "bottom-left"
},
"baseMapInfo": {
"drawBasemapControl" : false,
"showArcGISBasemaps" : false,
"customBaseMaps" : [
{"ibiBaseLayer" : "dark-gray"}
]
},
"overlayLayers":
[{
"ibiDataLayer": {"map-geometry" : {"map_by_field" : "MASTER_GEOMETRY"}},
"title" : "Chart"}]
},
"introAnimation": "{\"enabled\":false}"
}

*END
ENDSTYLE
HEADING
"Chart Geometry Object"
END

The output is shown in the following image.

Using Functions 689


GIS_IN_POLYGON: Determining if a Point is in a Complex Polygon

GIS_IN_POLYGON: Determining if a Point is in a Complex Polygon


Given a point and a polygon definition, the GIS_IN_POLYGON function returns the value 1
(TRUE) if the point is in the polygon or 0 (FALSE) if the point is not in the polygon. The value is
returned in integer format.

Syntax: How to Determine if a Point is in a Complex Polygon

GIS_IN_POLYGON(point, polygon_definition)

where:
point
Alphanumeric or text

Is the geometry point.


polygon_definition
Text

Is the geometry area (polygon) definition.

Example: Determining if a Point is in a Polygon


The following example determines if a station is inside ZIP code 10036. GIS_IN_POLYGON
returns 1 for a point inside the polygon definition and 0 for a point outside. The polygon
definition being passed is the same one used in the example for the GIS_GEOMETRY function
described previously and defines the polygon for ZIP code 10036 in Manhattan in New York
City. The value 1 is translated to Yes and 0 to No for display on the output.
DEFINE FILE esri/esri-citibike
WKID/A10 = '4326';
MASTER_GEOMETRY/TX256 (GEOGRAPHIC_ROLE=GEOMETRY_AREA) =
GIS_GEOMETRY( 'esriGeometryPolygon', WKID , GEOMETRY );
START_STATION_POINT/A200=GIS_POINT(WKID, START_STATION_LONGITUDE,
START_STATION_LATITUDE);
STATION_IN_POLYGON/I4=GIS_IN_POLYGON(START_STATION_POINT, MASTER_GEOMETRY);
IN_POLYGON/A5 = IF STATION_IN_POLYGON EQ 1 THEN 'Yes' ELSE 'No';
END
TABLE FILE esri/esri-citibike
PRINT
START_STATION_NAME AS Station
IN_POLYGON AS 'Station in zip, code 10036?'
BY START_STATION_ID AS 'Station ID'
ON TABLE SET PAGE NOLEAD
ON TABLE SET STYLE *
type=report, grid=off, size=10,$
type=data, column=in_polygon, style=bold, color=red, when = in_polygon eq
'Yes',$
ENDSTYLE
END

690
25. Simplified Geography Functions

The output is shown in the following image.

GIS_LINE: Building a JSON Line


Given two geometry points or lines, GIS_LINE builds a JSON line. The output is returned in text
format.

Syntax: How to Build a JSON Line

GIS_LINE(geometry1, geometry2)

where:
geometry1
Alphanumeric or text

Is the first point or line for defining the beginning of the new line.

Using Functions 691


GIS_LINE: Building a JSON Line

geometry2
Alphanumeric or text

Is the second point or line for the concatenation of the new line.

Example: Building a JSON Line


The following request prints start stations and end stations and builds a JSON line between
them.
DEFINE FILE ESRI/ESRI-CITIBIKE
STARTPOINT/A200 = GIS_POINT('4326', START_STATION_LONGITUDE,
START_STATION_LATITUDE);
ENDPOINT/A200 = GIS_POINT('4326', END_STATION_LONGITUDE,
END_STATION_LATITUDE);
CONNECTION_LINE/TX80 (GEOGRAPHIC_ROLE=GEOMETRY_LINE) =
GIS_LINE(STARTPOINT, ENDPOINT);
END
TABLE FILE ESRI/ESRI-CITIBIKE
PRINT END_STATION_NAME AS End CONNECTION_LINE AS 'Connecting Line'
BY START_STATION_NAME AS Start
WHERE START_STATION_NAME LE 'D'
ON TABLE SET PAGE NOLEAD
ON TABLE SET STYLE *
TYPE=REPORT, GRID=OFF,$
ENDSTYLE
END

692
25. Simplified Geography Functions

The output is shown in the following image.

Example: Charting Geometry Lines


The following request generates geometry lines and charts them on an Esri map.
DEFINE FILE ESRI-CITIBIKE
CONNECTION_LINE/TX80 (GEOGRAPHIC_ROLE=GEOMETRY_LINE)
=GIS_LINE(START_STATION_POINT, END_STATION_POINT);
DISTANCE/P33.11 TITLE 'Distance'=GIS_DISTANCE(START_STATION_POINT,
END_STATION_POINT);
END

Using Functions 693


GIS_LINE: Building a JSON Line

GRAPH FILE ESRI-CITIBIKE


PRINT
START_STATION_NAME
END_STATION_NAME
DISTANCE
ON TABLE PCHOLD FORMAT JSCHART
ON TABLE SET LOOKGRAPH BUBBLEMAP
ON TABLE SET EMBEDHEADING ON
ON TABLE SET AUTOFIT ON
ON TABLE SET STYLE *
TYPE=REPORT, TITLETEXT='Map', PAGESIZE=E, CHART-LOOK=com.esri.map, $
TYPE=DATA, COLUMN=N1, /*START_STATION_NAME*/
BUCKET=tooltip, $
TYPE=DATA, COLUMN=N2, /*END_STATION_NAME*/
BUCKET=tooltip, $
TYPE=DATA, COLUMN=N3, /*DISTANCE*/
BUCKET=tooltip, $

*GRAPH_JS_FINAL
"legend": {"visible": true},
"extensions" : { "com.esri.map" :
{ "scalebar" :
{
"scalebarUnit": "dual",
"attachTo" : "bottom-left"
},
"baseMapInfo": {
"drawBasemapControl" : false,
"showArcGISBasemaps" : false,
"customBaseMaps" : [
{"ibiBaseLayer" : "dark-gray"}
]
},
"overlayLayers":
[{
"ibiDataLayer": {"map-geometry" : {"map_by_field" : "CONNECTION_LINE"}},
"title" : "Chart"}]
},
"introAnimation": "{\"enabled\":false}"
}

*END
ENDSTYLE
HEADING
"Chart Geometry Lines"
END

694
25. Simplified Geography Functions

The output is shown in the following image.

GIS_POINT: Building a Geometry Point


Given a WKID (Well-Known ID) spatial reference, longitude, and latitude, the GIS_POINT
function builds a JSON point defining a Geometry object with the provided WKID, longitude, and
latitude. The function is optimized for those SQL engines that can build a JSON geometry
object.

The field to which the point is returned should have fixed length alphanumeric format, large
enough to hold the JSON describing the point (for example, A200).

Syntax: How to Build a Geometry Point

GIS_POINT(wkid, longitude, latitude)

where:
wkid
Fixed length alphanumeric

Is a spatial reference code (WKID). WKID is an abbreviation for Well-Known ID, which
identifies a projected or geographic coordinate system.
longitude
D20.8

Is the longitude for the point.

Using Functions 695


GIS_POINT: Building a Geometry Point

latitude
D20.8

Is the latitude for the point.

Example: Building a Geometry Point


The following request uses the spatial reference code 4326 (decimal degrees) and state
capital longitudes and latitudes to build a geometry point.
DEFINE FILE WF_RETAIL_LITE
GPOINT/A200 = GIS_POINT('4326', STATE_PROV_CAPITAL_LONGITUDE,
STATE_PROV_CAPITAL_LATITUDE);
END
TABLE FILE WF_RETAIL_LITE
SUM FST.STATE_PROV_CAPITAL_LONGITUDE AS Longitude
FST.STATE_PROV_CAPITAL_LATITUDE AS Latitude
FST.GPOINT AS Point
BY STATE_PROV_CAPITAL_NAME AS Capital
WHERE COUNTRY_NAME EQ 'United States'
WHERE STATE_PROV_CAPITAL_NAME LT 'C'
ON TABLE SET PAGE NOPAGE
END

The output is shown in the following image.

696
25. Simplified Geography Functions

Example: Charting Geometry Points


The following request generates geometry points using GIS_POINT charts them on an Esri map.
DEFINE FILE WF_RETAIL
GPOINT/A200 = GIS_POINT('4326', STATE_PROV_CAPITAL_LONGITUDE,
STATE_PROV_CAPITAL_LATITUDE);
END

GRAPH FILE WF_RETAIL


PRINT
STATE_PROV_NAME
WHERE STATE_PROV_CAPITAL_LONGITUDE NE MISSING
ON TABLE PCHOLD FORMAT JSCHART
ON TABLE SET LOOKGRAPH BUBBLEMAP
ON TABLE SET EMBEDHEADING ON
ON TABLE SET AUTOFIT ON
ON TABLE SET STYLE *
TYPE=REPORT, TITLETEXT='Map', PAGESIZE=E, CHART-LOOK=com.esri.map, $
TYPE=DATA, COLUMN=N1,
BUCKET=tooltip, $

*GRAPH_JS_FINAL
"bubbleMarker": {"maxSize": "10%"},
"legend": {"visible": true},
"extensions" : { "com.esri.map" :
{ "scalebar" :
{
"scalebarUnit": "dual",
"attachTo" : "bottom-left"
},
"baseMapInfo": {
"drawBasemapControl" : false,
"showArcGISBasemaps" : false,
"customBaseMaps" : [
{"ibiBaseLayer" : "gray"}
]
},
"overlayLayers":
[{
"ibiDataLayer": {"map-geometry" : {"map_by_field" : "GPOINT"}},
"title" : "Report"}]
},
"introAnimation": "{\"enabled\":false}"
}

*END
ENDSTYLE
HEADING
"Chart Geometry Points"
END

Using Functions 697


GIS_REVERSE_COORDINATE: Returning a Geographic Component

The output is shown in the following image.

GIS_REVERSE_COORDINATE: Returning a Geographic Component


Given longitude and latitude values and the name of a geographic component,
GIS_REVERSE_COORDINATE returns the specified geographic component values associated
with those coordinates.

Note: This function uses GIS services and requires an Esri ArcGIS adapter connection with
named credentials.

Syntax: How to Return a Geographic Component

GIS_REVERSE_COORDINATE(longitude, latitude, component)

where:
longitude
Numeric

Is the longitude of the component to return.


latitude
Numeric

Is the latitude of the component to return.

698
25. Simplified Geography Functions

component
Keyword

Is one of the following components:

MATCH_ADDRESS, which returns the matching address.

METROAREA, which returns the metro area name.

REGION, which returns the region name.

SUBREGION, which returns the subregion name.

CITY, which returns the city name.

POSTAL, which returns the postal code.

The value is returned as text and can be assigned to a field with text or alphanumeric (fixed or
variable length) format.

Example: Returning Geographic Components Associated With Coordinates


The following request uses city longitude and city latitude to return the matching address,
postal code, region, and subregion.
TABLE FILE WF_RETAIL_GEOGRAPHY
SUM FST.CITY_LONGITUDE AS Longitude FST.CITY_LATITUDE AS Latitude
COMPUTE
MatchingAddress/A250 = GIS_REVERSE_COORDINATE(CITY_LONGITUDE,
CITY_LATITUDE, MATCH_ADDRESS);
PostalCode/A250 = GIS_REVERSE_COORDINATE(CITY_LONGITUDE,
CITY_LATITUDE, POSTAL);
Region/A250 = GIS_REVERSE_COORDINATE(CITY_LONGITUDE, CITY_LATITUDE,
REGION);
Subregion/A250 = GIS_REVERSE_COORDINATE(CITY_LONGITUDE, CITY_LATITUDE,
SUBREGION);
BY CITY_NAME AS City
WHERE COUNTRY_NAME EQ 'United States'
WHERE TOTAL PostalCode NE ' '
WHERE RECORDLIMIT EQ 20
ON TABLE SET PAGE NOLEAD
END

Using Functions 699


GIS_SERVICE_AREA: Calculating a Geometry Area Around a Given Point

The output is shown in the following image.

GIS_SERVICE_AREA: Calculating a Geometry Area Around a Given Point


The GIS_SERVICE_AREA function uses a GIS service to calculate the geometry area with
access boundaries within the given time or distance from the provided geometry point. The
output is returned in text format.

Note: This function uses GIS services and requires an Esri ArcGIS adapter connection with
named credentials.

Syntax: How to Calculate a Geometry Area Around a Point

GIS_SERVICE_AREA(geo_point, distance, travel_mode)

where:
geo_point
Alphanumeric

Is the starting geometry point.


distance
Alphanumeric

Is the travel limitation in either time or distance units.


travel_mode
Alphanumeric

Is a valid travel mode as defined in gis_serv_area.mas in the Catalog directory under the
server installation directory. The accepted travel modes are;

'Miles'. This is the default value.

700
25. Simplified Geography Functions

'TravelTime'.

'TruckTravelTime'.

'WalkTime'.

'Kilometers'.

Example: Calculating a Service Area Around a Geometry Point


The following request calculates the geometry area that is a five-minute walk around a station.
DEFINE FILE esri/esri-citibike
WKID/A10='4326';
START_STATION_POINT/A200=GIS_POINT(WKID, START_STATION_LONGITUDE,
START_STATION_LATITUDE);
DISTANCE/A10='5';
TRAVEL_MODE/A10='WalkTime';
STATION_SERVICE_AREA/TX80 (GEOGRAPHIC_ROLE=GEOMETRY_AREA)=
GIS_SERVICE_AREA(START_STATION_POINT, DISTANCE, TRAVEL_MODE);
END
TABLE FILE esri/esri-citibike
PRINT
START_STATION_ID AS 'Station ID'
START_STATION_NAME AS 'Station Name'
STATION_SERVICE_AREA AS '5-Minute Walk Service Area Around Station'
WHERE START_STATION_ID EQ 479 OR 512;
ON TABLE SET PAGE NOLEAD
ON TABLE SET STYLE *
TYPE=REPORT, GRID=OFF, SIZE=12,$
ENDSTYLE
END

Using Functions 701


GIS_SERVICE_AREA: Calculating a Geometry Area Around a Given Point

The output is shown in the following image.

Example: Charting a Geometry Service Area Around a Point


The following request generates service areas that are 5-minute walking distances from start
station geometry points and charts them on an Esri map.
DEFINE FILE esri-citibike
WKID/A10='4326';
START_STATION_POINT/A200=GIS_POINT(WKID, START_STATION_LONGITUDE,
START_STATION_LATITUDE);
DISTANCE/A10='5';
TRAVEL_MODE/A10='WalkTime';
STATION_SERVICE_AREA/TX80 (GEOGRAPHIC_ROLE=GEOMETRY_AREA)=
GIS_SERVICE_AREA(START_STATION_POINT, DISTANCE, TRAVEL_MODE);
END

702
25. Simplified Geography Functions

GRAPH FILE ESRI-CITIBIKE


PRINT
START_STATION_NAME
END_STATION_NAME
DISTANCE
ON TABLE PCHOLD FORMAT JSCHART
ON TABLE SET LOOKGRAPH CHOROPLETH
ON TABLE SET EMBEDHEADING ON
ON TABLE SET AUTOFIT ON
ON TABLE SET STYLE *
TYPE=REPORT, TITLETEXT='Map', PAGESIZE=E, CHART-LOOK=com.esri.map, $
TYPE=DATA, COLUMN=N1, /*START_STATION_NAME*/
BUCKET=tooltip, $
TYPE=DATA, COLUMN=N2, /*END_STATION_NAME*/
BUCKET=tooltip, $
TYPE=DATA, COLUMN=N3, /*DISTANCE*/
BUCKET=tooltip, $

*GRAPH_JS_FINAL
"legend": {"visible": true},
"extensions" : { "com.esri.map" :
{ "scalebar" :
{
"scalebarUnit": "dual",
"attachTo" : "bottom-left"
},
"baseMapInfo": {
"drawBasemapControl" : false,
"showArcGISBasemaps" : false,
"customBaseMaps" : [
{"ibiBaseLayer" : "dark-gray"}
]
},
"overlayLayers":
[{
"ibiDataLayer": {"map-geometry" : {"map_by_field" :
"STATION_SERVICE_AREA"}}, "title" : "Chart"}]
},
"introAnimation": "{\"enabled\":false}"
}

*END
ENDSTYLE
HEADING
"Chart Geometry Service Area"
END

Using Functions 703


GIS_SERV_AREA_XY: Calculating a Service Area Around a Given Coordinate

The output is shown in the following image.

GIS_SERV_AREA_XY: Calculating a Service Area Around a Given Coordinate


The GIS_SERV_AREA_XY function uses a GIS service to calculate the geometry area with
access boundaries within the given time or distance from the provided coordinate. The output
is returned in text format.

Note: This function uses GIS services and requires an Esri ArcGIS adapter connection with
named credentials.

Syntax: How to Calculate a Geometry Area Around a Coordinate

GIS_SERV_AREA_XY(longitude, latitude, distance, travel_mode[, wkid])

where:
longitude
Alphanumeric

Is the longitude of the starting point.


latitude
Alphanumeric

Is the latitude of the starting point.

704
25. Simplified Geography Functions

distance
Integer

Is the travel limitation in either time or distance units.


travel_mode
Alphanumeric

Is a valid travel mode as defined in gis_serv_area.mas in the Catalog directory under the
server installation directory. The accepted travel modes are;

'Miles'. This is the default value.

'TravelTime'.

'TruckTravelTime'.

'WalkTime'.

'Kilometers'.
wkid
Alphanmeric

Is the spatial reference ID for the coordinate. WKID is an abbreviation for Well-Known ID,
which identifies a projected or geographic coordinate system. The default value is '4326',
which represents decimal degrees.

Using Functions 705


GIS_SERV_AREA_XY: Calculating a Service Area Around a Given Coordinate

Example: Calculating a Service Area Around a Coordinate


The following request calculates the geometry area that is a five-minute walk around a station,
using the longitude and latitude that specify the station location.
DEFINE FILE esri/esri-citibike
DISTANCE/I4=5;
WKID/A10='4326';
TRAVEL_MODE/A10='WalkTime';
STATION_SERVICE_AREA/TX80 (GEOGRAPHIC_ROLE=GEOMETRY_AREA)=
GIS_SERV_AREA_XY(START_STATION_LONGITUDE, START_STATION_LATITUDE,
DISTANCE, TRAVEL_MODE, WKID);
END
TABLE FILE esri/esri-citibike
PRINT
START_STATION_ID AS 'Station ID'
START_STATION_NAME AS 'Station Name'
STATION_SERVICE_AREA
AS '5-Minute Walk Service Area Around Station Coordinate'
WHERE START_STATION_ID EQ 479 OR 512;
ON TABLE SET PAGE NOLEAD
ON TABLE SET STYLE *
TYPE=REPORT, GRID=OFF, SIZE=12,$
ENDSTYLE
END

706
25. Simplified Geography Functions

The output is shown in the following image.

Example: Charting a Geometry Service Area Around a Coordinate


The following request generates service areas that are 5-minute walking distances from start
station coordinates and charts them on an Esri map.
DEFINE FILE esri-citibike
WKID/A10='4326';
DISTANCE/A10='5';
TRAVEL_MODE/A10='WalkTime';
STATION_SERVICE_AREA/TX80 (GEOGRAPHIC_ROLE=GEOMETRY_AREA)=
GIS_SERV_AREA_XY(START_STATION_LONGITUDE, START_STATION_LATITUDE,
DISTANCE, TRAVEL_MODE, WKID);
END

Using Functions 707


GIS_SERV_AREA_XY: Calculating a Service Area Around a Given Coordinate

GRAPH FILE ESRI-CITIBIKE


PRINT
START_STATION_NAME
END_STATION_NAME
DISTANCE
ON TABLE PCHOLD FORMAT JSCHART
ON TABLE SET LOOKGRAPH CHOROPLETH
ON TABLE SET EMBEDHEADING ON
ON TABLE SET AUTOFIT ON
ON TABLE SET STYLE *
TYPE=REPORT, TITLETEXT='Map', PAGESIZE=E, CHART-LOOK=com.esri.map, $
TYPE=DATA, COLUMN=N1, /*START_STATION_NAME*/
BUCKET=tooltip, $
TYPE=DATA, COLUMN=N2, /*END_STATION_NAME*/
BUCKET=tooltip, $
TYPE=DATA, COLUMN=N3, /*DISTANCE*/
BUCKET=tooltip, $

*GRAPH_JS_FINAL
"legend": {"visible": true},
"extensions" : { "com.esri.map" :
{ "scalebar" :
{
"scalebarUnit": "dual",
"attachTo" : "bottom-left"
},
"baseMapInfo": {
"drawBasemapControl" : false,
"showArcGISBasemaps" : false,
"customBaseMaps" : [
{"ibiBaseLayer" : "dark-gray"}
]
},
"overlayLayers":
[{
"ibiDataLayer": {"map-geometry" : {"map_by_field" :
"STATION_SERVICE_AREA"}}, "title" : "Chart"}]
},
"introAnimation": "{\"enabled\":false}"
}

*END
ENDSTYLE
HEADING
"Chart Geometry Service Area"
END

708
25. Simplified Geography Functions

The output is shown in the following image.

Using Functions 709


GIS_SERV_AREA_XY: Calculating a Service Area Around a Given Coordinate

710
Chapter 26 SQL Character Functions
SQL character functions manipulate alphanumeric fields and character strings.

They can be used in SQL Translator requests and, where supported by the DBMS, in
Direct SQL Passthru requests.
In this chapter:

LOCATE: Returning the Position of a Substring in a String

LOCATE: Returning the Position of a Substring in a String


Given a substring, a source string and a starting position (the default is 1), LOCATE returns the
position of the first occurrence of the substring, starting the search at the starting position. If
the substring is not found, LOCATE returns zero (0). The search is case insensitive.

Syntax: How to Return the Position of a Substring in a String

LOCATE(substr, source [,start])

where:
substr
Alphanumeric

Is the search string.


source
Alphanumeric

Is the source string.


start
Numeric

Is the optional starting position for the search. If omitted, it defaults to 1.

Using Functions 711


LOCATE: Returning the Position of a Substring in a String

Example: Returning the Position of a Substring in a String


The following SQL SELECT statement searches for the character a in FULLNAME, starting at
position 3, and starting at position 1.
SQL
SELECT FULLNAME,
LOCATE('a', FULLNAME, 3) AS 'START AT 3',
LOCATE('a', FULLNAME) AS 'START AT 1'
FROM
WF_RETAIL_CUSTOMER T1
FETCH FIRST 5 ROWS ONLY;
TABLE
HEADING CENTER
"Search for the Character 'a'"
ON TABLE SET PAGE NOLEAD
ON TABLE SET STYLE *
GRID=OFF,$
TYPE=HEADING, STYLE=BOLD, SIZE=16, $
ENDSTYLE
END

The output is shown in the following image.

712
Chapter 27 SQL Miscellaneous Functions
The SQL functions described in this chapter perform a variety of conversions, tests, and
manipulations.

They can be used in SQL Translator requests and, where supported by the DBMS, in
Direct SQL Passthru requests.
In this chapter:

CHR: Returning the ASCII Character Given a Numeric Code

CHR: Returning the ASCII Character Given a Numeric Code


Given a number code as an argument, CHR returns the ASCII character.

Syntax: How to Return the ASCII Character Given a Numeric Code

CHR(number)

where:
number
Numeric

Is the numeric code to be translated to an ASCII character.

Example: Returning the ASCII Character Given a Numeric Code


The following SELECT statement places a colon character between last name and first name.
SQL
SELECT
LAST_NAME AS ' ', CHR(58) AS ' ', FIRST_NAME AS ' '
FROM EMPLOYEE
;
TABLE
ON TABLE SET PAGE NOLEAD
ON TABLE SET STYLE *
GRID=OFF,$
ENDSTYLE
END

Using Functions 713


CHR: Returning the ASCII Character Given a Numeric Code

The output is shown in the following image.

714
Appendix A Creating a Subroutine
You can create custom subroutines to use in addition to the functions provided by
Information Builders. The process of creating a subroutine consists of the following
steps:

Writing a subroutine using any language that supports subroutine calls. Some of the
most common languages are FORTRAN, COBOL, PL/I, Assembler, and C. For details,
see Writing a Subroutine on page 715.

Compiling the subroutine. For details, see Compiling and Storing a Subroutine on page
726.

Storing the subroutine in a separate file; do not include it in the main program. For
details, Compiling and Storing a Subroutine on page 726.

Testing the subroutine. For details, see Testing the Subroutine on page 727.

Note: On z/OS, all subroutines called by WebFOCUS must be fully LE compliant.


In this appendix:

Writing a Subroutine

Compiling and Storing a Subroutine

Testing the Subroutine

Using a Custom Subroutine: The MTHNAM Subroutine

Subroutines Written in REXX

Writing a Subroutine
You can write a subroutine in any language that supports subroutines. If you intend to make
your subroutine available to other users, be sure to document what your subroutine does, what
the arguments are, what formats they have, and in what order they must appear in the
subroutine call.

When you write a subroutine you need to consider the requirements and limits that affect it.
These are:

Naming conventions. For details, see Naming a Subroutine on page 717.

Using Functions 715


Writing a Subroutine

Argument considerations. For details, see Creating Arguments on page 717.

Language considerations. For details, see Language Considerations on page 718.

Programming considerations. For details, see Programming a Subroutine on page 721.

If you write a program named INTCOMP that calculates the amount of money in an account
earning simple interest, the program reads a record, tests if the data is acceptable, and then
calls a subroutine called SIMPLE that computes the amount of money. The program and the
subroutine are stored together in the same file.

The program and the subroutine shown here are written in pseudocode (a method of
representing computer code in a general way):
Begin program INTCOMP.
Execute this loop until end-of-file.
Read next record, fields: PRINCPAL, DATE_PUT, YRRATE.
If PRINCPAL is negative or greater than 100,000,
reject record.
If DATE_PUT is before January 1, 1975, reject record.
If YRRATE is negative or greater than 20%, reject record.
Call subroutine SIMPLE (PRINCPAL, DATE_PUT, YRRATE, TOTAL).
Print PRINCPAL, YEARRATE, TOTAL.
End of loop.
End of program.

Subroutine SIMPLE (AMOUNT, DATE, RATE, RESULT).


Retrieve today's date from the system.
Let NO_DAYS = Days from DATE until today's date.
Let DAY_RATE = RATE / 365 days in a year.
Let RESULT = AMOUNT * (NO_DAYS * DAY_RATE + 1).
End of subroutine.

If you move the SIMPLE subroutine into a file separate from the main program and compile it,
you can call the subroutine. The following report request shows how much money employees
would accrue if they invested salaries in accounts paying 12%:
TABLE FILE EMPLOYEE
PRINT LAST_NAME DAT_INC SALARY AND COMPUTE
INVESTED/D10.2 = SIMPLE(SALARY, DAT_INC, 0.12, INVESTED);
BY EMP_ID
END

Note: The subroutine is designed to return only the amount of the investment, not the current
date because a subroutine can return only a single value each time it is called.

716
A. Creating a Subroutine

Naming a Subroutine
A subroutine name can be up to eight characters long unless the language you are using to
write the subroutine requires a shorter name. A name must start with a letter and can consist
of a combination of letters and/or numbers. Special symbols are not permitted.

Creating Arguments
When you create arguments for a subroutine, you must consider the following issues:

Maximum number of arguments. A subroutine may contain up to 200 arguments. You can
bypass this restriction by creating a subroutine that accepts multiple calls, as described in
Including More Than 200 Arguments in a Subroutine Call on page 723.

Argument types. You can use the same types of arguments in a subroutine as in a
function. For details on these argument types, see Argument Types on page 61.

Input arguments. Input arguments are passed to a subroutine using standard conventions.
Register one points to the list of arguments.

You should not assume that input parameters are stored in contiguous memory.

Output arguments. A subroutine returns only one output argument. This argument must be
the last in the subroutine. You can choose any format for the output argument except in
Dialogue Manager which requires the argument to have the format of the output field.

Internal processing. A subroutine's arguments are processed as follows:

An alphanumeric argument is not changed.

A numeric argument is converted to floating-point double-precision format except in an


operating system RUN command or when storing the output in a variable.

Dialogue Manager requirements. If you are writing a subroutine specifically for Dialogue
Manager, the subroutine may need to perform a conversion. For details on using a
subroutine with Dialogue Manager, see Calling a Function From a Dialogue Manager
Command on page 68.

The lengths of the calling arguments as defined in WebFOCUS must match the lengths of the
corresponding arguments defined in the subroutine.

Using Functions 717


Writing a Subroutine

Any deviation from these rules may result in problems in using the subroutine. Information
Builders recommends that you modify the subroutine to conform to the stated rules and then
link it above the line. In order to load subroutines above the line, the following are the required
link-edit options for compiling and storing the subroutine:

AMODE 31 (Addressing Mode - 31-bit addressing)

RMODE ANY (System can load this routine anywhere)

Language Considerations
When writing a subroutine, you must consider the following language issues:

Language and memory. If you write a subroutine in a language that brings libraries into
memory (for example, FORTRAN and COBOL), the libraries reduce the amount of memory
available to the subroutine.

FORTRAN. TSO supports FORTRAN input/output operations.

COBOL. When writing a subroutine in COBOL:

The subroutine must use the GOBACK command to return to the calling program. STOPRUN
is not supported.

Numeric arguments received from a request must be declared as COMP-2 (double precision
floating point).

The format described in the DEFINE or COMPUTE command determines the format of the
output argument:

WebFOCUS Format Picture

An Xn

I S9(9) COMP

P S9(n)[V9(m)]

where:

(1+n+m)/2 = 8 for small packed numbers.

(1+n+m)/2 = 16 for large packed numbers.

718
A. Creating a Subroutine

WebFOCUS Format Picture

D COMP-2

F COMP-1

PL/I. When writing a subroutine in PL/I:

The RETURNS attribute cannot be used.

The following attribute must be in the procedure (PROC) statement:


OPTIONS (COBOL)

Alphanumeric arguments received from a request must be declared as


CHARACTER (n)

where:

Is the field length as defined by the request. Do not use the VARYING attribute.

Numeric arguments received from a request must be declared as


DECIMAL FLOAT (16)

or
BINARY FLOAT (53)

Using Functions 719


Writing a Subroutine

The format described in the DEFINE or COMPUTE command determines the format of the
output argument:

WebFOCUS Format PL/I Declaration for Output

An CHARACTER (n)

I BINARY FIXED (31)

F DECIMAL FLOAT (6) or BINARY FLOAT (21)

D DECIMAL FLOAT (16) or BINARY FLOAT (53)

P DECIMAL FIXED (15) (for small packed numbers, 8


bytes)

DECIMAL FIXED (31) (for large packed numbers, 16


bytes)

Variables that are not arguments with the STATIC attribute must be declared. This avoids
dynamically allocating these variables every time the subroutine is executed.

C language. When writing a subroutine in C:

Do not return a value with the return statement.

Declare double-precision fields as Double.

The format defined in the DEFINE or COMPUTE command determines the format of the
output argument:

WebFOCUS Format C Declaration for Output

An char *xxx
n

Alphanumeric fields are not terminated with a null byte


and cannot be processed by many of the string
manipulation subroutines in the run-time library.

720
A. Creating a Subroutine

WebFOCUS Format C Declaration for Output

I long
*xxx

F float
*xxx

D double
*xxx

P No equivalent in C.

Programming a Subroutine
Consider the following when planning your programming requirements:

Write the subroutine to include an argument that specifies the output field.

If the subroutine initializes a variable, it must initialize it each time it is executed (serial
reusability).

Since a single request may execute a subroutine numerous times, code the subroutine as
efficiently as possible.

If you create your subroutine in a text file or text library, the subroutine must be 31-bit
addressable.

The last argument, which is normally used for returning the result of the subroutine, can
also be used to provide input from the subroutine.

You can add flexibility to your subroutine by using a programming technique. A programming
technique can be one of the following:

Executing a subroutine at an entry point. An entry point enables you to use one algorithm to
produce different results. For details, see Executing a Subroutine at an Entry Point on page
722.

Creating a subroutine with multiple subroutine calls. Multiple calls enable the subroutine to
process more than 200 arguments. For details, see Including More Than 200 Arguments in
a Subroutine Call on page 723.

Using Functions 721


Writing a Subroutine

Executing a Subroutine at an Entry Point


A subroutine is usually executed starting from the first statement. However, a subroutine can
be executed starting from any place in the code designated as an entry point. This enables a
subroutine to use one basic algorithm to produce different results. For example, the DOWK
subroutine calculates the day of the week on which a date falls. By specifying the subroutine
name DOWK, you obtain a 3-letter abbreviation of the day. If you specify the entry name
DOWKL, you obtain the full name. The calculation, however, is the same.

Each entry point has a name. To execute a subroutine at an entry point, specify the entry point
name in the subroutine call instead of the subroutine name. How you designate an entry point
depends on the language you are using.

Syntax: How to Execute a Subroutine at an Entry Point

{subroutine|entrypoint} (input1, input2,...outfield)

where:
subroutine
Is the name of the subroutine.
entrypoint
Is the name of the entry point to execute the subroutine at.
input1, input2,...
Are the subroutine's arguments.
outfield
Is the field that contains the result, or the format of the output value enclosed in single
quotation marks.

In Dialogue Manager, you must specify the format. In Maintain Data, you must specify the
name of the field.

722
A. Creating a Subroutine

Example: Executing a Subroutine at an Entry Point


The FTOC subroutine, written in pseudocode below, converts Fahrenheit temperature to
Centigrade. The entry point FTOK (designated by the Entry command) sets a flag that causes
273 to be subtracted from the Centigrade temperature to find the Kelvin temperature. The
subroutine is:
Subroutine FTOC (FAREN, CENTI).
Let FLAG = 0.
Go to label X.
Entry FTOK (FAREN, CENTI).
Let FLAG = 1.
Label X.
Let CENTI = (5/9) * (FAREN - 32).
If FLAG = 1 then CENTI = CENTI - 273.
Return.
End of subroutine.

The following is a shorter way to write the subroutine. Notice that the kelv output argument
listed for the entry point is different from the centi output argument listed at the beginning of
the subroutine:
Subroutine FTOC (FAREN, CENTI).
Entry FTOK (FAREN, KELV).
Let CENTI = (5/9) * (FAREN - 32).
KELV = CENTI - 273.
Return.
End of Subroutine.

To obtain the Centigrade temperature, specify the subroutine name FTOC in the subroutine
call. The subroutine processes as:
CENTIGRADE/D6.2 = FTOC (TEMPERATURE, CENTIGRADE);

To obtain the Kelvin temperature, specify the entry name FTOK in the subroutine call. The
subroutine processes as:
KELVIN/D6.2 = FTOK (TEMPERATURE, KELVIN);

Including More Than 200 Arguments in a Subroutine Call


A subroutine can specify a maximum of 200 arguments including the output argument. To
process more than 200 arguments, the subroutine must specify two or more call statements
to pass the arguments to the subroutine.

Using Functions 723


Writing a Subroutine

Use the following technique for writing a subroutine with multiple calls:
1. Divide the subroutine into segments. Each segment receives the arguments passed by one
corresponding subroutine call.
The argument list in the beginning of your subroutine must represent the same number of
arguments in the subroutine call, including a call number argument and an output
argument.

Each call contains the same number of arguments. This is because the argument list in
each call must correspond to the argument list in the beginning of the subroutine. You may
process some of the arguments as dummy arguments if you have an unequal number of
arguments. For example, if you divide 32 arguments among six segments, each segment
processes six arguments; the sixth segment processes two arguments and four dummy
arguments.

Subroutines may require additional arguments as determined by the programmer who


creates the subroutine.
2. Include a statement at the beginning of the subroutine that reads the call number (first
argument) and branches to a corresponding segment. Each segment processes the
arguments from one call. For example, number one branches to the first segment, number
two to the second segment, and so on.
3. Have each segment store the arguments it receives in other variables (which can be
processed by the last segment) or accumulate them in a running total.
End each segment with a command returning control back to the request (RETURN
command).
4. The last segment returns the final output value to the request.
You can also use the entry point technique to write subroutines that process more than 200
arguments. For details, see Executing a Subroutine at an Entry Point on page 722.

724
A. Creating a Subroutine

Syntax: How to Create a Subroutine With Multiple Call Statements

field = subroutine (1, group1, field)


;field = subroutine (2, group2, field);
.
.
.outfield = subroutine (n, groupn, outfield);

where:
field
Is the name of the field that contains the result of the segment or the format of the field
enclosed in single quotation marks. This field must have the same format as outfield.

Do not specify field for the last call statement; use outfield.
subroutine
Is the name of the subroutine up to eight characters long.
n
Is a number that identifies each subroutine call. It must be the first argument in each
subroutine call. The subroutine uses this call number to branch to segments of code.
group1, group2,...
Are lists of input arguments passed by each subroutine call. Each group contains the
same number of arguments, and no more than 26 arguments each.

The final group may contain dummy arguments.


outfield
Is the field that contains the result, or the format of the output value enclosed in single
quotation marks.

In Dialogue Manager, you must specify the format. In Maintain Data, you must specify the
name of the field.

Example: Creating a Subroutine Divided Into Segments


The ADD32 subroutine, written in pseudocode, sums 32 numbers. It is divided into six
segments, each of which adds six numbers from a subroutine call. (The total number of input
arguments is 36 but the last four are dummy arguments.) The sixth segment adds two
arguments to the SUM variable and returns the result. The sixth segment does not process
any values supplied for the four dummy arguments.

Using Functions 725


Compiling and Storing a Subroutine

The subroutine is:


Subroutine ADD32 (NUM, A, B, C, D, E, F, TOTAL).
If NUM is 1 then goto label ONE
else if NUM is 2 then goto label TWO
else if NUM is 3 then goto label THREE
else if NUM is 4 then goto label FOUR
else if NUM is 5 then goto label FIVE
else goto label SIX.

Label ONE.
Let SUM = A + B + C + D + E + F.
Return.

Label TWO
Let SUM = SUM + A + B + C + D + E + F
Return

Label THREE
Let SUM = SUM + A + B + C + D + E + F
Return

Label FOUR
Let SUM = SUM + A + B + C + D + E + F
Return

Label FIVE
Let SUM = SUM + A + B + C + D + E + F
Return

Label SIX
LET TOTAL = SUM + A + B
Return
End of subroutine

To use the ADD32 subroutine, list all six call statements, each call specifying six numbers.
The last four numbers, represented by zeros, are dummy arguments. The DEFINE command
stores the total of the 32 numbers in the SUM32 field.
DEFINE FILE EMPLOYEE
DUMMY/D10 = ADD32 (1, 5, 7, 13, 9, 4, 2, DUMMY);
DUMMY/D10 = ADD32 (2, 5, 16, 2, 9, 28, 3, DUMMY);
DUMMY/D10 = ADD32 (3, 17, 12, 8, 4, 29, 6, DUMMY);
DUMMY/D10 = ADD32 (4, 28, 3, 22, 7, 18, 1, DUMMY);
DUMMY/D10 = ADD32 (5, 8, 19, 7, 25, 15, 4, DUMMY);
SUM32/D10 = ADD32 (6, 3, 27, 0, 0, 0, 0, SUM32);
END

Compiling and Storing a Subroutine


After you write a subroutine, you need to compile and store it. This topic discusses compiling
and storing your subroutine for Windows and z/OS.

726
A. Creating a Subroutine

Compiling and Storing a Subroutine on z/OS


Compile the subroutine, then link-edit it and store the module in a load library. If your
subroutine calls other subroutines, compile and link-edit all the subroutines together in a
single module. Do not store the subroutine in the FUSELIB load library (FUSELIB.LOAD), as it
may be overwritten when your site installs the next release of WebFOCUS.

If the subroutine is written in PL/I, include the following when link-editing the subroutine
ENTRY subroutine

where:
subroutine
Is the name of the subroutine.

Compiling and Storing a Subroutine on UNIX


Run the program GENCPGM, which creates a .DLL file. Then check the location of your dynamic
link functions library file as specified by the IBICPG environment variable, and save the .DLL
file to this location.

Compiling and Storing a Subroutine on Windows


Run the program GENCPGM, which creates a .DLL file. Then check the location of your dynamic
link functions library file as specified by the IBICPG environment variable, and save the .DLL
file to this location.

Testing the Subroutine


After compiling and storing a subroutine, you can test it in a report request. In order to access
the subroutine, you need to issue the ALLOCATE command for z/OS.

If an error occurs during testing, check to see if the error is in the request or in the subroutine.

Procedure: How to Determine the Location of Error


You can determine the location of an error with the following:
1. Write a dummy subroutine that has the same arguments but returns a constant.
2. Execute the request with the dummy subroutine.
If the request executes the dummy subroutine normally, the error is in your subroutine. If the
request still generates an error, the error is in the request.

Using Functions 727


Using a Custom Subroutine: The MTHNAM Subroutine

Using a Custom Subroutine: The MTHNAM Subroutine


This topic discusses the MTHNAM subroutine as an example. The MTHNAM subroutine
converts a number representing a month to the full name of that month. The subroutine
processes as follows:
1. Receives the input argument from the request as a double-precision number.
2. Adds .000001 to the number which compensates for rounding errors. Rounding errors can
occur since floating-point numbers are approximations and may be inaccurate in the last
significant digit.
3. Moves the number into an integer field.
4. If the number is less than one or greater than 12, it changes the number to 13.
5. Defines a list containing the names of months and an error message for the number 13.
6. Sets the index of the list equal to the number in the integer field. It then places the
corresponding array element into the output argument. If the number is 13, the argument
contains the error message.
7. Returns the result as an output field.

Writing the MTHNAM Subroutine


The MTHNAM subroutine can be written in FORTRAN, COBOL, PL/I, BAL Assembler, and C.

Reference: MTHNAM Subroutine Written in FORTRAN


This is a FORTRAN version of the MTHNAM subroutine where:
MTH

Is the double-precision number in the input argument.


MONTH

Is the name of the month. Since the character string 'September' contains nine letters,
MONTH is a three element array. The subroutine passes the three elements back to your
application which concatenates them into one field.
A

Is a two dimensional, 13 by 3 array, containing the names of the months. The last three
elements contain the error message.
IMTH

Is the integer representing the month.

728
A. Creating a Subroutine

The subroutine is:

SUBROUTINE MTHNAM (MTH,MONTH)


REAL*8 MTH
INTEGER*4 MONTH(3),A(13,3),IMTH
DATA
+ A( 1,1)/'JANU'/, A( 1,2)/'ARY '/, A( 1,3)/' '/,
+ A( 2,1)/'FEBR'/, A( 2,2)/'UARY'/, A( 2,3)/' '/,
+ A( 3,1)/'MARC'/, A( 3,2)/'H '/, A( 3,3)/' '/,
+ A( 4,1)/'APRI'/, A( 4,2)/'L '/, A( 4,3)/' '/,
+ A( 5,1)/'MAY '/, A( 5,2)/' '/, A( 5,3)/' '/,
+ A( 6,1)/'JUNE'/, A( 6,2)/' '/, A( 6,3)/' '/,
+ A( 7,1)/'JULY'/, A( 7,2)/' '/, A( 7,3)/' '/,
+ A( 8,1)/'AUGU'/, A( 8,2)/'ST '/, A( 8,3)/' '/,
+ A( 9,1)/'SEPT'/, A( 9,2)/'EMBE'/, A( 9,3)/'R '/,
+ A(10,1)/'OCTO'/, A(10,2)/'BER '/, A(10,3)/' '/,
+ A(11,1)/'NOVE'/, A(11,2)/'MBER'/, A(11,3)/' '/,
+ A(12,1)/'DECE'/, A(12,2)/'MBER'/, A(12,3)/' '/,
+ A(13,1)/'**ER'/, A(13,2)/'ROR*'/, A(13,3)/'* '/
IMTH=MTH+0.000001
IF (IMTH .LT. 1 .OR. IMTH .GT. 12) IMTH=13
DO 1 I=1,3
1 MONTH(I)=A(IMTH,I)
RETURN
END

Reference: MTHNAM Subroutine Written in COBOL


This is a COBOL version of the MTHNAM subroutine where:
MONTH-TABLE

Is a field containing the names of the months and the error message.
MLINE

Is a 13-element array that redefines the MONTH-TABLE field. Each element (called A)
contains the name of a month; the last element contains the error message.
A

Is one element in the MLINE array.


IX

Is an integer field that indexes MLINE.


IMTH

Is the integer representing the month.


MTH

Is the double-precision number in the input argument.

Using Functions 729


Using a Custom Subroutine: The MTHNAM Subroutine

MONTH

Is the name of the month corresponding to the integer in IMTH.

The subroutine is:


IDENTIFICATION DIVISION.
PROGRAM-ID. MTHNAM.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
SOURCE-COMPUTER. IBM-370.
OBJECT-COMPUTER. IBM-370.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 MONTH-TABLE.
05 FILLER PIC X(9) VALUE 'JANUARY '.
05 FILLER PIC X(9) VALUE 'FEBRUARY '.
05 FILLER PIC X(9) VALUE 'MARCH '.
05 FILLER PIC X(9) VALUE 'APRIL '.
05 FILLER PIC X(9) VALUE 'MAY '.
05 FILLER PIC X(9) VALUE 'JUNE '.
05 FILLER PIC X(9) VALUE 'JULY '.
05 FILLER PIC X(9) VALUE 'AUGUST '.
05 FILLER PIC X(9) VALUE 'SEPTEMBER'.
05 FILLER PIC X(9) VALUE 'OCTOBER '.
05 FILLER PIC X(9) VALUE 'NOVEMBER '.
05 FILLER PIC X(9) VALUE 'DECEMBER '.
05 FILLER PIC X(9) VALUE '**ERROR**'.
01 MLIST REDEFINES MONTH-TABLE.
05 MLINE OCCURS 13 TIMES INDEXED BY IX.
10 A PIC X(9).
01 IMTH PIC S9(5) COMP.
LINKAGE SECTION.
01 MTH COMP-2.
01 MONTH PIC X(9).
PROCEDURE DIVISION USING MTH, MONTH.
BEG-1.
ADD 0.000001 TO MTH.
MOVE MTH TO IMTH.
IF IMTH < +1 OR > 12
SET IX TO +13
ELSE
SET IX TO IMTH.
MOVE A (IX) TO MONTH.
GOBACK.

Reference: MTHNAM Subroutine Written in PL/I


This is a PL/I version of the MTHNAM subroutine where:
MTHNUM

Is the double-precision number in the input argument.

730
A. Creating a Subroutine

FULLMTH

Is the name of the month corresponding to the integer in MONTHNUM.


MONTHNUM

Is the integer representing the month.


MONTH_TABLE

Is a 13-element array containing the names of the months. The last element contains the
error message.

The subroutine is:


MTHNAM: PROC(MTHNUM,FULLMTH) OPTIONS(COBOL);
DECLARE MTHNUM DECIMAL FLOAT (16) ;
DECLARE FULLMTH CHARACTER (9) ;
DECLARE MONTHNUM FIXED BIN (15,0) STATIC ;
DECLARE MONTH_TABLE(13) CHARACTER (9) STATIC
INIT ('JANUARY',
'FEBRUARY',
'MARCH',
'APRIL',
'MAY',
'JUNE',
'JULY',
'AUGUST',
'SEPTEMBER',
'OCTOBER',
'NOVEMBER',
'DECEMBER',
'**ERROR**') ;
MONTHNUM = MTHNUM + 0.00001 ;
IF MONTHNUM < 1 | MONTHNUM > 12 THEN
MONTHNUM = 13 ; FULLMTH = MONTH_TABLE(MONTHNUM) ;
RETURN;
END MTHNAM;

Using Functions 731


Using a Custom Subroutine: The MTHNAM Subroutine

Reference: MTHNAM Subroutine Written in BAL Assembler


This is a BAL Assembler version of the MTHNAM subroutine:
* =====================================================================
*
* A SIMPLE MAIN ASSEMBLE ROUTINE THAT CALLS THE LE CALLABLE SERVICES
*
* =====================================================================
MTHNAM CEEENTRY PPA=MAINPPA,AUTO=WORKSIZE,MAIN=NO
USING WORKAREA,13
*
L 3,0(0,1) LOAD ADDR OF FIRST ARG INTO R3
LD 4,=D'0.0' CLEAR OUT FPR4 AND FPR5
LE 6,0(0,3) FP NUMBER IN FPR6
LPER 4,6 ABS VALUE IN FPR4
AW 4,=D'0.00001' ADD ROUNDING CONSTANT
AW 4,DZERO SHIFT OUT FRACTION
STD 4,FPNUM MOVE TO MEMORY
L 2,FPNUM+4 INTEGER PART IN R2
TM 0(3),B'10000000' CHECK SIGN OF ORIGINAL NO
BNO POS BRANCH IF POSITIVE
LCR 2,2 COMPLEMENT IF NEGATIVE
*
POS LR 3,2 COPY MONTH NUMBER INTO R3
C 2,=F'0' IS IT ZERO OR LESS?
BNP INVALID YES. SO INVALID
C 2,=F'12' IS IT GREATER THAN 12?
BNP VALID NO. SO VALID
INVALID LA 3,13(0,0) SET R3 TO POINT TO ITEM 13 (ERROR)
*
VALID SR 2,2 CLEAR OUT R2
M 2,=F'9' MULTIPLY BY SHIFT IN TABLE
*
LA 6,MTH(3) GET ADDR OF ITEM IN R6

732
A. Creating a Subroutine

L 4,4(0,1) GET ADDR OF SECOND ARG IN R4


MVC 0(9,4),0(6) MOVE IN TEXT
*
* TERMINATE THE CEE ENVIRONMENT AND RETURN TO THE CALLER
*
CEETERM RC=0
* ====================================================================
* CONSTANTS
* ====================================================================
DS 0D ALIGNMENT
FPNUM DS D FLOATING POINT NUMBER
DZERO DC X'4E00000000000000' SHIFT CONSTANT
MTH DC CL9'DUMMYITEM' MONTH TABLE
DC CL9'JANUARY'
DC CL9'FEBRUARY'
DC CL9'MARCH'
DC CL9'APRIL'
DC CL9'MAY'
DC CL9'JUNE'
DC CL9'JULY'
DC CL9'AUGUST'
DC CL9'SEPTEMBER'
DC CL9'OCTOBER'
DC CL9'NOVEMBER'
DC CL9'DECEMBER'
DC CL9'**ERROR**'
*
MAINPPA CEEPPA CONSTANTS DESCRIBING THE CODE BLOCK
* ====================================================================
* THE WORKAREA AND DSA
* ====================================================================
WORKAREA DSECT
ORG *+CEEDSASZ LEAVE SPACE FOR THE DSA FIXED PART
PLIST DS 0D
PARM1 DS A
PARM2 DS A
PARM3 DS A
PARM4 DS A
PARM5 DS A
*
FOCPARM1 DS F SAVE FIRST PARAMETER PASSED
FOCPARM2 DS F SAVE SECOND PARAMETER PASSED
*
DS 0D
WORKSIZE EQU *-WORKAREA
CEEDSA MAPPING OF THE DYNAMIC SAVE AREA
CEECAA MAPPING OF THE COMMON ANCHOR AREA
*
END MTHNAM NOMINATE MTHNAM AS THE ENTRY POINT
/*

Using Functions 733


Using a Custom Subroutine: The MTHNAM Subroutine

Reference: MTHNAM Subroutine Written in C


This is a C language version of the MTHNAM subroutine:
void mthnam(double *,char *);
void mthnam(mth,month)
double *mth;
char *month;
{
char *nmonth[13] = {"January ",
"February ",
"March ",
"April ",
"May ",
"June ",
"July ",
"August ",
"September",
"October ",
"November ",
"December ",
"**Error**"};
int imth, loop;
imth = *mth + .00001;
imth = (imth < 1 || imth > 12 ? 13 : imth);
for (loop=0;loop < 9;loop++)
month[loop] = nmonth[imth-1][loop];
}

Calling the MTHNAM Subroutine From a Request


You can call the MTHNAM subroutine from a report request.

Example: Calling the MTHNAM Subroutine


The DEFINE command extracts the month portion of the pay date. The MTHNAM subroutine
then converts it into the full name of the month, and stores the name in the PAY_MONTH field.
The report request prints the monthly pay of Alfred Stevens.
DEFINE FILE EMPLOYEE
MONTH_NUM/M = PAY_DATE;
PAY_MONTH/A12 = MTHNAM (MONTH_NUM, PAY_MONTH);
END
TABLE FILE EMPLOYEE
PRINT PAY_MONTH GROSS
BY EMP_ID BY FIRST NAME BY LAST_NAME
BY PAY_DATE
IF LN IS STEVENS
END

734
A. Creating a Subroutine

The output is:


EMP_ID FIRST NAME LAST_NAME PAY_DATE PAY_MONTH GROSS
------- ---------- --------- -------- --------- -------
071382660 ALFRED STEVENS 81/11/30 NOVEMBER $833.33
81/12/31 DECEMBER $833.33
82/01/29 JANUARY $916.67
82/02/26 FEBRUARY $916.67
82/03/31 MARCH $916.67
82/04/30 APRIL $916.67
82/05/28 MAY $916.67
82/06/30 JUNE $916.67
82/07/30 JULY $916.67
82/08/31 AUGUST $916.67

Subroutines Written in REXX


A request can call a subroutine coded in REXX. These subroutines, also called FUSREXX
macros, provide a 4GL option to the languages supported for user-written subroutines.

REXX subroutines are supported in the z/OS environment. A REXX subroutine contains REXX
source code. Compiled REXX code is not supported.

A REXX subroutine contains REXX source code. Compiled REXX code is not supported.

REXX subroutines are not necessarily the same in all operating environments. Therefore, some
of the examples may use REXX functions that are not available in your environment.

Because of CPU requirements, the use of REXX subroutines in large production jobs should be
monitored carefully.

For more information on REXX subroutines, see your REXX documentation.

Reference: Storing and Searching for a REXX Subroutine


To store a REXX subroutine, DDNAME FUSREXX must be allocated to a PDS. This library is
searched before other z/OS libraries.

The search order for a REXX subroutine is:


1. FUSREXX.
2. Standard z/OS search order.

Using Functions 735


Subroutines Written in REXX

Syntax: How to Call a REXX Subroutine

DEFINE FILE filename


fieldname/{An|In} = subname(inlen1, inparm1, ..., outlen, outparm);
END

or
{DEFINE|COMPUTE} fieldname/{An|In} = subname(inlen1, inparm1, ...,
outlen, outparm);

or
-SET &var = subname(inlen1, inparm1, ..., outlen, outparm);

where:
fieldname
Is the field that contains the result.
An, In

Is the format of the field that contains the result.


subname
Is the name of the REXX subroutine.
inlen1, inparm1 ...
Are the input parameters. Each parameter consists of a length and an alphanumeric
parameter value. You can supply the value, the name of an alphanumeric field that
contains the value, or an expression that returns the value. Up to 13 input parameter pairs
are supported. Each parameter value can be up to 256 bytes long.

Dialogue Manager converts numeric arguments to floating-point double-precision format.


Therefore, you can only pass alphanumeric input parameters to a REXX subroutine using -
SET.
outlen, outparm
Is the output parameter pair, consisting of a length and a result. In most cases, the result
should be alphanumeric, but integer results are also supported. The result can be a field
or a Dialogue Manager variable that contains the value, or the format of the value
enclosed in single quotation marks. The return value can be a minimum of one byte long
and a maximum (for an alphanumeric value) of 256 bytes.

Note: If the value returned is an integer, outlen must be 4 because WebFOCUS reserves
four bytes for integer fields.

736
A. Creating a Subroutine

&var

Is the name of the Dialogue Manager variable that contains the result.

Example: Returning the Day of the Week


The REXX subroutine DOW returns the day of the week corresponding to the date an employee
was hired. The routine contains one input parameter pair and one return field pair.

DEFINE FILE EMPLOYEE


1. AHDT/A6 = EDIT(HIRE_DATE) ;
2. DAY_OF_WEEK/A9 WITH AHDT = DOW(6, AHDT, 9, DAY_OF_WEEK);
END

TABLE FILE EMPLOYEE


PRINT LAST_NAME HIRE_DATE DAY_OF_WEEK
END

The procedure processes as follows:

1. The EDIT function converts HIRE_DATE to alphanumeric format and stores the result in a
field with the format A6.
2. The result is stored in the DAY_OF_THE_WEEK field, and can be up to nine bytes long.

The output is:


LAST_NAME HIRE_DATE DAY_OF_WEEK
--------- --------- -----------
STEVENS 80/06/02 Monday
SMITH 81/07/01 Wednesday
JONES 82/05/01 Saturday
SMITH 82/01/04 Monday
BANNING 82/08/01 Sunday
IRVING 82/01/04 Monday
ROMANS 82/07/01 Thursday
MCCOY 81/07/01 Wednesday
BLACKWOOD 82/04/01 Thursday
MCKNIGHT 82/02/02 Tuesday
GREENSPAN 82/04/01 Thursday
CROSS 81/11/02 Monday
The REXX subroutine appears below. It reads the input date, reformats it to MM/DD/YY
format, and returns the day of the week using a REXX DATE call.

/* DOW routine. Return WEEKDAY from YYMMDD format date */


Arg ymd .
Return Date('W',Translate('34/56/12',ymd,'123456'),'U')

Using Functions 737


Subroutines Written in REXX

Example: Passing Multiple Arguments to a REXX Subroutine


The REXX subroutine INTEREST has four input parameters.
DEFINE FILE EMPLOYEE
1. AHDT/A6 = EDIT(HIRE_DATE);
2. ACSAL/A12 = EDIT(CURR_SAL);
3. DCSAL/D12.2 = CURR_SAL;
4. PV/A12 = INTEREST(6, AHDT, 6, '&YMD', 3, '6.5', 12, ACSAL, 12, PV);
END

TABLE FILE EMPLOYEE


PRINT LAST_NAME FIRST_NAME HIRE_DATE DCSAL PV
END

The procedure processes as follows:

1. EDIT converts HIRE_DATE to alphanumeric format and stores the result in AHDT.
2. EDIT converts CURR_SAL to alphanumeric format and stores the result in ACSAL.
3. CURR_SAL is converted to a floating-point double-precision field that includes commas, and
the result is stored in DCSAL.
4. The second input field is six bytes long. Data is passed as a character variable &YMD in
YYMMDD format.
The third input field is a character value of 6.5, which is three bytes long to account for the
decimal point in the character string.

The fourth input field is 12 bytes long. This passes the character field ACSAL.

The return field is up to 12 bytes long and is named PV.

The output is:


LAST_NAME FIRST_NAME HIRE_DATE DCSAL PV
--------- ---------- --------- ----- --
STEVENS ALFRED 80/06/02 11,000.00 14055.14
SMITH MARY 81/07/01 13,200.00 15939.99
JONES DIANE 82/05/01 18,480.00 21315.54
SMITH RICHARD 82/01/04 9,500.00 11155.60
BANNING JOHN 82/08/01 29,700.00 33770.53
IRVING JOAN 82/01/04 26,862.00 31543.35
ROMANS ANTHONY 82/07/01 21,120.00 24131.19
MCCOY JOHN 81/07/01 18,480.00 22315.99
BLACKWOOD ROSEMARIE 82/04/01 21,780.00 25238.25
MCKNIGHT ROGER 82/02/02 16,100.00 18822.66
GREENSPAN MARY 82/04/01 9,000.00 10429.03
CROSS BARBARA 81/11/02 27,062.00 32081.82

738
A. Creating a Subroutine

The REXX subroutine appears below. The REXX Format command is used to format the return
value.
/* Simple INTEREST program. dates are yymmdd format */
Arg start_date,now_date,percent,open_balance, .

begin = Date('B',Translate('34/56/12',start_date,'123456'),'U')
stop = Date('B',Translate('34/56/12',now_date,'123456'),'U')
valnow = open_balance * (((stop - begin) * (percent / 100)) / 365)

Return Format(valnow,9,2)

Example: Accepting Multiple Tokens in a Parameter


A REXX subroutine can accept multiple tokens in a parameter. The following procedure passes
employee information (PAY_DATE and MO_PAY) as separate tokens in the first parameter. It
passes three input parameters and one return field.
DEFINE FILE EMPLOYEE
1. COMPID/A256 = FN | ' ' | LN | ' ' | DPT | ' ' | EID ;
2. APD/A6 = EDIT(PAY_DATE);
3. APAY/A12 = EDIT(MO_PAY);
4. OK4RAISE/A1 = OK4RAISE(256, COMPID, 6, APD, 12, APAY, 1, OK4RAISE);
END

TABLE FILE EMPLOYEE


PRINT EMP_ID FIRST_NAME LAST_NAME DEPARTMENT
IF OK4RAISE EQ '1'
END

The procedure processes as follows:

1. COMPID is the concatenation of several character fields passed as the first parameter and
stored in a field with the format A256. Each of the other parameters is a single argument.
2. EDIT converts PAY_DATE to alphanumeric format.
3. EDIT converts MO_PAY to alphanumeric format.
4. OK4RAISE executes, and the result is stored in OK4RAISE.

The output is:


EMP_ID FIRST_NAME LAST_NAME DEPARTMENT
------ ---------- --------- ----------
071382660 ALFRED STEVENS PRODUCTION

Using Functions 739


Subroutines Written in REXX

The REXX subroutine appears below. Commas separate FUSREXX parameters. The ARG
command specifies multiple variable names before the first comma and, therefore, separates
the first FUSREXX parameter into separate REXX variables, using blanks as delimiters between
the variables.
/* OK4RAISE routine. Parse separate tokens in the 1st parm, */
/* then more parms */

Arg fname lname dept empid, pay_date, gross_pay, .

If dept = 'PRODUCTION' & pay_date < '820000'


Then retvalue = '1'
Else retvalue = '0'

Return retvalue

REXX subroutines should use the REXX RETURN subroutine to return data. REXX EXIT is
acceptable, but is generally used to end an EXEC, not a FUNCTION.

Correct Not as Clear


/* Some FUSREXX function */ /* Another FUSREXX function */
Arg input Arg input
some rexx process .. some rexx process ...
Return data_to_WebFOCUS Exit 0

Formats and REXX Subroutines


A REXX subroutine requires input data to be in alphanumeric format. Most output is returned in
alphanumeric format. If the format of an input argument is numeric, use the EDIT or FTOA
functions to convert the argument to alphanumeric. You can then use the EDIT or ATODBL
functions to convert the output back to numeric.

The output length in the subroutine call must be four. Character variables cannot be more than
256 bytes. This limit also applies to REXX subroutines. FUSREXX routines return variable
length data. For this reason, you must supply the length of the input arguments and the
maximum length of the output data.

A REXX subroutine does not require any input parameters, but requires one return parameter,
which must return at least one byte of data. It is possible for a REXX subroutine not to need
input, such as a function that returns USERID.

740
A. Creating a Subroutine

A REXX subroutine does not support WebFOCUS date input arguments. When working with
dates you can do one of the following:

Pass an alphanumeric field with date display options and have the subroutine return a date
value.

Date fields contain the integer number of days since the base date 12/31/1900. REXX
has a date function that can accept and return several types of date formats, including one
called Base format ('B') that contains the number of days since the REXX base date
01/01/0001. You must account for the difference, in number of days, between the
WebFOCUS base date and the REXX base date and convert the result to integer.

Pass a date value converted to alphanumeric format. You must account for the difference
in base dates for both the input and output arguments.

Example: Returning a Result in Alphanumeric Format


The NUMCNT subroutine returns the number of copies of each classic movie in alphanumeric
format. It passes one input parameter and one return field.
TABLE FILE MOVIES
PRINT TITLE AND COMPUTE
1. ACOPIES/A3 = EDIT(COPIES); AS 'COPIES'
AND COMPUTE
2. TXTCOPIES/A8 = NUMCNT(3, ACOPIES, 8, TXTCOPIES);
WHERE CATEGORY EQ 'CLASSIC'
END

The procedure processes as follows:

1. The EDIT field converts COPIES to alphanumeric format, and stores the result in ACOPIES.
2. The result is stored in an 8-byte alphanumeric field TXTCOPIES.

The output is:


TITLE COPIES TXTCOPIES
----- ------ ---------
EAST OF EDEN 001 One
CITIZEN KANE 003 Three
CYRANO DE BERGERAC 001 One
MARTY 001 One
MALTESE FALCON, THE 002 Two
GONE WITH THE WIND 003 Three
ON THE WATERFRONT 002 Two
MUTINY ON THE BOUNTY 002 Two
PHILADELPHIA STORY, THE 002 Two
CAT ON A HOT TIN ROOF 002 Two
CASABLANCA 002 Two

Using Functions 741


Subroutines Written in REXX

The subroutine is:


/* NUMCNT routine. */
/* Pass a number from 0 to 10 and return a character value */
Arg numbr .
data = 'Zero One Two Three Four Five Six Seven Eight Nine Ten'
numbr = numbr + 1 /* so 0 equals 1 element in array */
Return Word(data,numbr)

Example: Returning a Result in Integer Format


In the following example, the NUMDAYS subroutine finds the number of days between
HIRE_DATE and DAT_INC and returns the result in integer format.
DEFINE FILE EMPLOYEE
1. AHDT/A6 = EDIT(HIRE_DATE);
2. ADI/A6 = EDIT(DAT_INC);
3. BETWEEN/I6 = NUMDAYS(6, AHDT, 6, ADI, 4, 'I6') ;
END

TABLE FILE EMPLOYEE


PRINT LAST_NAME HIRE_DATE DAT_INC BETWEEN
IF BETWEEN NE 0
END

The procedure processes as follows:

1. EDIT converts HIRE_DATE to alphanumeric format and stores the result in AHDT.
2. EDIT converts DAT_INC to alphanumeric format and stores the result in ADI.
3. NUMDAYS finds the number of days between AHDT and ADI and stores the result in integer
format.

The output is:


LAST_NAME HIRE_DATE DAT_INC BETWEEN
--------- --------- ------- -------
STEVENS 80/06/02 82/01/01 578
STEVENS 80/06/02 81/01/01 213
SMITH 81/07/01 82/01/01 184
JONES 82/05/01 82/06/01 31
SMITH 82/01/04 82/05/14 130
IRVING 82/01/04 82/05/14 130
MCCOY 81/07/01 82/01/01 184
MCKNIGHT 82/02/02 82/05/14 101
GREENSPAN 82/04/01 82/06/11 71
CROSS 81/11/02 82/04/09 158

742
A. Creating a Subroutine

The subroutine appears below. The return value is converted from REXX character to HEX and
formatted to be four bytes long.
/* NUMDAYS routine. */
/* Return number of days between 2 dates in yymmdd format */
/* The value returned will be in hex format */

Arg first,second .

base1 = Date('B',Translate('34/56/12',first,'123456'),'U')
base2 = Date('B',Translate('34/56/12',second,'123456'),'U')

Return D2C(base2 - base1,4)

Example: Passing a Date Value as an Alphanumeric Field With Date Options


In the following example, a date is used by passing an alphanumeric field with date options to
the DATEREX1 subroutine. DATEREX1 takes two input arguments: an alphanumeric date in
A8YYMD format and a number of days in character format. It returns a smart date in YYMD
format that represents the input date plus the number of days. The format A8YYMD
corresponds to the REXX Standard format ('S').

The number 693959 represents the difference, in number of days, between the WebFOCUS
base date and the REXX base date:
/* REXX DATEREX1 routine. Add indate (format A8YYMD) to days */
Arg indate, days .
Return D2C(Date('B',indate,'S')+ days - 693959, 4)

The following request uses the DATEREX1 macro to calculate the date that is 365 days from
the hire date of each employee. The input arguments are the hire date and the number of days
to add. Because HIRE_DATE is in I6YMD format, it must be converted to A8YYMD before being
passed to the macro:
TABLE FILE EMPLOYEE
PRINT LAST_NAME FIRST_NAME HIRE_DATE
AND COMPUTE
ADATE/YYMD = HIRE_DATE; NOPRINT
AND COMPUTE
INDATE/A8YYMD= ADATE; NOPRINT
AND COMPUTE
NEXT_DATE/YYMD = DATEREX1(8, INDATE, 3, '365', 4, NEXT_DATE);
BY LAST_NAME NOPRINT
END

Using Functions 743


Subroutines Written in REXX

The output is:


LAST_NAME FIRST_NAME HIRE_DATE NEXT_DATE
--------- ---------- --------- ---------
BANNING JOHN 82/08/01 1983/08/01
BLACKWOOD ROSEMARIE 82/04/01 1983/04/01
CROSS BARBARA 81/11/02 1982/11/02
GREENSPAN MARY 82/04/01 1983/04/01
IRVING JOAN 82/01/04 1983/01/04
JONES DIANE 82/05/01 1983/05/01
MCCOY JOHN 81/07/01 1982/07/01
MCKNIGHT ROGER 82/02/02 1983/02/02
ROMANS ANTHONY 82/07/01 1983/07/01
SMITH MARY 81/07/01 1982/07/01
SMITH RICHARD 82/01/04 1983/01/04
STEVENS ALFRED 80/06/02 1981/06/02

Example: Passing a Date as a Date Converted to Alphanumeric Format


In the following example, a date is passed to the subroutine as a smart date converted to
alphanumeric format. The DATEREX2 subroutine takes two input arguments: an alphanumeric
number of days that represents a smart date, and a number of days to add. It returns a smart
date in YYMD format that represents the input date plus the number of days. Both the input
date and output date are in REXX base date ('B') format.

The number 693959 represents the difference, in number of days, between the WebFOCUS
base date and the REXX base date:
/* REXX DATEREX2 routine. Add indate (original format YYMD) to days */
Arg indate, days .
Return D2C(Date('B',indate+693959,'B') + days - 693959, 4)

The following request uses DATEREX2 to calculate the date that is 365 days from the hire date
of each employee. The input arguments are the hire date and the number of days to add.
Because HIRE_DATE is in I6YMD format, it must be converted to an alphanumeric number of
days before being passed to the macro:
TABLE FILE EMPLOYEE
PRINT LAST_NAME FIRST_NAME HIRE_DATE
AND COMPUTE
ADATE/YYMD = HIRE_DATE; NOPRINT
AND COMPUTE
INDATE/A8 = EDIT(ADATE); NOPRINT
AND COMPUTE
NEXT_DATE/YYMD = DATEREX2(8,INDATE,3,'365',4,NEXT_DATE);
BY LAST_NAME NOPRINT
END

744
A. Creating a Subroutine

The output is:


LAST_NAME FIRST_NAME HIRE_DATE NEXT_DATE
--------- ---------- --------- ---------
BANNING JOHN 82/08/01 1983/08/01
BLACKWOOD ROSEMARIE 82/04/01 1983/04/01
CROSS BARBARA 81/11/02 1982/11/02
GREENSPAN MARY 82/04/01 1983/04/01
IRVING JOAN 82/01/04 1983/01/04
JONES DIANE 82/05/01 1983/05/01
MCCOY JOHN 81/07/01 1982/07/01
MCKNIGHT ROGER 82/02/02 1983/02/02
ROMANS ANTHONY 82/07/01 1983/07/01
SMITH MARY 81/07/01 1982/07/01
SMITH RICHARD 82/01/04 1983/01/04
STEVENS ALFRED 80/06/02 1981/06/02

Using Functions 745


Subroutines Written in REXX

746
Appendix B ASCII and EBCDIC Codes
The table in this appendix lists ASCII and EBCDIC codes for printable and non-printable
characters.
In this appendix:

ASCII and EBCDIC Code Chart

ASCII and EBCDIC Code Chart


This chart shows the standard ASCII characters in numerical order with the corresponding
decimal and hexadecimal values.

Decimal Hex ASCII EBCDIC

0 00 NUL null NUL null

1 01 SOH start of heading SOH start of heading

2 02 STX start of text STX start of text

3 03 ETX end of text ETX end of text

4 04 EOT end of transmission SEL select

5 05 ENQ enquiry HT horizontal tab

6 06 ACK acknowledge RNL required new-line

7 07 BEL bell DEL delete

8 08 BS backspace GE graphic escape

9 09 HT horizontal tab SPS superscript

10 0A LF line feed RPT repeat

11 0B VT vertical tab VT vertical tab

12 0C FF form feed FF form feed

Using Functions 747


ASCII and EBCDIC Code Chart

Decimal Hex ASCII EBCDIC

13 0D CR carriage return CR carriage return

14 0E SO shift out SO shift out

15 0F SI shift in SI shift in

16 10 DLE data link escape DLE data link escape

17 11 DC1 device control 1 DC1 device control 1

18 12 DC2 device control 2 DC2 device control 2

19 13 DC3 device control 3 DC3 device control 3

20 14 DC4 device control 4 RES/ restore/enable


ENP presentation

21 15 NAK negative acknowledge NL new-line

22 16 SYN synchronous idle BS backspace

23 17 ETB end of transmission POC program-operator


block communications

24 18 CAN cancel CAN cancel

25 19 EM end of medium EM end of medium

26 1A SUB substitute UBS unit backspace

27 1B ESC escape CU1 customer use 1

28 1C FS file separator IFS interchange file


separator

29 1D GS group separator IGS interchange group


separator

30 1E RS record separator IRS interchange record


separator

748
B. ASCII and EBCDIC Codes

Decimal Hex ASCII EBCDIC

31 1F US unit separator IUS/ interchange unit


ITB separator /
intermediate
transmission block

32 20 SP space DS digit select

33 21 ! exclamation point SOS start of significance

34 22 " straight double FS field separator


quotation mark

35 23 # number sign WUS word underscore

36 24 $ dollar sign BYP/ bypass/inhibit


INP presentation

37 25 % percent sign LF line feed

38 26 & ampersand ETB end of transmission


block

39 27 ' apostrophe ESC escape

40 28 ( left parenthesis SA set attribute

41 29 ) right parenthesis

42 2A * asterisk SM/ set model switch


SW

43 2B + addition sign CSP control sequence prefix

44 2C , comma MFA modify field attribute

45 2D - subtraction sign ENQ enquiry

46 2E . period ACK acknowledge

47 2F / right slash BEL bell

48 30 0 0

Using Functions 749


ASCII and EBCDIC Code Chart

Decimal Hex ASCII EBCDIC

49 31 1 1

50 32 2 2 SYN synchronous idle

51 33 3 3 IR index return

52 34 4 4 PP presentation position

53 35 5 5 TRN

54 36 6 6 NBS numeric backspace

55 37 7 7 EOT end of transmission

56 38 8 8 SBS subscript

57 39 9 9 IT indent tab

58 3A : colon RFF required form feed

59 3B ; semicolon CU3 customer use 3

60 3C < less-than DC4 device control 4

61 3D = equal NAK negative acknowledge

62 3E > greater-than

63 3F ? question mark SUB substitute

64 40 @ at symbol SP space

65 41 A A

66 42 B B

67 43 C C

68 44 D D

69 45 E E

70 46 F F

750
B. ASCII and EBCDIC Codes

Decimal Hex ASCII EBCDIC

71 47 G G

72 48 H H

73 49 I I

74 4A J J ¢ cent

75 4B K K . period

76 4C L L < less-than

77 4D M M ( left parenthesis

78 4E N N + addition sign

79 4F O O | logical or

80 50 P P & ampersand

81 51 Q Q

82 52 R R

83 53 S S

84 54 T T

85 55 U U

86 56 V V

87 57 W W

88 58 X X

89 59 Y Y

90 5A Z Z ! exclamation mark

91 5B [ left bracket $ dollar sign

92 5C \ left slant * asterisk

Using Functions 751


ASCII and EBCDIC Code Chart

Decimal Hex ASCII EBCDIC

93 5D ] right bracket ) right parenthesis

94 5E ^ hat, circumflex ; semicolon

95 5F _ underscore ¬ logical not

96 60 ` grave - subtraction sign

97 61 a a / right slash

98 62 b b

99 63 c c

100 64 d d

101 65 e e

102 66 f f

103 67 g g

104 68 h h

105 69 i i

106 6A j j | split vertical bar

107 6B k k , comma

108 6C l l % percent sign

109 6D m m _ underscore

110 6E n n > greater-than

111 6F o o ? question mark

112 70 p p

113 71 q q

114 72 r r

752
B. ASCII and EBCDIC Codes

Decimal Hex ASCII EBCDIC

115 73 s s

116 74 t t

117 75 u u

118 76 v v

119 77 w w

120 78 x x

121 79 y y ˋ grave

122 7A z z : colon

123 7B { opening brace # number sign

124 7C | vertical line @ at symbol

125 7D } closing brace ' apostrophe

126 7E ~ tilde = equal

127 7F " straight double


quotation mark

128 80

129 81 a a

130 82 b b

131 83 c c

132 84 d d

133 85 e e

134 86 f f

135 87 g g

136 88 h h

Using Functions 753


ASCII and EBCDIC Code Chart

Decimal Hex ASCII EBCDIC

137 89 i i

138 8A

139 8B

140 8C

141 8D

142 8E

143 8F

144 90

145 91 j j

146 92 k k

147 93 l l

148 94 m m

149 95 n n

150 96 o o

151 97 p p

152 98 q q

153 99 r r

154 9A

155 9B

156 9C

157 9D

158 9E

754
B. ASCII and EBCDIC Codes

Decimal Hex ASCII EBCDIC

159 9F

160 A0

161 A1 ~ similar, tilde

162 A2 s s

163 30 t t

164 A4 u u

165 A5 v v

166 A6 w w

167 A7 x x

168 A8 y y

169 A9 z z

170 AA

171 AB

172 AC

173 AD

174 AE

175 AF

176 B0

177 B1

178 B2

179 B3

180 B4

Using Functions 755


ASCII and EBCDIC Code Chart

Decimal Hex ASCII EBCDIC

181 B5

182 B6

183 B7

184 B8

185 B9

186 BA

187 BB

188 BC

189 BD

190 BE

191 BF

192 C0 { left brace

193 C1 A A

194 C2 B B

195 C3 C C

196 C4 D D

197 C5 E E

198 C6 F F

199 C7 G G

200 C8 H H

201 C9 I I

202 CA

756
B. ASCII and EBCDIC Codes

Decimal Hex ASCII EBCDIC

203 CB

204 CC

205 CD

206 CE

207 CF

208 D0 } right brace

209 D1 J J

210 D2 K K

211 D3 L L

212 D4 M M

213 D5 N N

214 D6 O O

215 D7 P P

216 D8 Q Q

217 D9 R R

218 DA

219 DB

220 DC

221 DD

222 DE

223 DF

224 E0 \ left slash

Using Functions 757


ASCII and EBCDIC Code Chart

Decimal Hex ASCII EBCDIC

225 E1

226 E2 S S

227 E3 T T

228 E4 U U

229 E5 V V

230 E6 W W

231 E7 X X

232 E8 Y Y

233 E9 Z Z

234 EA

235 EB

236 EC

237 ED

238 EE

239 EF

240 F0 0 0

241 F1 1 1

242 F2 2 2

243 F3 3 3

244 F4 4 4

245 F5 5 5

246 F6 6 6

758
B. ASCII and EBCDIC Codes

Decimal Hex ASCII EBCDIC

247 F7 7 7

248 F8 8 8

249 F9 9 9

250 FA | vertical line

251 FB

252 FC

253 FD

254 FE

255 FF EO eight ones

Using Functions 759


ASCII and EBCDIC Code Chart

760
Legal and Third-Party Notices

SOME TIBCO SOFTWARE EMBEDS OR BUNDLES OTHER TIBCO SOFTWARE. USE OF SUCH
EMBEDDED OR BUNDLED TIBCO SOFTWARE IS SOLELY TO ENABLE THE FUNCTIONALITY (OR
PROVIDE LIMITED ADD-ON FUNCTIONALITY) OF THE LICENSED TIBCO SOFTWARE. THE
EMBEDDED OR BUNDLED SOFTWARE IS NOT LICENSED TO BE USED OR ACCESSED BY ANY
OTHER TIBCO SOFTWARE OR FOR ANY OTHER PURPOSE.

USE OF TIBCO SOFTWARE AND THIS DOCUMENT IS SUBJECT TO THE TERMS AND CONDITIONS
OF A LICENSE AGREEMENT FOUND IN EITHER A SEPARATELY EXECUTED SOFTWARE LICENSE
AGREEMENT, OR, IF THERE IS NO SUCH SEPARATE AGREEMENT, THE CLICKWRAP END USER
LICENSE AGREEMENT WHICH IS DISPLAYED DURING DOWNLOAD OR INSTALLATION OF THE
SOFTWARE (AND WHICH IS DUPLICATED IN THE LICENSE FILE) OR IF THERE IS NO SUCH
SOFTWARE LICENSE AGREEMENT OR CLICKWRAP END USER LICENSE AGREEMENT, THE
LICENSE(S) LOCATED IN THE "LICENSE" FILE(S) OF THE SOFTWARE. USE OF THIS DOCUMENT
IS SUBJECT TO THOSE TERMS AND CONDITIONS, AND YOUR USE HEREOF SHALL CONSTITUTE
ACCEPTANCE OF AND AN AGREEMENT TO BE BOUND BY THE SAME.

This document is subject to U.S. and international copyright laws and treaties. No part of this
document may be reproduced in any form without the written authorization of TIBCO Software
Inc.

TIBCO, the TIBCO logo, the TIBCO O logo, FOCUS, iWay, Omni-Gen, Omni-HealthData, and
WebFOCUS are either registered trademarks or trademarks of TIBCO Software Inc. in the
United States and/or other countries.

Java and all Java based trademarks and logos are trademarks or registered trademarks of
Oracle Corporation and/or its affiliates.

All other product and company names and marks mentioned in this document are the property
of their respective owners and are mentioned for identification purposes only.

This software may be available on multiple operating systems. However, not all operating
system platforms for a specific software version are released at the same time. See the
readme file for the availability of this software version on a specific operating system platform.

THIS DOCUMENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.

THIS DOCUMENT COULD INCLUDE TECHNICAL INACCURACIES OR TYPOGRAPHICAL ERRORS.


CHANGES ARE PERIODICALLY ADDED TO THE INFORMATION HEREIN; THESE CHANGES WILL
BE INCORPORATED IN NEW EDITIONS OF THIS DOCUMENT. TIBCO SOFTWARE INC. MAY MAKE
IMPROVEMENTS AND/OR CHANGES IN THE PRODUCT(S) AND/OR THE PROGRAM(S)
DESCRIBED IN THIS DOCUMENT AT ANY TIME.

761
THE CONTENTS OF THIS DOCUMENT MAY BE MODIFIED AND/OR QUALIFIED, DIRECTLY OR
INDIRECTLY, BY OTHER DOCUMENTATION WHICH ACCOMPANIES THIS SOFTWARE, INCLUDING
BUT NOT LIMITED TO ANY RELEASE NOTES AND "READ ME" FILES.

This and other products of TIBCO Software Inc. may be covered by registered patents. Please
refer to TIBCO's Virtual Patent Marking document (https://www.tibco.com/patents) for details.

Copyright © 2021. TIBCO Software Inc. All Rights Reserved.

762
Index
argument formats 62
-IF command 70, 71
argument length 63
-RUN command 72, 73
argument types 61
-SET command 69, 70
ASCII character chart 50, 747

A ASCII function 534


ASIS function 197, 198, 548
ABS function 548
assigning date-time values 443
accessing external functions 78
COMPUTE command 443, 445
OS/390 78
DEFINE command 443
UNIX 79
IF criteria 443, 446
VM/CMS 79
WHERE criteria 443, 445
Windows 79
ATODBL function 504
accessing functions 59–61
AYM function 419
OS/390 79
AYMD function 420
ADD function 482
adding function libraries 79 B
alphanumeric argument 62
bar charts 549
alphanumeric strings 504
scales 549
converting 504
BAR function 549, 550
analytic functions 81
batch allocation 78
INCREASE 114
bit strings 201, 202
PCT_INCREASE 118
BITSON function 199, 200
PREVIOUS 121
BITVAL function 201, 202
RUNNING_AVE 123
branching in procedures 70
RUNNING_MAX 126
functions and 70, 71, 73
RUNNING_MIN 129
browser cookie values, retrieving 652
RUNNING_SUM 132
BUSDAYS parameter 367
ANOMALY_IF function 606
business days 367
ARGLEN function 196, 197
BUSDAYS parameter 367

Using Functions 763


Index

BYTVAL function 202–204 character functions 23, 25, 28, 29, 196
EDIT 211, 212
C GETTOK 213, 215

calling functions 59–61 LCWORD 215–217, 281, 282

Dialogue Manager and 68 LCWORD2 216–218

FOCUS commands and 67 LCWORD3 217

from another function 73 LEFT 148

IF criteria 74, 75 LJUST 218

in Maintain 61 LOCASE 220, 221

RECAP command and 76, 77 Maintain-specific 30, 279

WHEN criteria 75, 76 OVRLAY 221, 222

WHERE criteria 74 PARAG 223, 224

CEILING function 534 POSIT 227, 228

CHAR function 489 REGEXP_INSTR 169

CHAR_LENGTH function 136 REGEXP_REPLACE 172

CHAR2INT function 280 REGEXP_SUBSTR 174

character chart 50, 747 REPEAT 176

character functions 23, 25, 28, 29, 196 RIGHT 179

ARGLEN 196, 197 RJUST 230, 231

ASIS 197, 198 SOUNDEX 231, 232

BITSON 199, 200 SPACE 184

BITVAL 201, 202 SPELLNM 233, 234

BYTVAL 202–204 SQL 711

CHKFMT 204, 206, 207 SQUEEZ 234, 235

CHKNUM 206 STRIP 235–237

CTRAN 207–209 SUBSTR 239–241, 256

CTRFLD 210, 211 TRIM 241–243

DCTRAN 263 TRIMV 258

DIFFERENCE 139 UPCASE 244

DSTRIP 266, 267 variable length 251

764
Index

character functions 23, 25, 28, 29, 196 CHGDAT function 421, 423
XMLDECOD 245 CHKFMT function 204, 206, 207
XMLENCOD 247 CHKNUM function 206
character strings 196, 218 CHKPCK function 551
adding 284 CHR function 713
bits 199, 201 CLASSIFY_BLR function 609
centering 210, 211 CLASSIFY_KNN function 613
comparing 231, 295, 296 CLASSIFY_RF function 615
converting case 220, 244, 281, 283, 301 CLASSIFY_XGB function 619
determining length 300 CLSDDREC 661
Dialogue Manager 197 COALESCE function 306
dividing 223 commands 659
extracting 284, 297 passing 659
extracting characters 211 COMPACTFORMAT function 490
extracting substrings 213, 239, 241, 256, compiling subroutines 726
299 OS/390 727
finding substrings 227, 290 UNIX 727
format 204 Windows 727
justifying 218, 230, 291 components 440
measuring length 196, 282 COMPUTE command 67
overlaying 221, 289 assigning date-time values 445
reducing spaces 234 CONCAT function 137
removing occurrences 241, 300 controlling function parameter verification 65
right-justifying 230, 291 conversion functions, simplified 39, 489
spelling out numbers 233 CHAR 489
substrings 293, 297 CTRLCHAR 492
translating characters 202, 207, 208, 280 DT_FORMAT 494
CHECKMD5 function 304 HEXTYPE 497
CHECKPRIVS function 646 TO_INTEGER 501
CHECKSUM function 305 TO_NUMBER 502

Using Functions 765


Index

conversion functions,simplified date and time functions 366


PHONETIC 499 arguments and 440
converting formats 504 AYM 419
cookie values, retrieving 652 AYMD 420
creating subroutines 715 CHGDAT 421, 423
cross-referenced data sources 333 DA 424
CTRAN function 207–209 DADMY 424
CTRFLD function 210, 211 DADYM 424
CTRLCHAR function 492 DAMDY 424
custom subroutines 728–730, 732, 734 DAMYD 424
DATEADD 375
D DATECVT 378

DA functions 424 DATEDIF 380

DADMY function 424 DATEMOV 383

DADYM function 424 DATETRAN 390

DAMDY function 424 DAYDM 424

DAMYD function 424 DAYMD 424, 425

data sets 649, 654 DOWK 427

data source functions 31, 303 DOWKL 427

FIND 324, 325 DTDMY 428

LAST 331, 332 DTDYM 428

LOOKUP 333, 334, 336, 337 DTMDY 428

data sources 303 DTMYD 428

cross-referenced 333, 337 DTYDM 428

decoding values 320 DTYMD 428

retrieving values 331–333, 337 GREGDT 430

values 303 HADD 446–448

verifying values 324, 325 HCNVRT 449, 450

date and date-time functions 33 HDATE 450, 451


HDIFF 452, 453

766
Index

date and time functions 366 date functions 33


HDTTM 453–455 work days 367
HGETC 457–459 date-time format
HGETZ 458 ISO standard input values 443
HHMMSS 460 date-time functions 36, 455
HHMS 461 DT_TOLOCAL 345
HINPUT 462, 463 DT_TOUTC 347
HMIDNT 464, 465 HEXTR 455
HNAME 468, 469 HMASK 465
HPART 469–471 date-time values 479
HSETPT 471–473 adding 419, 420, 482
HTIME 473, 474 assigning 443
JULDAT 432 converting 473, 474
legacy 35, 417 converting formats 421, 424, 428, 432, 449,
Maintain-specific 479 450, 453
standard 367 determining day of week 487
TIMETOTS 474, 475 determining quarter 485
TODAY 415 elapsed time 433, 483
YM 433 extracting 483, 484, 488
YMD 427 finding day of week 427
date argument 62 finding difference 380, 426, 452
date formats 440 incrementing 446
formatted-string format 441, 442 moving dates 383
international 390 retrieving 479–481
numeric string format 441 retrieving components 469
translated-string format 442 retrieving time 460, 461
date function returning dates 415
DAYNAME 342 setting 485
MONTHNAME 362 setting time 464
storing 457, 458

Using Functions 767


Index

date-time values 479 DIFFERENCE function 139


subtracting 419, 420, 486 DIGITS function 142
DATEADD function 375 DMOD function 553, 554
DATECVT function 378 DMY function 426
DATEDIF function 380 double exponential smoothing 90
DATEFORMAT parameter 436 FORECAST_DOUBLEXP 90
DATEMOV function 383 double-byte characters 263, 266
DATETRAN function 390, 398 DOWK function 427
DAY function 483 DOWKL function 427
DAYDM function 424 DPART function 406
DAYMD function 424, 425 DSTRIP function 266, 267
DAYNAME function 342 DSUBSTR function 267
DB_EXPR function 309 DT_CURRENT_DATE function 343
DB_LOOKUP function 317 DT_CURRENT_DATETIME function 343
COMPUTE command 317 DT_CURRENT_TIME function 344
DEFINE 317 DT_FORMAT function 494
MODIFY 317 DT_TOLOCAL function 345
TABLE COMPUTE 317 DT_TOUTC function 347
DCTRAN function 263 DTADD function 351
DECODE function 320–323 DTDIFF function 354
decoding functions 31, 303, 320–323 DTDMY function 428
decoding values 320 DTDYM function 428
from files 320, 322, 323 DTMDY function 428
in a function 320–322 DTMYD function 428
DEDIT function 264 DTPART function 357
DEFINE command 67 DTRUNC function 359
functions and 67 DTSTRICT parameter 438, 439
deleting function libraries 79 DTYDM function 428
Dialogue Manager 68 DTYMD function 428
functions and 68

768
Index

E FMLCAP function 557


FMLFOR function 558
EBCDIC character chart 50, 747
FMLINFO function 560
EDALIB.LOAD library 78
FMLLIST function 561
EDIT function 211, 212, 505, 506
FMOD function 553, 554
enabling parameter verification 64
FOCUS commands 67
ENCRYPT function 640
FOR lists 561
entry points 722, 723
retrieving 561
environment variables 648
FORECAST_DOUBLEXP
assigning values 650
double exponential smoothing 90
retrieving values 648
FORECAST_EXPAVE
error messages 647
exponential moving average 87
EXP function 555, 556
FORECAST_LINEAR
EXPN function 556
linear regression equation 97
exponential moving average 87
FORECAST_MOVAVE
FORECAST_EXPAVE 87
simple moving average 81
external functions 19, 78
FORECAST_SEASONAL

F triple exponential smoothing 92


format conversion functions 40
FEXERR function 647, 648
ATODBL 504
FGETENV function 648
EDIT 505, 506
FIND function 324, 325
FPRINT 506
FINDMEM function 649, 650
FTOA 511, 512
FIQTR function 408
HEXBYT 512–514
FIYR function 410
ITONUM 515, 516
FIYYQ function 412
ITOPACK 516, 517
FLOOR function 537
ITOZ 518, 519
FML (Financial Modeling Language) 561
PCKOUT 519, 520
retrieving tag lists 561
PTOA 520–522
retrieving tag values 558
TSTOPACK 522
FML hierarchies 557

Using Functions 769


Index

format conversion functions 40 function types 20


UFMT 524, 525 date 33
format conversions 504 date and date-time 33
packed numbers 519 date-time 36
to alphanumeric 511, 520 decoding 31, 303
to characters 512 format conversion 40
to double-precision 515 geography, simplified 49
to hexadecimal 524 Maintain-specific character 30
to packed decimal 516 Maintain-specific light update support 42
to zoned format 518 numeric 42, 547
formats 504 numeric, simplified 42
alphanumeric 506 script 45, 581
converting 504 simplified conversion 39
formatted-string format 441, 442 system 47, 645
FPRINT function 495, 506 system, simplified 46
FPUTENV function 650, 651 functions 17, 19, 59, 317
FTOA function 511, 512 -IF command and 70, 71
function arguments 61 -RUN command and 72, 73
formats 62 accessing 59
functions as 73 analytic 81
in subroutines 717 branching in procedures 70, 71, 74
length 63 calling 59–61, 67, 73
number 64 character 711
types 61 COMPUTE command and 67
function libraries 61, 79 date and time 366, 417
adding 79 DEFINE command and 67
deleting 79 Dialogue Manager and 68
function types 20 external 19
character 196, 279 FIND 324, 325
data source 31, 303 FIQTR 408

770
Index

functions 17, 19, 59, 317 geograpny functions


FIYR 410 GIS_DISTANCE 676
FIYYQ 412 GIS_DRIVE_ROUTE 678
FMLCAP 557 GIS_GEOCODE_ADDR 682
FMLFOR 558 GIS_GEOCODE_ADDR_CITY 683
FMLINFO 559, 560 GIS_GEOCODE_ADDR_POSTAL 685
FMLLIST 561 GIS_GEOMETRY 686
FOCUS commands and 68 GIS_IN_POLYGON 690
HEXTR 455 GIS_LINE 691
HMASK 465 GIS_POINT 695
internal 19 GIS_SERV_AREA_XY 704
invoking 60 GIS_SERVICE_AREA 700
languages 17 GET_TOKEN function 144
MIRR 564 GETCOOKI function 652
operating system commands and 72, 73 GETENV function 641
operating systems 17 GETHEADR function 653
SLEEP 664 GETPDS function 654–656
SQL 711, 713 GETTOK function 213, 215
STRREP 237 GETUSER function 656–658
subroutines 19, 715 GIS_DISTANCE function 676
types 20 GIS_DRIVE_ROUTE function 678
VALIDATE command and 67 GIS_GEOCODE_ADDR function 682
variable length character 251 GIS_GEOCODE_ADDR_CITY function 683
variables and 69, 70 GIS_GEOCODE_ADDR_POSTAL function 685
VM/CMS 79 GIS_GEOMETRY function 686
XIRR 577 GIS_IN_POLYGON function 690
FUSELIB.LOAD library 78 GIS_LINE function 691
GIS_POINT function 695
G GIS_REVERSE_COORDINATE function 698

geography functions 671 GIS_SERV_AREA_XY function 704

Using Functions 771


Index

GIS_SERVICE_AREA function 700 HYYWD function 476


GREGDT function 430, 433
GRPLIST function 657 I

IF criteria 73
H
assigning date-time values 446
HADD function 446–448 functions and 74, 75
hash value 304, 305 IMOD function 553, 554
HCNVRT function 449, 450 IMPUTE function 326
HDATE function 450, 451 INCREASE function 114
HDIFF function 452, 453 INITCAP function 145
HDTTM function 453–455 Initial_HHMMSS function 480
header variables, retrieving 653 Initial_TODAY 480
HEXBYT function 512–514 Initial_TODAY function 480
HEXTR function 455 INT function 562
HEXTYPE function 497 INT2CHAR function 280
HGETC function 457–459 internal functions 19
HGETZ function 458 internal modified rate of return 564
HHMMSS function 460, 479, 480 internal rate of return 577
HHMS function 461 international date formats 390
HINPUT function 462, 463 invoking functions 59, 60
HMASK function 465 ISO standard date-time formats 443
HMIDNT function 464, 465 ITONUM function 515, 516
HNAME function 468, 469 ITOPACK function 516, 517
holidays 367, 368, 370 ITOZ function 518, 519
holiday files 368, 370 IWC.FindAppCGIValue function 529, 530, 585,
HPART function 469–471 586
HSETPT function 471–473 IWC.GetAppCGIValue function 530, 531, 587
HTIME function 473, 474 IWCLink function 582
HTMTOTS function 474 IWCSwitchToSecure function 583
HTTP header variables, retrieving 653 IWCSwitchToUnsecure function 583

772
Index

IWCTrigger function 584, 585 light update support functions 42


IWC.GetAppCGIValue 530, 531
J linear regression equation 97

JOBNAME function 658 FORECAST_LINEAR 97

JULDAT function 432 LJUST function 218, 283

JULIAN function 483, 484 load libraries 78


LOCAS function
K variable length 253
LOCASE function 220, 221
KKFCUT function 273
LOCATE function 711
LOG function 563
L
LOG10 function 538
lag values 110
LOOKUP function 333, 334, 336, 337
languages 17
extended function 337
LAST function 331, 332
LOWER function 150, 283
LAST_NONBLANK function 146
LPAD function 151
LCWORD function 215–217, 281, 282
LTRIM function 153
LCWORD2 function 216–218, 281, 282
LCWORD3 function 217, 218 M
LEADZERO parameter 373
Machine Learning (Python-based) functions 605
LEFT function 148
machine learning functions, Python
legacy date functions 33
ANOMALY_IF 606
DMY 426
CLASSIFY_BLR 609
legacy dates 417
CLASSIFY_KNN 613
legacy versions 417
CLASSIFY_RF 615
MDY 426
CLASSIFY_XGB 619
YMD 426
REGRESS_KNN 622
legacy dates 417
REGRESS_POLY 624
LENGTH function 282
REGRESS_RF 628
light update support functions 42
REGRESS_XGB 631
IWC.FindAppCGIValue 529, 530

Using Functions 773


Index

Machine learning functions Maintain-specific date and time functions 479


RUN_MODEL 634 Initial_TODAY 480
Maintain data source functions 325 JULIAN 483, 484
Maintain-specific character functions 30, 279 MONTH 484
CHAR2INT 280 QUARTER 485
INT2CHAR 280 SETMDY 485, 486
LCWORD 281 standard 479
LCWORD2 281, 282 SUB 486, 487
LENGTH 282 TODAY 480, 481
LJUST 283 TODAY2 481
LOWER 283 WEEKDAY 487, 488
MASK 284, 285 YEAR 488
NLSCHR 288, 289 Maintain-specific functions 61
OVRLAY 289, 290 MNTUWS function library 61
POSIT 290, 291 script 581
RJUST 291, 292 SELECTS 292
SELECTS 292, 293 STRNCMP 297
STRAN 293, 294 STRTOKEN 298
STRCMP 295 Maintain-specific light update support functions
STRICMP 296, 297 42
STRTOKEN 297, 298 Maintain-specific script functions 45
SUBSTR 299 MASK function 284, 285
TRIM 300 MAX function 563, 564
TRIMLEN 300, 301 MD5 hash value 304
UPCASE 301 MDY function 426
Maintain-specific date and time functions 479 MIN function 563, 564
ADD 482 MIRR function 564
DAY 483 MNTGETTOK function 285
HHMMSS 479, 480 MNTUWS function library 61
Initial_HHMMSS 480 modified rate of return 564

774
Index

MONTH built-in function 484 numeric functions 42, 547, 549


MONTH function 484 ABS 548
MONTHNAME function 362 ASCII 534
MTHNAM subroutine 728–730, 732, 734 ASIS 548
MVSDYNAM function 659, 660 BAR 549, 550
CHKPCK 551
N DMOD 553, 554

naming subroutines 717 EXP 555, 556

National Language Support (NLS) 288 FMLCAP 557

NLS (National Language Support) 288 FMLFOR 558

NLSCHR function 288, 289 FMLINFO 559–561

NORMSDST function 568, 570–572 FMOD 553, 554

NORMSINV function 568, 570–572 IMOD 553, 554

NULLIF function 338 INT 562

number of arguments 64 LOG 563

numbers 548 LOG10 538

absolute value 548 MAX 563, 564

bar charts 549 MIN 563, 564

calculating remainders 553 NORMSDST 568, 570–572

generating random 572, 575 NORMSINV 568, 570–572

greatest integer 562 PRDNOR 572–574

logarithms 563 PRDUNI 572, 573

maximum 563 RDNORM 575, 576

minimum 563 RDUNIF 575, 576

raising to a power 555 ROUND 542

square root 576 SIGN 543

standard normal deviation 568, 569, 571 SQRT 576, 577

validating packed fields 551 TRUNCATE 544

numeric argument 62 numeric string format 441


numeric values 547

Using Functions 775


Index

O PTOA function 520–522


PUTDDREC 661
operating system commands 72, 73
PUTENV function 641
operating systems 17
Python functions 605
order of arguments 64
Python machine learning functions
OS/390 727
ANOMALY_IF 606
compiling subroutines 727
CLASSIFY_BLR 609
storing functions 78, 79
CLASSIFY_KNN 613
storing subroutines 727
CLASSIFY_RF 615
OUTLIER function 596
CLASSIFY_XGB 619
OVRLAY function 221, 222, 289, 290
REGRESS_KNN 622

P REGRESS_POLY 624
REGRESS_RF 628
packed numbers, writing to an output file 525
REGRESS_XGB 631
PARAG function 223, 224
PARTITION_AGGR 101 Q
PARTITION_REF 110
QUARTER function 485
PATTERN function 225
PATTERNS function 156
R
PCKOUT function 519, 520
PCT_INCREASE function 118 rate of return 564, 577

PHONETIC function 499 RDNORM function 575, 576

POSIT function 227, 228, 290, 291 RDUNIF function 575, 576

POSITION function 158 RECAP command 76, 77

PRDNOR function 572–574 REGEX function 162

PRDUNI function 572, 573 REGEXP_INSTR function 169

PREVIOUS function 121 REGEXP_REPLACE function 172

prior values 110 REGEXP_SUBSTR function 174

process IDs 658 REGRESS_KNN function 622

programming subroutines 721 REGRESS_POLY function 624

arguments 723, 725 REGRESS_RF function 628

776
Index

REGRESS_XGB function 631 script functions 45, 581


regular expressions 161 IWC.FineAppCGIValue 585, 586
on z/OS 162 IWC.GetAppCGIValue 587
REPEAT function 176 IWCLink 582
REPLACE function 177 IWCSwitchToSecure 583
reporting server functions IWCSwitchToUnsecure 583
GETCOOKI 652 IWCTrigger 584, 585
GETHEADR 653 scripts 581
retrieving environment variable values 648 SELECTS function 292, 293
retrieving FML hierarchy captions 557 SET parameters 367
return rate functions 564 BUSDAYS 367
MIRR 564 DTSTRICT 438, 439
XIRR 577 HDAY 368, 370
REVERSE function 229 LEADZERO 373
REXX subroutines 735–744 SETMDY function 485, 486
formats 740 SFTDEL function 274
RIGHT function 179 SFTINS function 276
RJUST function 230, 231, 291, 292 SIGN function 543
rolling calculations 101 SIGN SQL numeric function 543
ROUND function 542 simple moving average 81
RPAD function 181 FORECAST_MOVAVE 81
RTRIM function 183 simplified character functions 135
RUN_MODEL machine learning function 634 simplified conversion functions 489
RUNNING_AVE function 123 simplified date functions 341
RUNNING_MAX function 126 simplified geography functions 49
RUNNING_MIN function 129 GIS_REVERSE_COORDINATE 698
RUNNING_SUM function 132 simplified numeric functions 42
simplified system functions 46, 639
S single-byte characters 263, 266

scales 549 SLACK function 642

Using Functions 777


Index

SLEEP function 664 STRICMP function 296, 297


SOUNDEX function 231, 232 string replacement 237, 238
SPACE function 184 STRIP function 235–237
SPAWN function 665, 666 STRNCMP function 297
SPELLNM function 233, 234 STRREP function 237, 238
SPLIT function 185 STRTOKEN function 297, 298
SQL character functions SUB function 486, 487
LOCATE 711 subroutines 19, 715
SQL functions 711, 713 compiling 726
SQL misc functions creating 715
CHR 713 custom 728–730, 732, 734
SQL numeric functions entry points 722, 723
SIGN 543 MTHNAM 728–730, 732, 734
SQRT function 576, 577 naming 717
SQUEEZ function 234, 235 programming 721
standard date and time functions 367 REXX 735–744
standard date functions 33 storing 726
standard normal deviation 568, 569, 571 testing 727
statistical functions 589 writing 715
storing external functions SUBSTR function 239–241, 256, 299
OS/390 78, 79 variable length 256
UNIX 79 SUBSTRING function 186
VM/CMS 79 substrings 211
Windows 79 comparing 297
storing subroutines 726 extracting 211, 213, 239, 241, 256, 299
OS/390 727 finding 227, 290
UNIX 727 overlaying character strings 221, 289
Windows 727 substituting 293
STRAN function 293, 294 SYSTEM function 667, 668
STRCMP function 295

778
Index

system functions 47, 645 translated-string format 442


CHECKPRIVS 646 TRIM function 241–243, 300
FEXERR 647, 648 TRIM_ function 190
FGETENV 648 TRIMLEN function 300, 301
FINDMEM 649, 650 TRIMV function 258
FPUTENV 650, 651 triple exponential smoothing 92
GETCOOKI 652 FORECAST_SEASONAL 92
GETHEADR 653 TRUNCATE function 544
GETPDS 654–656 TSTOPACK function 522
GETUSER 656–658
GRPLIST 657 U
JOBNAME 658 UFMT function 524, 525
MVSDYNAM 659, 660 UNIX 79
SPAWN 665, 666 accessing functions 79
SYSTEM 667, 668 compiling subroutines 727
SYSVAR 668 storing functions 79
SYSVAR function 668 storing subroutines 727
UPCASE function 244, 301
T
UPPER function 193
tag lists 561 user IDs 656
retrieving 561 USERFCHK setting 64, 65
tag values 558 USERFNS setting 64
testing subroutines 727
time formats 442 V
TIMETOTS function 474, 475 VALIDATE command 67
TO_INTEGER function 501 values 320
TO_NUMBER function 502 decoding 320
TODAY function 415, 480, 481 verifying 324, 325
TODAY2 function 481 variable length character functions 251
TOKEN function 188

Using Functions 779


Index

verifying function parameters 64 work days 367


controlling 65 holidays 367, 368, 370
enabling 64 writing subroutines 715
VM/CMS 79 creating arguments 717
accessing external functions 79 entry points 722, 723
storing external functions 79 languages 718
naming subroutines 717
W programming 721, 723, 725

WEEKDAY function 487, 488


X
WEEKFIRST parameter 437
WHEN criteria 75 XIRR function 577
WHERE criteria 445 XMLDECOD function 245
assigning date-time values 445 XMLENCOD function 247
functions and 74 XTPACK function 525
Windows 79
accessing external functions 79 Y
compiling subroutines 727 YEAR function 488
storing subroutines 727 YM function 433
work days 367 YMD function 426, 427
business days 367

780

You might also like