Subhash Mantha SAS Developer subhashmantha@gmail.com.

Post on 17-Dec-2015

233 views 7 download

Transcript of Subhash Mantha SAS Developer subhashmantha@gmail.com.

Subhash ManthaSAS Developer

subhashmantha@gmail.com

Audit trailImportant in the following fields

Clinical TrialsFinancial CompaniesProduct Development

Answers four important questionsWhenWhyHowWho

Some of the parameters used for Trail CapturingDate TimeWho ran the programRun time of the program

Methods for capturing a trailSAS LogsExcel FilesXML filesSAS DatasetsCSV/Text Files

SAS Way of capturing a trailDatasets

AppendModifyRecreate

External filesAppendRecreate

Trail by datasetsData step

Set Modify

Proc stepAppend

Trail by external filesData step

File (recreate)File with mod option (append)

Proc stepProc export (Recreate /add new sheets to an

existing file)ODS CSVALL

Writing to external filesData stepProc stepSAS File functions

SAS File functionsFilename: Assigns a file reference in a data step Fexist: Checks if a file exists.Fopen : opens the file and creates a file handlerFclose : Closes the file that has been opened

using the file handlerFput : Writes the information to the file data

bufferFwrite : Writes data from file data buffer to the

actual file

Appending information to the bottom of a fileData step:Data _null_;File <fileref> <file options> mod;Set somedsname;Put fields we want;Run;File functions:Data _null_;Rc_open=filename(‘fileref’, “full file name along with path”);if rc_open=0 and fexist(‘fileref’) then do; fid=fopen(‘fileref’ , ’a’); putrc=fput(fid,<string to be written to the file>); writerc=fwrite(fid); closerc=fclose(fid);End;Run;

Advantages of using file functionsVerify if the file existsCheck if the file is in useWait until the file is available to useCatch exceptions if data write to a file failedHelp work around user locks

Concurrent access of datasetsFound in multi user systemsSeveral people trying to access the same fileOthers might have a file in read mode while

somebody else is trying to update the file

Methods to address concurrent access :“Assume” That it never happens as in a single

user systemTry to resolve the lock

Code%macro update_audit_file (name_of_program=, campaign_channel=,

result_of_run=&syscc._&sysrc._&sysmsg, name_of_audit_file=);

%local user date_of_run ; %let user=%sysget(USERNAME); %let date_of_run=%sysfunc(putn(%sysfunc(datetime()),datetime18.)); %let filerf=update;

proc sql noprint; select distinct scan(xpath,-1,'\') into :files separated by '|' from sashelp.vextfl where index(xpath,'.sas')>0; quit; %let rc=%sysfunc(filename(filerf,"&name_of_audit_file"));

%put rc=&rc; %if %sysfunc(fexist(&filerf)) %then %do;

%let open_rc=%sysfunc(fopen(&filerf,a)); %put &open_rc=; %do %while (&open_rc <= 0 ); %let rc_sleep=%sysfunc(sleep(10));

%put The file being updated is open please close it; %put SAS will try to update it in 10 seconds;

%let open_rc=%sysfunc(fopen(&filerf,a)); %end;

%if &open_rc > 0 %then %do; %let log=%sysfunc(getoption(altlog)); %let log_file=%sysfunc(scan(%str(&log),-1,'\'));

%let list=%sysfunc(getoption(altprint)); %let list_file=%sysfunc(scan(%str(&list),-1,'\')); %let path=%sysfunc(getoption(sasinitialfolder)); %let put_rc=%sysfunc(fput(&open_rc,

%str(&user,&date_of_run,&name_of_campaign,&campaign_channel,&path,&result_of_run,&log_file,&list_file,&files)));

%let write_rc=%sysfunc(fwrite(&open_rc)); %let close_rc=%sysfunc(fclose(&open_rc)); %end; %put &open_rc &close_rc;

%end; %else %do; %put the file &filerf does not exist;

%put creating file &name_of_audit_file; data _null_; file "&name_of_audit_file"; put 'user,dateofrun,name_of_campaign,campaign_channel,path,result_of_run,log_file,list_file,files'; %put %sysfunc(sysmsg());

%end; %mend update_audit_file;

Code Continued….

ReferencesSAS on-line documentation in Release 9.1.3Using SAS Functions in Data Steps, Yue Ye, The

R.W. Johnson Pharmaceutical Research Institute, Raritan, NJ Yong Lin, The Cancer Institute of New Jersey, New Brunswick, NJ

AcknowledgementsVitaly Feldman, SAS Institute Inc.