Audit Trail Datasets (to track modifications) in SAS

Posted: March 24, 2011 in SAS Tips & Tricks

Definition of an Audit Trail:
The audit trail is an optional SAS file that you can create in order to log modifications to a SAS data file. Each time an observation is added, deleted, or updated, information is written to the audit trail about who made the modification, what was modified, and when.
Many businesses and organizations require an audit trail for security reasons. The audit trail maintains historical information about the data, which gives you the opportunity to develop usage statistics and patterns. The historical information enables you to track individual pieces of data from the moment they enter the data file to the time they leave.
The audit trail is also the only facility in SAS that stores observations from failed append operations and that were rejected by integrity constraints. The audit trail enables you to write a DATA step to extract the failed or rejected observations, use information describing why the observations failed to correct them, and then reapply the observations to the data file.

The audit trail is created by the default Base SAS engine and has the same libref and member name as the data file, but has a data set type of AUDIT. It replicates the variables in the data file and additionally stores two types of audit variables:
1) _AT*_ variables, which automatically store modification data
2) user variables, which are optional variables you can define to collect modification data

You initiate an audit trail in the DATASETS procedure with the AUDIT statement.

Eg:
/* Create SALES data set. */
data mylib.sales;
length product $9;
input product invoice renewal;
datalines;
FSP 1270.00 570
SAS 1650.00 850
STAT 570.00 0
STAT 970.82 600
OR 239.36 0
SAS 7478.71 1100
SAS 800.00 800
;
run;
/* Create an audit trail with a user variable. */
proc datasets lib=mylib nolist;
audit sales;
initiate;
user_var reason_code $ 20;
run;

The following example inserts an observation into MYLIB.SALES.DATA and prints the update data in the MYLIB.SALES.AUDIT.
/* Do an update. */
proc sql;
insert into mylib.sales
set product = ‘AUDIT’,
invoice = 2000,
renewal = 970,
reason_code = “Add new product”;
quit;
/* Print the audit trail. */
proc sql;
select * from mylib.sales(type=audit);
quit;

Updated Data in MYLIB.SALES.AUDIT Dataset:
The SAS System

product reason_code _ATOPCODE_ _ATUSERID_ _ATDATETIME_
________________________________________________________________________________
AUDIT Add new product DA xxxxxx 31OCT2007:11:24:32

Source: SAS Online Doc

Leave a comment