6. Functions : Stat Library Functions : MODAL Function : Example—MODAL function:
 
Share this page                  
Example—MODAL function:
/*
** Identify the most frequently occurring surname in an HR database
*/
initialize()=
declare
    array_name           = array of stringobject;
    array_results        = array of stringobject;
    dbso                 = DBSessionObject;
    msg                  = varchar(256) not null;
    n                    = integer not null;
    ret                  = integer not null;
enddeclare
{
    /*
    ** Connect to the database
    */
    ret = dbso.Connect(database = 'xxxxxxxx::testdb');
    CurFrame.DBSession = dbso;
    /*
    ** Load the surnames in the roster into an array
    */

    n = 1;

    select lastname as array_name[n].Value
    from hr_table
    {
        n = n + 1;
    };
    /*
    ** Pass the array of surnames to the Modal function
    */

    array_results = modal(list = array_name);
    /*
    ** Loop through the results to report all modal values
    */
    
    if array_results.AllRows = 0 then
        msg = 'No modal value';
    else
        msg = 'The modal value(s) is/are: ';

        for n = 1 to array_results.LastRow do
            msg = msg + HC_NEWLINE;
            msg = msg + array_results[n].Value;
        endfor;
    endif;
    /*
    ** Report the results
    */

    message msg;

    /*
    ** Disconnect from the database
    */

    dbso.Disconnect();
}
Sample output is shown in the following illustration: