You are on page 1of 17

User Defined Functions

User-Defined Database Functions


A database user-defined function (UDF) can be referenced in
• explicit pass-through code (passed verbatim to the database)
• implicit pass-through code (requires a mapping).

A database user-defined function can be used in implicit pass-through code


if the SAS/ACCESS engine has an SQL function mapping for the user-defined
function.

1
Copyri g ht © S A S Insti tute Inc. A l l ri g hts reserved.

List the SQL Function Mappings


SQL_FUNCTIONS_COPY=saslog

22

Copyri g ht © S A S Insti tute Inc. A l l ri g hts reserved.

SAS/ACCESS software uses the SQL dictionary to determine what it should pass into the database
as well as how the database functions map to SAS functions. The SAS SQL dictionary for Oracle
can be viewed by submitting a LIBNAME statement with SQL_Functions_Copy=saslog. Additional
Oracle user-defined functions can be appended to this list.
2

Using User-Defined Database Functions


To use a database user-defined function in an implicit pass-through query,
add the user-defined function to the SAS SQL function mapping dictionary.

3
Copyri g ht © S A S Insti tute Inc. A l l ri g hts reserved.

The data step creates a dataset that describes a new SAS function and the user-defined Oracle
function. These two functions must have the same name and return the same values. This dataset is
then used to append the SAS function to the existing in-memory SAS function list.

Using User-Defined Database Functions


Use the Import User Defined Functions tool to add the function to the
Expression Editor.

4
Copyri g ht © S A S Insti tute Inc. A l l ri g hts reserved.
3

Using User-Defined Database Functions


Use the Import User Defined Functions tool to add the function to the
Expression Editor.

5
Copyri g ht © S A S Insti tute Inc. A l l ri g hts reserved.

Users can add the function to an existing container or define their own. Users then will select the
library that contains the user-defined function.

Using User-Defined Database Functions


The user-defined function will be available for use in an implicit pass-
through query and will be shown in the Expression Editor.

6
Copyri g ht © S A S Insti tute Inc. A l l ri g hts reserved.
4

Creating and Using Oracle User-Defined Functions


This demonstration illustrates creating a user-defined function in Oracle and adding the
function to the SQL function mappings for Oracle. The function is also added to the
Expression Editor and then used in an implicit pass-through expression.
1. Create a user-defined function in Oracle.
a. Select Start  All Programs  Oracle – OraDb11g_home1  Application Development
 SQL Plus.
b. At the Enter user-name: prompt, type educ and press Enter.
c. At the Enter password: prompt, type educ and press Enter.

d. Turn on Quick Edit mode.


This allows users to paste into the SQL Plus window.
1) Right click on the SQL Plus window’s title bar and select Properties.
5

2) Under Edit Options, check the box next to QuickEdit Mode.

3) Select OK.
6

e. At the SQL> prompt, type or copy the following statements and press Enter:
create or replace function "RISKRATING"
(variable1 in NUMBER)
return VARCHAR2
is
BEGIN
IF MOD(variable1, 2) = 0 THEN
RETURN 'High Risk';
END IF;
IF MOD(variable1, 2) <> 0 THEN
RETURN 'Low Risk';
END IF;
END;
/
7

f. At the SQL> prompt, type or copy the following statements and press Enter:
grant execute on riskrating to public;
commit;

The grant execute to public statement gives all users access to the function.
g. At the SQL> prompt, type quit; and press Enter to close the SQL Command Line window.
2. If necessary, access SAS Data Integration Studio using Bruno’s credentials.
a. Select Start  All Programs  SAS  SAS Data Integration Studio.
b. Verify that the connection profile is My Server.
c. Click OK to close the Connection Profile window. The Log On window appears.
d. Enter Bruno as the user ID and Student1 as the password.
e. Click OK to close the Log On window.
3. If necessary, import starter metadata for the DIFT Customer Riskrating Report job.
Note: If the DIFT Oracle Library and the ORACLE Customer Dim table exist in the
metadata repository, the import is not necessary.
a. Right-click the Data Mart Development folder and select Import  SAS Package.
b. In the Import SAS Package window, click Browse.
c. From the D:\workshop\dift\solutions\ELP_Packages folder, select
DI_ELP_UserDefinedDemo.spk.
d. Click OK.
e. Click Next.
f. Verify that all metadata objects are selected.
8

g. Click Next  Next  Next  Next  Next  Finish.


4. List the current functions in the SAS SQL function mappings dictionary.
a. In SAS Data Integration Studio, select Tools  Code Editor.
b. On the Folders tab, expand Data Mart Development  Orion Source Data.
c. Right-click DIFT Oracle Library and select View Libname.
d. Copy the LIBNAME statement and paste it in the Code Editor.
e. Add the option sql_functions_copy=saslog to the LIBNAME statement.

f. Click Run.
g. Click the Log tab.

The current SAS SQL function mappings for Oracle are shown.
9

3. Create a function-reference data set for user-defined functions.


a. Copy the following DATA step code and the LIBNAME statement to the Code Editor.
data sasdata.udforacle;
sasfuncname ='RISKRATING';
sasfuncnamelen = 10;
dbmsfuncname = 'RISKRATING';
dbmsfuncnamelen = 10;
FUNCTION_CATEGORY = "";
FUNC_USAGE_CONTEXT = "";
FUNCTION_RETURNTYP = "";
FUNCTION_NUM_ARGS = 1;
CONVERT_ARGS = 0;
ENGINEINDEX = 0;
output;
run;

LIBNAME diftora ORACLE PATH=xe AUTHDOMAIN="OracleAuth"


sql_functions="EXTERNAL_APPEND=sasdata.udforacle"
sql_functions_copy=saslog;
The SASDATA library is a shared preassigned library.
b. Click Run.
10

c. Click the Log tab.

The RISKRATING function appears at the bottom of the function list displayed after the
LIBNAME statement.
d. Select File  Close to close the Code Editor. Do not save changes.
4. Create a new library definition for Oracle and add the SQL_FUNCTIONS= option.
a. If necessary, expand the Data Mart Development  Orion Reports folders.
b. Right-click the Database Processing folder and select New  Library.
c. Select Oracle Library and click Next.
d. Type DIFT Oracle UDF Library as the Name.
e. Verify that the Location is /Data Mart Development/Orion Reports/Database Processing.
f. Click Next.
g. Use the arrow to move SASApp to the selected box and click Next.
h. Type oraudf as the Libref.
i. Click the Advanced Options button.
j. Click the Other Options tab.
k. Enter sql_functions="EXTERNAL_APPEND=sasdata.udforacle" in the Option(s) to be
appended field.

l. Click OK to close the Advanced Options window.


m. Click Next.
n. Verfiy DIFT Oracle Server is selected for the Database Server.
o. Verify that the Connection is Connection: DIFT Oracle Server.
p. Verify that the Default Login is (none).
11

q. Click Next.
r. Verify the summary information and click Finish.
5. Use the Import User Defined Functions tool to add the RiskRating function to the Expression
Editor.
a. Select Tools in the SAS Data Integration Studio toolbar.
b. Select Import User Defined Functions….

c. Type User Defined in the Container field.


d. Next to Library, click the ellipsis.
12

e. Select the DIFT Oracle UDF Library from the Data Mart Development\Orion
Reports\Database Processing folder as the Library.

f. Move the Native function set to the Selected box with the arrow.

g. Select the Preview button.


13

h. Verify that the RISKRATING function appears for import.


i. Click close to exit the Preview window.
j. Select the Import button.

k. If the import completed successfully, click No to exit the window.


l. Select Close to exit the Import User Defined Functions window.
6. Create a job that uses the RiskRating Oracle user-defined function.
a. Click the Folders tab.
b. Expand Data Mart Development  Orion Reports  Database Processing.
c. Right-click the Database Processing folder and select New  Job.
d. Name the job DIFT Customer Riskrating Report.
e. Verify that the location is /Data Mart Development/Orion Reports/Database Processing.
f. Click OK.
g. From the Database Processing folder, add the ORACLE Customer Dim table to the job.
h. Click the Transformations tab.
i. Expand the SQL group.
j. Add an Extract transformation to the job.
k. Connect the ORACLE Customer Dim table to Extract.

7. Configure the Extract transformation.


a. Right-click the Extract transformation and select Properties.
b. Click the Mappings tab.
c. Verify that 11 columns are propagated and mapped.

d. Click to add a new column to the target table.


e. Name the new column CustomerRisk.
f. Click in the Expression field for CustomerRisk and select Advanced.
g. In the Expression Editor, click the Functions tab.
14

h. Double-click the User Defined folder.

Note: The RISKRATING function is available in the Expression Editor from the Oracle category.
i. Double-click RISKRATING to add it to the expression.
j. Click the Data Sources tab.
k. Expand the CUSTOMER_DIM table.
l. Double-click CUSTOMER_ID so that it replaces the highlighted argument to the RiskRating
function in the Expression Text field.
15

m. Click OK to close the Expression builder.


n. Right-click and select Fix Warning  Update Mappings to Match Columns Used in
Expression to establish appropriate mappings.
o. Click OK to close the Extract Properties.
8. Add DBMS tracing options to the job.
a. Right-click in the background of the job and select Properties.
b. Click the Precode and Postcode tab.
c. Select the Precode option.
d. Enter options sastrace=',,,d' sastraceloc=saslog nostsuffix; in the Precode area.

e. Click OK to close the Extract Properties.


9. Register the target table.
a. Right-click the Extract work table and select Register Table.
b. Type ORACLE Customer Risk as the Name.
c. Verify that the Location is /Data Mart Development/Orion Reports/Database Processing.
d. Click the Physical Storage tab.
e. Enter CustomerRisk as the physical name.
f. Select DIFT Oracle UDF Library as the Library.

g. Click OK to close the Extract properties.


10. Update the library reference for the source table.
a. Right-click the ORACLE Customer Dim table and select Properties.
b. Click the Physical Storage tab.
16

c. Select DIFT Oracle UDF Library as the Library.

d. Click OK to close the Properties window.


11. Select File  Save to save the job metadata.
12. Check for database processing.

a. Click to check database processing.


b. Verify that Extract will process in Oracle.

13. Execute the job and view the results.


a. Click Run.
b. Verify that the job completes successfully.
c. Click the Log tab.
17

d. Scroll to the PROC SQL code.

Implicit pass-through was used to pass the RiskRating function to Oracle.


e. Scroll down to the CREATE TABLE statement.

The SAS/ACCESS to Oracle engine prepared an Oracle CREATE TABLE statement that
references the Oracle user-defined RISKRATING function.
f. Select File  Close to close the job.

You might also like