**************************************************************************************************** * PROGRAM OVERVIEW **************************************************************************************************** * * PROGRAM: appendfiles.sas * * Created (mm/dd/yyyy): 04/19/2015 * Last modified: 04/30/2015 * Version: 1.0 * *-------------------------------------------------------------------------------------------------- * PURPOSE: * To append two dataset of the same nature without having lengths and variable w.a.r.n.i.n.g.s. The largest * lengths for each variable will be kept. * * Program inputs: * -SAS data files of the same nature * * Program outputs: * -SAS data file (.SAS7BDAT format) containing the appended data * * PARAMETERS: * -FileToAppend = SAS dataset containing the file to append to the other file * -AppendToFile = SAS dataset containing the records to which new records will be appended * -OutFile = SAS dataset containing the records from FileToAppend and AppendToFile * * Programming Notes: * - The variable order of the output dataset will be the same from the AppendToFile first, * then new variables come after * *-------------------------------------------------------------------------------------------------- * CONTACT INFO: * Mini-Sentinel Coordinating Center * info@mini-sentinel.org * *-------------------------------------------------------------------------------------------------- * CHANGE LOG: * * Version Date Initials Comment (reference external documentation when available) * ------- -------- -------- --------------------------------------------------------------- * mm/dd/yy * ***************************************************************************************************; %macro ms_appendfiles(FileToAppend,AppendToFile,OutFile); %put =====> MACRO CALLED: APPENDFILES v1.0; *Get variable attributes; proc contents data=&FileToAppend. NOPRINT out=ToAppendFmts; run; proc contents data=&AppendToFile. NOPRINT out=AppendToFmts; run; *Create a variable list and keep the largest lengths and keep only the variables that are in common; data Combine; set ToAppendFmts(in=a) AppendToFmts; Name=UPCASE(NAME); run; proc sort data=combine; by NAME descending length; run; proc sort nodupkey data=combine; by NAME; run; data combine; set combine; format LengthCall $100. char $1.; if TYPE=2 then Char="$"; LengthCall=strip(NAME)||" "||strip(char)||Strip(put(LENGTH,best.))||"."; run; proc sql noprint; select LengthCall into :VarLengths separated by ' ' from combine; quit; %put &VarLengths.; *Keep order of the AppendToFile; proc sql noprint; select upcase(name) into :VarOrder separated by ' ' from AppendToFmts order by varnum; quit; %put &VarOrder.; *Create preformatted header; data Header; retain &VarOrder.; length &VarLengths.; call missing(of _All_); if _N_=0; run; *Append files (using set to avoid force warnings); data &OutFile.; set Header &AppendToFile. &FileToAppend.; run; *clean up; proc datasets library=work nolist nowarn; delete ToAppendFmts AppendToFmts Combine Header; quit; %put NOTE: ******** END OF MACRO: APPENDFILES v1.0 ********; %mend ms_appendfiles;