Suppose that you created a simple job in which a SAS table named ADVERSE is loaded
into another SAS table named ADVERSE2.
There is a one-to-one mapping of columns from ADVERSE to ADVERSE2. SAS Data Integration
Studio generates the following code for this job. Note how the status handling macro
and macro variables are used.
/*--------------------------------------------------
* Name: Simple Load Job
* Description: Code generated for Server SASMain
* Generated: Tue Jun 29 13:29:09 EDT 2008
*--------------------------------------------------*/
/* This is the setup required to capture the transformation return code */
%let JOB_RC=0;
%let TRANS_RC=0;
%global SQLRC;
%global SYSERR;
%macro RCSET(error);
%if (&error gt &TRANS_RC) %then
%let TRANS_RC=&error;
%if (&error gt &JOB_RC) %then
%let JOB_RC=&error;
%mend RCSET;
%let TRANS_RC=0;
options VALIDVARNAME=ANY;
/*
* Access the data for Test_lib
*/
LIBNAME testlib BASE "C:\sources\test";
%RCSET(&syslibrc);
%let SYSLAST=%nrquote(testlib."ADVERSE"n);
/***************************************************
* Name: Loader
* Description: Codegen
* Generated: Tue Jun 29 13:29:09 EDT 2008
****************************************************/
%let SYSOPT=;
%global DBXRC;
%global DWNUMIDX;
%global DBXLAST;
%let DBXRC=-1;
%let DWNUMIDX=-1;
%let DBXLAST=&SYSLAST;
/*--------------------------------------------------
* Name: DBWALOAD
* Description: Define load data macro
* Generated: Tue Jun 29 13:29:09 EDT 2008
*--------------------------------------------------*/
%macro dbwaload;
/* Determine if the target table exists */
%let DBXRC = %sysfunc(exist(testlib."ADVERSE_SORTED"n, DATA));
%if &DBXRC>0 %then
%do; /* if table exists*/
/*--------------------------------------------------
* Name: Truncate
* Description: Truncate a table
* Generated: Tue Jun 29 13:29:09 EDT 2008
*--------------------------------------------------*/
%put NOTE: Truncating table ...;
/* get the constraints from the table */
proc contents data = testlib."ADVERSE_SORTED"n
out2 = work.etls_constraints
noprint;
run;
/* get the number of constraints (number of rows) */
%let etl_numRows = 0;
%let etl_dsid=%sysfunc(open(work.etls_constraints));
%if (&etl_dsid gt 0) %then
%do;
%let etl_numRows = %sysfunc(attrn(&etl_dsid, NOBS));
%let etl_dsid = %sysfunc(close(&etl_dsid));
%end;
%let etl_primaryKey = NO;
%if (&etl_numRows gt 0) %then
%do; /* table has constraints */
/* determine if another table has a foreign key that points to this table */
data work.etls_constraints;
set work.etls_constraints;
type = upcase(type);
if (type eq "REFERENTIAL") then
do;
call symput("etl_primaryKey", "YES");
stop;
end;
/* delete any indexes that are created by another constraint */
if (type eq "INDEX" and ICOwn eq "YES") then
delete;
run;
%end; /* table has constraints */
%if (&etl_primaryKey eq YES) %then
%do; /* table has primary key and referential constraints */
data _null_;
put "WARNING: Because the target table has referential integrity "
constraint(s), an attempt will be made to truncate the table using "
the 'delete&039: statement in sql. This procedure may fail if the "
constraints are violated. Note that if the procedure is successful,
the rows will only be logically deleted, not physically deleted.";
run;
/* logically delete all the records from the table */
proc sql;
delete * from testlib."ADVERSE_SORTED"n;
quit;
%RCSET(&sqlrc);
%end; /* table has primary key and referential constraints */
%else
%do; /* table does not have a primary key and referential constraints */
%if (&etl_numRows gt 0) %then
%do; /* table has constraints */
/* delete the constraints from the table */
proc datasets lib=testlib nolist;
modify "ADVERSE_SORTED"n;
ic delete _all_;
quit;
%end; /* table has constraints */
/* physically delete all the records from the table */
data testlib."ADVERSE_SORTED"n;
set testlib."ADVERSE_SORTED"n;
stop;
run;
%RCSET(&syserr);
%if (&etl_numRows gt 0) %then
%do; /* table has constraints */
/* recreate the constraints on the table */
data _null_;
set work.etls_constraints end=eof;
if _n_ eq 1 then
do;
call execute("proc datasets lib=testlib nolist;");
call execute(& modify "ADVERSE_SORTED"n;');
end;
call execute(" " || recreate);
if eof then
call execute("quit;");
run;
%RCSET(&syserr);
%end; /* table has constraints */
%end; /* table does not have a primary key and referential constraints */
%put NOTE: Deleting work.etls_constraints...;
proc datasets lib=work nolist nowarn memtype=(data view);
delete etls_constraints;
quit;
%end; /* if table exists*/
/*--------------------------------------------------
* Name: Create Table
* Description: Create a new table
* Generated: Tue Jun 29 13:29:09 EDT 2008
*--------------------------------------------------*/
%if &DBXRC=0 %then
%do; /* if table does not exist*/
%put NOTE: Creating table ...;
data testlib."ADVERSE_SORTED"n
(label="ADVERSE2");
attrib "aedecod"n length=$21 format=$F21. informat=$F21.
label="AE Decode from Dictionary";
attrib "subjid"n length=8 format=BEST12. informat=F12.
label="Subject ID";
attrib "studyid"n length=$8 format=$F8. informat=$F8.
label="Study ID";
attrib "trtgrp"n length=$8 format=$F8. informat=$F8.
label="Treatment Group";
attrib "bodysys"n length=$20
label="Body System";
attrib "aesev"n length=$10
label="Severity";
attrib "aeout"n length=$15< br> label="Outcome";
stop;
run;
%RCSET(&syserr);
%end; /* if table does not exist*/
%let sqlrc = 0;
/*--------------------------------------------------
* Name: Append
* Description: Append new data
* Generated: Tue Jun 29 13:29:09 EDT 2008
*--------------------------------------------------*/
%put NOTE: Appending data ...;
proc append base=testlib."ADVERSE_SORTED"n
data=&DBXLAST (&SYSOPT) force;
run;
%RCSET(&syserr);
%mend dbwaload;
/*--------------------------------------------------
* Name: DBWALOAD
* Description: Execute load data macro
* Generated: Tue Jun 29 13:29:09 EDT 2008
*--------------------------------------------------*/
%dbwaload;