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)

Wednesday, July 8, 2015

Compare the data structure using PROC COMPARE

PROC COMPARE is more useful than you think. It is mainly used to compare observations. And it is also a good tool to compare data structure. To compare only data structure, please exclude observation comparison using dataset option OBS=0.

Please see the code at below:
data test;
    set sashelp.class;
    drop name;
    test = 1;
run;

proc compare base=sashelp.class (obs=0) compare=test (obs=0) listvars; run;