Home > Back-end >  Proc compare loop through multiple datasets in a folder SAS
Proc compare loop through multiple datasets in a folder SAS

Time:07-09

I have two folders containing numerous datasets. Each folder contains identical datasets and I'd like to compare to ensure they are similar. Is it possible to loop through each folder and compare each dataset?

%macro compare(dpath=, cpath=,);

%do i = 1 %to n;

proc compare base = &dpath data = &cpath;
run;

%mend;

%compare(dpath=folder1_path, cpath=folder2_path);

CodePudding user response:

This macro will compare the contents of all data exactly as-is and output if there are any differences at all. If there are no differences, the dataset all_differences will not be created.

%macro compare(dpath=, cpath=);
    libname d "&dpath";
    libname c "&cpath";

    /* Save all datasets to macro variables:
       &dataset1 &dataset2 etc. */
    data _null_;
        set sashelp.vmember;
        where libname = 'D';

        call symputx(cats('name', _N_), memname);
        call symputx('n', _N_);
    run;

    proc datasets lib=work nolist;
        delete all_differences;
    quit;

    %do i = 1 %to &n;

        /* Compare dataset names and only output if they are unequal */
        proc compare base    = d.&&name&i 
                     compare = c.&&name&i 
                     out     = outcomp
                     outnoequal;
        run;

        /* Get the number of obs from outcomp */
        %let dsid = %sysfunc(open(outcomp));
        %let nobs = %sysfunc(attrn(&dsid, nlobs));
        %let rc   = %sysfunc(close(&dsid));

        /* If outcomp is populated, log the dataset with differences */
        %if(&nobs > 0) %then %do;
            data _difference_;
                length dsn $32.;
                dsn = "&&name&i.";
            run;

            proc append base=all_differences
                        data=_difference_
                        force;
            run;
        %end;
    %end;
%mend;

CodePudding user response:

Point librefs to the "folders". Get the lists of datasets. Use the list to drive the code generation.

%macro compare(dpath,cpath);
libname left "&dpath";
libname right "&cpath";

proc contents data=left._all_ noprint out=contents;
run;

data _null_;
  set contents;
  by membname;
  if first.memname;
  call execute(catx(' '
    ,'proc compare base=',cats('left.',memname)
    ,'compare=',cats('right.',memname)
    ,';run;'
  ));
run;

%mend;

%compare
(dpath=folder1_path
,cpath=folder2_path
);

To make it more robust you might want to do things like check that the member names in LEFT actually match the member names in RIGHT. Or add an ID statement to the generated PROC COMPARE code so that PROC COMPARE knows how to match observations, otherwise it will just match the observations in the order they appear.

  • Related