Using the SAS_PUT() Function in the DB2 Database

Implicit Use of the SAS_PUT() Function

After you install the formats that SAS supplies in libraries inside the DB2 database and publish any custom format definitions that you created in SAS, you can access the SAS_PUT() function with your SQL queries.
If the SQLMAPPUTTO= system option is set to SAS_PUT and you submit your program from a SAS session, the SAS SQL processor maps PUT function calls to SAS_PUT() function references that DB2 understands.
This example illustrates how the PUT function is mapped to the SAS_PUT() function using implicit pass-through.
options sqlmapputto=sas_put;

%put &mapconn;
user=dbitest password=dbigrp1 server=spubox database=TESTDB
   sql_functions="EXTERNAL_APPEND=WORK.dbfuncext" sql_functions_copy=saslog

libname dblib DB2 &mapconn;

   /*-- Set SQL debug global options --*/
   /*----------------------------------*/
   options sastrace=',,,d' sastraceloc=saslog;

   /*-- Execute SQL using Implicit Passthru --*/
   /*-----------------------------------------*/
   proc sql noerrorstop;
      title1 'Test SAS_PUT using Implicit Passthru ';
      select distinct
         PUT(PRICE,Dollar8.2) AS PRICE_C
            from dblib.mailorderdemo;

   quit;
These lines are written to the SAS log.
options sqlmapputto=sas_put;

%put &mapconn;
user=dbitext password=dbigrp1 server=spubox database=TESTDB
   sql_functions="EXTERNAL_APPEND=WORK.dbfuncext" sql_functions_copy=saslog;

libname dblib DB2 &mapconn;

NOTE: Libref DBLIB was successfully assigned, as follows:
      Engine:        DB2
      Physical Name: spubox

         /*-- Set SQL debug global options --*/
         /*----------------------------------*/
        options sastrace=',,,d' sastraceloc=saslog;

         /*-- Execute SQL using Implicit Passthru --*/
         /*-----------------------------------------*/
         proc sql noerrorstop;
           title1 'Test SAS_PUT using Implicit Passthru ';
           select distinct
              PUT(PRICE,Dollar8.2) AS PRICE_C
            from dblib.mailorderdemo
                ;
DB2: AUTOCOMMIT is NO for connection 1
DB2: AUTOCOMMIT turned ON for connection id 1

DB2_1: Prepared: on connection 1
SELECT * FROM mailorderdemo

DB2: AUTOCOMMIT is NO for connection 2
DB2: AUTOCOMMIT turned ON for connection id 2

DB2_2: Prepared: on connection 2
 select distinct cast(sas_put(mailorderdemo."PRICE", 'DOLLAR8.2') as char(8))
        as PRICE_C from mailorderdemo

DB2_3: Executed: on connection 2
Prepared statement DB2_2

ACCESS ENGINE:  SQL statement was passed to the DBMS for fetching data.

               Test SAS_PUT using Implicit Passthru                        9
                                                13:42 Thursday, May 7, 2011

                             PRICE_C
                             _______
                              $10.00
                              $12.00
                              $13.59
                              $48.99
                              $54.00
                               $8.00
                              $14.00
                              $27.98
                              $13.99

           quit;
Be aware of these items:
  • The SQLMAPPUTTO= system option must be set to SAS_PUT to ensure that the SQL processor maps your PUT functions to the SAS_PUT() function and the SAS_PUT() reference is passed through to DB2.
  • The SAS SQL processor translates the PUT function in the SQL SELECT statement into a reference to the SAS_PUT() function.
    select distinct cast(sas_put("sas"."mailorderdemo"."PRICE", 'DOLLAR8.2')
       as char(8)) as "PRICE_C" from "sas"."mailorderdemo"
    A large value, VARCHAR(n), is always returned because one function prototype accesses all formats. Use the CAST expression to reduce the width of the returned column to be a character width that is reasonable for the format that is being used.
    The return text cannot contain a binary zero value (hexadecimal 00) because the SAS_PUT() function always returns a VARCHAR(n) data type and a DB2 VARCHAR(n) is defined to be a null-terminated string.
The SELECT DISTINCT clause executes inside DB2, and the processing is distributed across all available data nodes. DB2 formats the price values with the $DOLLAR8.2 format and processes the SELECT DISTINCT clause using the formatted values.

Explicit Use of the SAS_PUT() Function

If you use explicit pass-through (direct connection to DB2), you can use the SAS_PUT() function call in your SQL program.
This example shows the same query from Implicit Use of the SAS_PUT() Function and explicitly uses the SAS_PUT() function call.
options sqlmapputto=sas_put sastrace=',,,d' sastraceloc=saslog;

proc sql noerrorstop;
   title1 'Test SAS_PUT using Explicit Passthru';
   connect to DB2 (user=dbitest password=XXXXXXX database=testdb
      server=spubox);

   select * from connection to DB2
      (select distinct cast(sas_put("PRICE",'DOLLAR8.2') as char(8)) as
         "PRICE_C" from mailorderdemo);

disconnect from DB2;
quit;
The following lines are written to the SAS log.
options sqlmapputto=sas_put sastrace=',,,d' sastraceloc=saslog;

proc sql noerrorstop;
 title1 'Test SAS_PUT using Explicit Passthru';
 connect to DB2 (user=dbitest password=XXXXXXX database=testdb server=spubox);

 select * from connection to DB2
    (select distinct cast(sas_put("PRICE",'DOLLAR8.2') as char(8)) as
        "PRICE_C" from mailorderdemo);

            Test SAS_PUT using Explicit Passthru            2
                                            17:13 Thursday, May 7, 2011

                          PRICE_C
                          _______
                           $27.98
                           $10.00
                           $12.00
                           $13.59
                           $48.99
                           $54.00
                           $13.98
                            $8.00
                           $14.00

 disconnect from DB2;
 quit;
Note: If you explicitly use the SAS_PUT() function in your code, it is recommended that you use double quotation marks around a column name to avoid any ambiguity with the keywords. For example, if you did not use double quotation marks around the column name, DATE, in this example, all date values would be returned as today's date.
select distinct
   cast(sas_put("price", 'dollar8.2') as char(8)) as "price_c",
   cast(sas_put("date", 'date9.1') as char(9)) as "date_d",
   cast(sas_put("inv", 'best8.') as char(8))  as "inv_n",
   cast(sas_put("name", '$32.') as char(32)) as "name_n"
from mailorderdemo;