%***********************************************************************; %* MACRO NAME: UNION.SAS *; %* *; %* PURPOSE: *; %* Concatenate two data sets without truncating any character *; %* variables which appear in both data sets, but with different *; %* lengths. *; %* *; %* PROGRAMMER: Martha Cox 24-NOV-2009 *; %* Cloned from SAS Institute Sample Library. *; %***********************************************************************; %* USAGE: Call in a SAS program, not within a data step. *; %* *; %* example: %union(dsn1=cihi00, *; %* dsn2=cihi01, *; %* out=cihiall *; %* ); *; %* *; %* INPUT PARAMETERS: *; %* DSN1 = Name of the first input data set *; %* DSN2 = Name of the second input data set *; %* Valid values: *; %* Default: *; %* If a two-level name is used (permanent data set), the *; %* calling program must provide the libname statement. *; %* The order of the data sets is not important. *; %* *; %* OUT = Name of the output (combined) data set *; %* Valid values: *; %* Default: *; %* If a two-level name is used (permanent data set), the *; %* calling program must provide the libname statement. *; %* *; %* LIMITATIONS: *; %* -- Calling program should not have existing data sets called *; %* WORK.OUT1 or WORK.OUT2. *; %* -- Default directory should not contain a file called *; %* COMBINED.SAS. *; %* *; %***********************************************************************; %macro union(dsn1=, /*Name of the first data set */ dsn2=, /*Name of the second data set */ out= /*Name of output (combined) data set */ ); proc contents data=&dsn1 out=out1 (keep=name type length where=(type=2) ) noprint ; run; proc contents data=&dsn2 out=out2 (keep=name type length where=(type=2) ) noprint ; run; data _null_; file "combined.sas"; merge out1 out2(rename=(length=length2)) end=last ; by name; if _n_ = 1 then put "data &out;"; vlen = max(length,length2); put " length " name " $" vlen 2. ";"; if last then do; put " set &dsn1 &dsn2 ;"; put "run;"; end; run; %include "combined.sas"; %mend union;