Friday, July 31, 2015

Code to Compare Data Strcuture

As I said in previous blog, we can compare the data structure using PROC COMPARE. Below is sample code.
data test;
    set sashelp.class; A=1;
    drop name;
run;

%macro comp(souce_dsn=, target_dsn=);
    proc compare base=&source_dsn(obs=0) comp=&target_dsn (obs=0) noprint;
    run;

    /*
    PROC COMPARE return code - SYSINFO:
        0400X Base data set has variable not in comparison 
        0800X Comparison data set has variable not in base  
        2000X Conflicting variable types
        0010X Variable has different length                     
    */
    %if %eval(%sysfunc(band(&sysinfo, 0400x)) = 0400x ) 
        or %eval(%sysfunc(band(&sysinfo, 0800X)) = 0800X ) 
        or %eval(%sysfunc(band(&sysinfo, 2000X)) = 2000X ) 
        or %eval(%sysfunc(band(&sysinfo, 0010X)) = 0010X ) 
    %then %do;
        %put ERROR: &source_dsn has different structure with &target_dsn;
    %end;
%mend;

%comp(souce_dsn=sashelp.class, target_dsn=work.test)

No comments: