PROC APPEND - appending data vertically
PROC APPEND is used to append data vertically in SAS in an already existing dataset.
Let us firstly initialize our library:
libname mylib '/home/u50789927/My_datasets';
PROC APPEND BASE = <base_data> DATA = <new_data_to_be_appended> <FORCE>;
BASE_DATA : Data where output will be appended (If base data does not exist then a new data will be created)
DATA : Dataset whose rows need to be appended with the base dataset.
FORCE: To be used when new_data_to_be_appended has extra columns as compared to base dataset.
Case 1: When Base_data does not exist
In the following code we are appending an already existing data SASHELP.SHOES to a base data MYLIB.proc_append_output. Since it does not exist as of now thus a new data will be created.
PROC APPEND BASE = MYLIB.PROC_Append_output DATA = SASHELP.SHOES; RUN;
Case 2: When Base_data already exists
Task: Append SASHELP.SHOES again to the already existing dataset proc_append_output
PROC APPEND BASE = MYLIB.PROC_Append_output DATA = SASHELP.SHOES; RUN; PROC CONTENTS DATA = MYLIB.PROC_Append_output; RUN;
Note our resultant data has now 790 (395 * 2) rows.
Case 3: When New_data has a missing column.
Let us create a new base dataset to understand this named PROC_Append_output2
PROC APPEND BASE = MYLIB.PROC_Append_output2 DATA = SASHELP.SHOES; RUN;
Let us try to again append SASHELP.SHOES by dropping Sales column.
PROC APPEND BASE = MYLIB.PROC_Append_output2 DATA = SASHELP.SHOES(Drop = Sales); RUN;
SAS will create the dataset but will show a warning in the log! In the appended dataset, Sales will have missing values.
Case 4: When New_data has extra columns.
To understand this let us create a dummy date where Sales2 is a new column as follows:
DATA MYLIB.DUMMY_DATA; SET SASHELP.SHOES; Sales2 = Sales*2; RUN;
Let us append this data in our already existing base data PROC_Append_output2
PROC APPEND BASE = MYLIB.PROC_Append_output2 DATA = MYLIB.DUMMY_DATA; RUN;
Appending in such a situation will lead to an error!
In this situation FORCE statement comes to our rescue. It will not throw up the error, but a warning and will discard the extra column!
PROC APPEND BASE = MYLIB.PROC_Append_output2 DATA = MYLIB.DUMMY_DATA FORCE; RUN;