FindCommonVariables.sas

/****************************************************************************/
/*  Program Name  : FindCommonVariables.sas                                 */
/*  Purpose       : List variables appearing in both of two given data sets */
/*                                                                          */
/*--------------------------------------------------------------------------*/
/*  Author        : Matt Trzcinski                                          */
/*  Last Update   : 2017/10/25                                              */
/*                                                                          */
/*--------------------------------------------------------------------------*/
/*  Input(s)      : Two datasets                                            */
/*  Output(s)     : Log listing                                             */
/*                                                                          */
/*--------------------------------------------------------------------------*/
/*  Example(s)    : %FindCommonVariables(sashelp.class, sashelp.class);     */
/*                                                                          */
/*--------------------------------------------------------------------------*/
/*  Template      : %FindCommonVariables(ds1= ,ds2= );                      */
/*                                                                          */
/*--------------------------------------------------------------------------*/
/*  Note(s)       :                                                         */
/*                                                                          */
/*  Development   :                                                         */
/*                                                                          */
/****************************************************************************/

%macro SetSystemOptions(opt1, opt2, opt3);
  options &opt1. &opt2. &opt3.;
%mend;

%macro ListLength(list, delimiter=|);
  %if %sysevalf(%superq(list)=, boolean) %then %let count = 0;
  %else %let count = %eval(%sysfunc(countc(&list., &delimiter.)) + 1);
  &count
%mend;

%macro FindCommonVariables(ds1, ds2) / minoperator;

**********************************

*** Error Handling / Top Matter

**********************************;
  %local
    i
    listVars1         listVars2
    isTwoPartNameDS1  isTwoPartNameDS2
    libnamePart1      libnamePart2
    memnamePart1      memnamePart2
  ;

  %if not %sysfunc(exist(&ds1.)) %then %do;
    %put ERROR: [MACRO] Dataset %upcase(&ds1.) not found.;
    %return;
    %end;

  %if not %sysfunc(exist(&ds2.)) %then %do;
    %put ERROR: [MACRO] Dataset %upcase(&ds2.) not found.;
    %return;
    %end;

  %let originalNOTES = %sysfunc(getoption(notes));
  option nonotes;

**********************************

*** Determine Name Parts

**********************************;
  %let isTwoPartNameDS1 =  %sysfunc(findc(&ds1, .));
  %let isTwoPartNameDS2 =  %sysfunc(findc(&ds2, .));

  %if &isTwoPartNameDS1. %then %do;
    %let libnamePart1 = %scan(&ds1, 1, .);
    %let memnamePart1 = %scan(&ds1, 2, .);
    %end;
  %else %do;
    %let libnamePart1 = WORK;
    %let memnamePart1 = &ds1;
    %end;

  %if &isTwoPartNameDS2. %then %do;
    %let libnamePart2 = %scan(&ds2, 1, .);
    %let memnamePart2 = %scan(&ds2, 2, .);
    %end;
  %else %do;
    %let libnamePart2 = WORK;
    %let memnamePart2 = &ds2;
    %end;

**********************************

*** Get lists of variables

**********************************;
  proc sql noprint;
    select name
    into : listVars1 separated by ' '
    from dictionary.columns
    where libname = "%upcase(&libnamePart1.)"
      and memname = "%upcase(&memnamePart1.)"
    ;
  quit;

  proc sql noprint;
    select name
    into : listVars2 separated by ' '
    from dictionary.columns
    where libname = "%upcase(&libnamePart2.)"
      and memname = "%upcase(&memnamePart2.)"
    ;
  quit;

**********************************

*** Are ds1 variables in ds2?

**********************************;
  %put;
  %put Variables common to %upcase(&ds1.) and %upcase(&ds2.): ;
  %put;

  %do i = 1 %to %ListLength(&listVars1., delimiter = ' 'n );
    %let var = %scan(&listVars1., &i.);
    %if &var. in (&listVars2.) %then %put &var.;
    %end;

**********************************

*** Housekeeping

**********************************;
  %SetSystemOptions(&originalNOTES.);

%mend;

Powered by peut-publier

©2020 Matt Trzcinski