*------*;

*------%compare_contents:--Data Set Contents Comparison Macro ------*;

*------*;

/*

Macro %COMPARE_CONTENTS read_me notes:

Inputs:

dataset_listA list of SAS dataset names. No commas! Required input!

outdataAn option to name the output dataset name. By default is set to: "_compare".

If a different name is provided, the dataset will be available in the SAS

working directory after the macro executes. If it is left as the default,

"_compare", then the output datset will be deleted by the macro.

Not required to input.

printAn option to automatically print or not the output dataset.

By default set to "Y" for "yes" to print.

Set to anything besides Y to surpress printing.

Not required to input.

formatAn option to apply a format to the output indicator variables.

By default set to 8.0 so present variables are indicated by '1'

and non-present variables are indicated by '.'.

Not required to input.

Returns:

Prints (to SAS Listing) a dataset with one variable being a list of all the variable

names in all of the datasets and then 1 indicator variable per dataset. The indicator

variable is 1 if the variable is in that dataset and missing otherwise.

Example calls:

--This example inputs only the required dataset_list and uses defaults for the other optional inputs:

%compare_contents(dataset_list= dataset1 dataset2 dataset3);

run;

--This example inputs different options for the other inputs. An output dataset "cc_output" is created,

with indicator variables given an 8.1 format and nothing is automatically printed:

%compare_contents(dataset_list= dataset1 dataset2 dataset3, outdata=cc_output, print=N, format=8.1);

Other notes:

--Uses the macro %words which is also included in this file.

--Compares variable names in all caps. If there are different variables spelled the same but with

different capitalization, this macro will not pick that out.

*/

*------*;

*------WORDS macro -- START ------*;

/**

Macro %WORDS read_me notes:

Inputs:

stringA character string made up of multiple "words" each separated by blank space.

No commas!

Returns:

Returns only a count of the number of "words" in the character string

Example calls:

Used within another macro to assign a local macro variable the number of "words" in a character string.

%let num_var=%words(&var_list);

This creates the macro variable "num_var" which is the number of words(variables) in the macro input

variable &var_list.

**/

*** MACRO COUNTS NUMBER OF WORDS IN A STRING ***;

%macro words(string);

%local count word;

%let count=1;

%let word=%qscan(&string,&count);

%do %while(&word ne);

%let count=%eval(&count+1);

%let word=%qscan(&string,&count);

%end;

%eval(&count-1)

%mend words;

run;

*------WORDS macro ---- END ------*;

*------*;

*------*;

*------COMPARE_CONTENTS macro --- START ------*;

%macro compare_contents(dataset_list=, outdata=_compare, print=Y, format=8.0);

*Get number of datasets in the input dataset list;

%let num_datasets=%words(&dataset_list);

*Create a macro variable for each dataset name;

%do i=1 %to &num_datasets;

%let _ds&i=%scan(&dataset_list,&i);

%end;

*Loop through dataset name list getting the contents of each into another dataset;

%do i=1 %to &num_datasets;

*Get output from proc contents;

run; ods listing close; run;

proc contents data=&_ds&i; ods output Variables=_contents&i; run;

run; ods listing; run;

*Create variable name variable & indicator variables;

data _contents&i(keep=variable &_ds&i);

set _contents&i;

&_ds&i=1;

variable=upcase(variable);

*format the indicator variables, default is 8.0 so that present is '1' and not present is '.';

format &_ds&i &format;

run;

*Sort dataset before merge;

proc sort data=_contents&i; by variable;

run;

%end;

*Merge the contents datasets;

data _compare;

merge _contents1-_contents&num_datasets;

by variable;

run;

*Makes a copy of _compare if another output dataset name is provided;

data &outdata;

set _compare;

run;

*optional printed output, default setting is to print;

%if &print=Y %then %do;

*Print the contents datasets with indicator flag variables;

proc print data=&outdata;

title "Comparing Contents of the datasets: &dataset_list"; run; title;

%end;

*Delete temporary working datasets in macro, output dataset is not deleted iff a name other than _compare is provided;

proc datasets library=work nolist nowarn;

delete _contents1-_contents&num_datasets _compare;

run;

quit;

%mend;

*------COMPARE_CONTENTS macro --- END ------*;

*------*;