Using Functions

In this section:

This section describes system-supplied functions you can use in expressions. Write your own functions to solve specific application problems.

For complete information, see the WebFOCUS Using Functions manual.


Top of page

x
System-supplied Function Examples

The arguments for the following usage examples are amper variables, expressions, or other functions.

Function

Description

ABS

Returns the absolute value of a number. Computes on one argument:

-SET &PRICE = (ABS(&AMOUNT-&OLDAMOUNT))/100;
INT

Returns the integer part of a number. Computes on one argument:

-SET &YEAR = INT(&DATE/10000);
MAX

Returns the maximum value. Computes on one or more arguments, each separated by a comma:

-SET &LARGE = IF &FACTOR GT 10 THEN MAX(10,&AMOUNT)
-  ELSE MAX(0,&AMOUNT,&VALUE/10);
MIN

Returns the minimum value. Computes on one or more arguments, each separated by a comma:

-SET &LOW = MIN(0,&AMOUNT,&NEWAMOUNT,&OTHER);
LOG

Returns the logarithm of a number, base e. Computes on one argument:

-SET &VAL = 100*&AMOUNT*LOG(&PRICE);
SQRT

Returns the square root of a number. Computes on one argument:

-SET &VALUE = 100*&AMOUNT/SQRT(&TOTA);


x
System-supplied Function Table

This table summarizes additional system-supplied functions and arguments. Contact your Information Builder representative to request full documentation on these functions.

Function

Arguments

Description

ABS
value

Returns the absolute value of a number.

ARGLEN
inlength,infield,outfield

Calculates the non-blank length of an alphanumeric field.

ASIS
value

Distinguishes between a blank and zero.

ATODBL
number,inlength,outfield

Converts an alphanumeric field containing numeric data to a double-precision decimal field.

AYM
indate,months,outfield

Adds or subtracts a number of months from a given date.

AYMD
indate,days,outfield

Adds or subtracts a number of days from a given date.

BAR
barlength,infield,maxvalue,
char,outfield

Includes a bar graph in a tabular report. Available for MVS and VM only.

BITSON
bitnumber,infield,outfield

Interprets multi-punch data (data that cannot be represented alphanumerically).

BITVAL
infield,startbit,number,
outfield

Obtains the decimal value of a string of bits.

BYTVAL
character,outfield

Obtains the decimal equivalent of an alphanumeric character.

CHGDAT
oldformat,newformat,indate,
outfield

Changes the format of a date.

CHKFMT
numchar,infield,mask,
outfield

Checks character strings for incorrect character types.

CHKPCK
inlength,infield,error,
outfield

Validates packed field format.

CNCTUSR
outfield

Indicates the connected user.

CTRAN
inlength,infield,incode,
outcode,outfield

Substitutes characters in a string.

CTRFLD
infield,inlength,outfield

Centers character strings within fields.

DADMY
indate,outfield

Calculates number of days from 1/1/00 with input in day-month-year format.

DADYM
indate,outfield

Calculates number of days from 1/1/00 with input in day-year-month format.

DAMDY
indate,outfield

Calculates number of days from 1/1/00 with input in month-day-year format.

DAMYD
indate,outfield

Calculates number of days from 1/1/00 with input in month-year-day format.

DAYDM
indate,outfield

Calculates number of days from 1/1/00 with input in year-day-month format.

DAYMD
indate,outfield

Calculates number of days from 1/1/00 with input in year-month-day format.

DATEADD
YYMDdate,unit,#units

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

DATECVT
indate,infmt,outfmt

Converts date formats within applications without requiring intermediate calculations.

DATEDIF
fromYYMD,toYYMD,unit

Returns the difference between two dates in units.

DATEMOV
YYMDdate,move-point

Moves a date to a significant point on the calendar.

DECODE
instring (invalue 
outvalue...)

Value translation (for more information, see Decoding a Value).

DMOD
dividend,divisor,outfield

Calculates the remainder from a division operation and returns a double-precision value.

DMY
begin,end

Calculates the difference between two dates in integer, alphanumeric, or packed format.

DOWK
indate,outfield

Provides the day of the week (in 4-character alphanumeric format) based on input date.

DOWKL
indate,outfield

Provides the day of the week (in 12-character alphanumeric format) based on input date.

DTDMY
number,outfield

Calculates the date from the number of days since 1/1/00 in day-month-year format.

DTDYM
number,outfield

Calculates the date from the number of days since 1/1/00 in day-year-month format.

DTMDY
number,outfield

Calculates the date from the number of days since 1/1/00 in month-day-year format.

DTMYD
number,outfield

Calculates the date from the number of days since 1/1/00 in month-year-day format.

DTYDM
number,outfield

Calculates the date from the number of days since 1/1/00 in year-day-month format.

DTYMD
number,outfield

Calculates the date from the number of days since 1/1/00 in year-month-day format.

EDIT
infield[,mask]

Converts numeric to string when both parms are supplied or string to masked string when only the first parm is supplied. For more information, see Editing a Value.

EXP
power,outfield

Raises "e" to a given power.

EXPN
n.nn {E|D} {+|-} p

Evaluates an argument expressed in scientific notation.

FEXERR
nnnnn,A72

Retrieves an error message.

FGETENV
envnamelen,envname,
outfieldlen,outfldformat

Retrieves the value of an environment variable and returns it as an alphanumeric string.

FINDMEM
ddname,member,outfield

Determines whether a partitioned data set contains a specified member. Available for MVS only.

FMLINFO
'forvalue', outfield 

Retrieves FML value for direct use in calculations or a report.

FMOD
dividend,divisor,outfield

Calculates the remainder from a division operation and returns a single-precision value.

FORECAST
fld2,interval,npredict, 
method

Uncovers trends in numeric data. Methods are: Simple Moving Average (MOVAVE), Exponential Moving Average (EXPAVE), and Linear Regression Analysis (REGRESS).

FPUTENV
namelength,name, 
valuelength,value,rc

Assigns a character string to an environment variable. The rc parameter is an integer return value denoting success or failure. It requires an integer format (for example, I8). An rc value of 0 means success. All other values mean failure.

If the string value contains embedded, leading, or trailing spaces, the whole string must be enclosed in double quotation marks (").

FTOA
number,usage,outfield

Converts a numeric field to alphanumeric format without inserting leading zeros.

GETPDS
ddname,member,outfield

Determines whether a specific member of a partitioned data set (PDS) exists and returns the PDS name.

GETSECID
outfield

Retrieves the security ID. Available for MVS only.

GETTOK
infield,inlen,toknum,delim,
outlen,outfield

Extracts a token from a data string.

GETUSER
outfield

Retrieves the user ID from the system.

GREGDT
indate,outfield

Converts a Julian date to a Gregorian date.

HADD
dtfield,component,
increment,length,Hformat

Specifies a date-time field by a given number of units.

HCNVRT
dtfield,Hfmt,rlength,Ann

Converts a date-time field to alphanumeric format for use with operators such as EDIT, CONTAINS, and LIKE.

HDATE
dtfield,dateformat

Extracts the date portion of a date-time field and converts it to a date format.

HDIFF
dtfield1,dtfield2,component
,Dformat

Finds the number of boundaries of a given type crossed going from date 2 to date 1.

HDTTM
datefield,length,Hformat

Converts a date field to a date-time field.

HEXBYT 
number,outfield

Obtains the character equivalent of a numeric value.

HGETC
length,Hformat

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

HHMMSS
outfield

Retrieves the current time from the system.

HINPUT
inputlength,inputstring,
length,Hfmt

Converts an alphanumeric string to a date-time value.

HMIDNT
dtfield,length,Hformat

Changes the time portion of a date-time field to midnight.

HNAME
dtfield,component,Aformat

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

HPART
dtfield,component,Iformat

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

HSETPT
dtfield,component,value,
length,Hformat

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

HTIME
length,dtfield,Dformat

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

IMOD 
dividend,divisor,outfield

Calculates the remainder from a division operation and returns an integer value.

INT
value

Returns the integer part of an argument.

ITONUM
sigbytes,infield,outfield

Converts large binary integers in non-FOCUS files to double-precision format.

ITOPACK
sigbytes,infield,outfield

Converts large binary integers in non-Dialogue Manager files to packed format.

ITOZ
outlength,number,outfield

Converts a numeric field to a zoned decimal.

JULDAT
indate,outfield

Converts a Gregorian date to a Julian date.

LCWORD
length,instring,outstring

Translates uppercase characters in alphanumeric fields to lowercase on a word-by-word basis.

LJUST
inlength,infield,outfield

Left-justifies a character string within a field.

LOCASE
length,infield,outfield

Translates uppercase characters in alphanumeric fields to lowercase characters.

LOG
value

Returns the logarithm of a number, base e.

MAX
value1,value2...

Returns the maximum value.

MDY
begin,end

Calculates the difference between two dates in integer, alphanumeric, or packed format.

MIN
value1,value2...

Returns the maximum value.

MVS DYNAM
command,length,rc

Transfers a specified FOCUS DYNAM command to the DYNAM command processor. The rc parameter is an integer return value denoting success or failure. It requires an integer format (for example, I8). An rc value of 0 means success. All other values mean failure.

NORMSDST
value,outfield 

Calculates percent of data value less than or equal to a normalized value.

NORMSINV
value,outfield 

Calculates the upper percent of data value boundary for a normalized value.

OVRLAY
base,baselen,substring,
sublen,position,outfield

Overlays a character string on a character string.

PARAG
inlength,infield,delimiter,
subsize,outfield

Inserts a delimiter into a character string.

PCKOUT
infield,outlength,outfield

Outputs a packed field.

POSIT
parent,inlength,substring,
sublength,outfield

Finds the position of a character string in another string.

PRDNOR 
seed,outfield

Generates repeatable random numbers for normal distribution.

PRDUNI
seed,outfield

Generates repeatable random numbers for uniform distribution.

RDNORM
outfield

Generates random numbers for normal distribution.

RDUNIF
outfield

Generates random numbers for uniform distribution.

REVERSE
length,input,output

Reverses the characters that were input.

RJUST
inlength,infield,outfield

Right-justifies an alphanumeric field.

SOUNDEX
inlength,string,outfield

Searches for character strings phonetically.

SPELLNUM
outlength,infield,outfield

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

SQRT
value

Returns the square root of a number.

STRIP
length,source_string,
strip_char,result

Removes all occurrences of a specific character from an input string.

SUBSTR
inlength,parent,start,end,
outlength,outfield

Extracts a substring.

TEMPPATH
outlength,outfield

Retrieves the physical directory name of the current agent process.

TODAY
outfield

Retrieves the current date from the system.

TRIM
location,string,
string_length,pattern,
pattern_length,,result

Removes leading and/or trailing occurrences of a pattern within a string. The location parm indicating where to trim the pattern is L (leading), T (trailing), or B (both leading and trailing).

TRUNCATE
var1

Removes trailing blanks from Dialogue Manager amper variables and adjusts the length accordingly.

UFMT
infield,inlength,outfield

Converts characters in alphanumeric fields to HEX representation.

UPCASE
length,infield,outfield

Translates lowercase characters in alphanumeric fields to uppercase.

YM
fromdate,todate,outfield

Returns the number of months between two dates.

YMD
begin,end

Calculates the difference between two dates in integer, alphanumeric, or packed format.

In most cases, outfield is expressed as a format and length such as A10 or I8 depending on the expected usage of the function.



x
Verifying Function Parameters

How to:

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.

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:



x
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 will be used:

SET USERFNS= {SYSTEM|LOCAL}

where:

SYSTEM

Gives precedence to functions supplied by Information Builders. SYSTEM is the default setting. This setting is required in order to enable parameter verification.

LOCAL

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



x
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, a message is generated and the evaluation of the affected expression is terminated.

Note that if a parameter provided is the incorrect type, verification fails and processing terminates.

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.

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

Note that if a parameter provided is the incorrect type, verification fails and processing terminates.

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 that if a parameter provided is the incorrect type, verification fails and processing terminates.


Top of page

x
Creating Routines

Custom routines may be written in a 3GL and added to the server's search path provided they are:

The script, gencpgm, is provided to assist in the actual compilation of a program on most platforms, as well as sample routines, but any method is allowed provided that it compiles and links a program correctly. For more information, see GENCPGM Usage.

On MVS, build routines into a loadlib and allocate as ALLOCATE F(IBICPG) DA('USER.LIBRARY.LOAD') SHR, unless they are REXX based (see MVS REXX below).

On VM, build routines into a loadlib and execute GENSUBLL EXEC to generate the new loadlib, unless they are REXX based (see VM REXX below).

On MVS and VM, routines may also be written in REXX. On MVS, REXX routines must be stored in a PDS with a FUSREXX ddname allocated to the PDS. On VM, each routine must be in a file with a file type of FUSREXX on an accessible disk. On VM, compiled REXX is also supported and uses the same file type of FUSREXX.


Top of page

x
Editing a Value

How to:

The mask option of the EDIT function inserts characters in an alphanumeric value, or extracts certain characters from the value.



x
Syntax: How to Use the EDIT Function
EDIT(fieldname,'mask');

where:

fieldname

Is the name of the field to be edited.

'mask'

Is a value that the field name matches against, enclosed in single quotation marks. If mask contains the number 9, the corresponding character in fieldname is moved to the new field. If mask contains a dollar sign ($), the corresponding character in fieldname is ignored. If a character in mask is neither the number 9 nor a dollar sign, the character is inserted in the new field.



Example: Using the EDIT Function

In the following example, assume that &EMP_ID is a 9-character alphanumeric field and &FIRST_NAME is a 10-character alphanumeric field. Suppose you want to edit &EMP_ID by inserting hyphens, then display the first initial and last name of an employee.

-SET &EMPIDEDIT = EDIT(&EMP_ID,'999-99-9999');
-SET &FIRST_INIT = EDIT(&FIRST_NAME,'9$');
-TYPE &EMPIDEDIT &FIRST_INIT &LAST_NAME

Assume that &EMP_ID is 516888704 and &FIRST_NAME is 'EDWARD'. The client application receives:

516-88-8704 E Jones

Top of page

x
Decoding a Value

How to:

Many times the value of a field is coded. For example, the field &SEX may contain code F for female employees and code M for male employees, reducing the storage requirement for the value.

One method for decoding (expanding) these values is to include a series of nested -IF...THEN...ELSE commands (for example, -IF &SEX IS 'M' THEN 'MALE' ELSE 'FEMALE';), but this becomes very cumbersome. As an alternative, Dialogue Manager provides the DECODE function.



x
Syntax: How to Decode a Value
DECODE fieldname (code1 result1 code2 result2...[ELSE default]);

where:

fieldname

Is an alphanumeric or numeric field to be decoded.

code

Is the code to be expanded.

result

Is the expanded value to be substituted for code. If this value has embedded blanks or commas, or if it is a negative number, enclose it in single quotation marks.

Use either commas or blanks to separate the code from the result, or one pair from another pair.

default

Is the value to be assigned if the code is not found. If you do not supply a default, Dialogue Manager assigns a blank or zero.

Code up to 40 lines of pairs of elements (a pair is a code and a result), or 39 if you include an ELSE.



Example: Using the DECODE Function

In the following example, values (results) are substituted for the codes FED, STAT, CITY, FICA, HLTH, and SAVE:

-SET &DEDUCTION = DECODE &DED_CODE(FED 'TAXES' STAT 'TAXES'
-  CITY 'TAXES' FICA 'FICA' HLTH 'INSURANCE' SAVE 'PERSONAL'
-  ELSE 'OTHER');
-TYPE &DEDUCTION


x
Syntax: How to Store Codes and Results in a Separate File
DECODE &testvar (filename [ELSE default]);

where:

filename

Is the symbolic name of a physical file.

Each record in the file must contain one pair of elements (a code and a result), separated by a comma or blanks. For example:

F FEMALE
M MALE

DECODE tests each record in filename; if the value of &testvar matches a value in the first column of filename, DECODE returns the value in the second column. For example, if the above file is named GENDER, the following results in MALE:

-SET &SEX = M;
-SET &SEX = DECODE &SEX(GENDER);
-TYPE &SEX

In the following example, &TAKE is set to 0 for &SELECT values found in filename, and is set to 1 in all other cases:

&TAKE = DECODE &SELECT (filename ELSE 1);
&VALUE = IF &TAKE IS 0 THEN...ELSE...;

Top of page

x
Creating an Indexed Variable

How to:

You can append the value of one variable to the value of another variable, creating an indexed variable. This feature applies to both local and global variables.

If the indexed value is numeric, the effect is similar to that of an array in traditional computer programming languages. For example, if the value of index &K varies from 1 to 10, the variable &AMOUNT.&K refers to one of ten variables, from &AMOUNT1 to &AMOUNT10.

A numeric index can be used as a counter; it can be set, incremented, and tested in a procedure.



x
Syntax: How to Create an Indexed Variable
-SET &name.&index[.&index...] = expression;

where:

&name

Is a variable.

.&index

Is a numeric or alphanumeric variable whose value is appended to &name. The period is required.

When more than one index is used, all index values are concatenated and the string appends to the name of the variable.

For example, &V.&I.&J.&K is equivalent to &V1120 when &I=1, &J=12, and &K=0.

expression

Is a valid expression. For information on the kinds of expressions you can write, see the appropriate Creating Reports manual.



Example: Using an Indexed Variable in a Loop

An indexed variable can be used in a loop. The following example creates the equivalent of a DO loop used in traditional programming languages:

-SET &N = 0;
-LOOP
-SET &N = &N+1;
-IF &N GT 12 GOTO OUT;
-SET &MONTH.&N=&N;
-TYPE &MONTH.&N 
-GOTO LOOP
-OUT

In this example, &MONTH is the indexed variable and &N is the index. The value of the index is supplied through the command -SET; the first -SET initializes the index to 0, and the second -SET increments the index each time the procedure goes through the loop.

If the value of an index is not defined prior to reference, a blank value is assumed. As a result, the name and value of the indexed variable do not change.

Indexed variables are included in the system limit of 1024, which includes variables reserved by FOCUS.


Top of page

x
Removing Trailing Blanks From Variables With the TRUNCATE Function

How to:

The Dialogue Manager TRUNCATE function removes trailing blanks from Dialogue Manager amper variables and adjusts the length accordingly.

The Dialogue Manager TRUNCATE function has only one argument, the string or variable to be truncated. If you attempt to use the Dialogue Manager TRUNCATE function with more than one argument, the following message is generated:

(FOC03665) Error loading external function 'TRUNCATE'

This function can only be used in Dialogue Manager commands that support function calls, such as -SET and -IF commands. It cannot be used in -TYPE or -CRTFORM commands or in arguments passed to stored procedures.

Note: A user-written function of the same name can exist without conflict.



x
Syntax: How to Remove Trailing Blanks From Variables
-SET &var2 = TRUNCATE(&var1);

where:

&var2

Is the Dialogue Manager variable to which the truncated string is returned. The length of this variable is the length of the original string or variable minus the trailing blanks. If the original string consisted of only blanks, a single blank, with a length of one is returned.

&var1

Is a Dialogue Manager variable or a literal string enclosed in single quotation marks. System variables and statistical variables are allowed as well as user-created local and global variables.



Example: Removing Trailing Blanks

The following example shows the result of truncating trailing blanks:

-SET &LONG = 'ABC   ' ;
-SET &RESULT =  TRUNCATE(&LONG);
-SET &LL = &LONG.LENGTH;
-SET &RL = &RESULT.LENGTH;
-TYPE LONG   =  &LONG  LENGTH =  &LL
-TYPE RESULT =  &RESULT LENGTH =  &RL

The output is:

LONG   =  ABC     LENGTH =  06
RESULT =  ABC LENGTH =  03

The following example shows the result of truncating a string that consists of all blanks:

-SET &LONG = '      ' ;
-SET &RESULT =  TRUNCATE(&LONG);
-SET &LL = &LONG.LENGTH;
-SET &RL = &RESULT.LENGTH;
-TYPE LONG   =  &LONG  LENGTH =  &LL
-TYPE RESULT =  &RESULT LENGTH =  &RL

The output is:

LONG   =          LENGTH =  06
RESULT =    LENGTH =  01

The following example uses the TRUNCATE function as an argument for EDIT:

-SET &LONG = 'ABC   ' ;
-SET &RESULT =  EDIT(TRUNCATE(&LONG)|'Z','9999');
-SET &LL = &LONG.LENGTH;
-SET &RL = &RESULT.LENGTH;
-TYPE LONG   =  &LONG  LENGTH =  &LL
-TYPE RESULT =  &RESULT LENGTH =  &RL

The output is:

LONG   =  ABC     LENGTH =  06
RESULT =  ABCZ LENGTH =  04

Top of page

x
Using Variables to Alter Commands

A variable can refer to a FOCUS command or to a particular field. Therefore, the command structure of a procedure can be determined by the value of the variable.



Example: Using a Variable to Control What the TABLE Command Prints

In this example, the variable &FIELD determines the field to print in the TABLE request.

In the file named SALES, the variable &FIELD can display the values RETURNS, DAMAGED, or UNIT_SOLD.

TABLE FILE SALES
 .
 .
 .
PRINT &FIELD
BY PROD_CODE
 .
 .
 .

Top of page

x
Controlling Process Flow Using STATE, &EXITRC, and –IF

You can use the STATE command to test for the existence of a particular physical file. This command is typically used with an –IF test against the &EXITRC system variable to control the flow of a procedure.

The STATE command is supported for physical files names on all platforms. The following are examples of physical file names as represented on a variety of platforms: car.foc, c:\car.foc, /tmp/car.foc, IADMIN/FOCUS(CAR), /QSYS.LIB/IADMIN.LIB/FOCUS.FILE(CAR.MBR), IADMIN:[IADMIN]CAR.FOC, IADMIN.CAR.DATA.



Example: Controlling Process Flow Using STATE, &EXITRC, and –IF

This example creates a car.foc file, unless one already exists in the current directory).

STATE car.foc
-IF &EXITRC EQ 0 THEN GOTO RESUME ;
CREATE FILE CAR
-RESUME

Note: STATE does not require an explicit –RUN command because the &EXITRC system variable automatically forces execution of the command stack.


iWay Software