User Defined Functions
User Defined Functions
User Defined Functions
1
Copyri g ht © S A S Insti tute Inc. A l l ri g hts reserved.
22
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
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.
4
Copyri g ht © S A S Insti tute Inc. A l l ri g hts reserved.
3
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.
6
Copyri g ht © S A S Insti tute Inc. A l l ri g hts reserved.
4
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
f. Click Run.
g. Click the Log tab.
The current SAS SQL function mappings for Oracle are shown.
9
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.
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….
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.
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
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.