PROC APPEND In SAS (With Examples)

The PROC APPEND procedure can be used to append observations from one dataset to the end of another SAS dataset. It kind of stacks the datasets one after another.

The APPEND procedure functions the same as the APPEND statement in the PROC DATASETS procedure.

The only difference between the APPEND procedure and the APPEND statement in PROC DATASETS is the default for libref in the BASE= and DATA= options.

Basic Syntax:

				/* syntax */

proc append base=data-set-1 data=data-set-2;
run;
			

The following sample SAS datasets will be created to demonstrate different uses of the proc append procedure in SAS.

				/* create sample sas datasets */
data game1;
	input player $ points match;
	datalines;
Jackobe 190 96
Kjell   199 76 
Morten  170 96 
Steven  184 88 
Peter   193 90
;
proc print;
title 'Dataset: work.game1';
run;


data game2;
	input player $ points match;
	datalines;
Anna    187 72
Jan     199 89
Nils    180 97
Kristin 173 76
Michael 129 91
;
proc print;
title 'Dataset: work.game2';
run;
			
PROC APPEND In SAS With Examples

Create New Dataset With PROC APPEND

The proc append procedure in SAS appends data=option dataset to base=option dataset. If the base=dataset doesn’t exist then it creates a new dataset matching exactly the same structure as data=dataset and appends data values.

In the below example work.mastergame dataset doesn’t exist so after running proc append procedure, it creates a new dataset.

				/* create new dataset with proc append */
/* append work.game1 dataset to the work.mastergame dataset*/

proc append 
	base=work.mastergame 
	data=work.game1;
run;

/* view dataset */
proc print data=work.mastergame;
run;
			
create new dataset with proc append

Now let’s try to append another dataset work.game2 data into work.mastergame dataset so we will have all the data from game1 and game2 into mastergame dataset.

In this scenario work.mastergame does exist, it means no new dataset will be created. Only the work.game2 data will be appended at the end of the work.mastergame dataset.

				/* append game2 dataset to mastergame dataset*/
proc append 
	base=work.mastergame 
	data=work.game2;
run;

/* view dataset */
proc print data=work.mastergame;
run;
			
proc append in sas sample example

PROC APPEND With Mismatched Datasets

To use the proc append procedure your input datasets must be similar. The table structure, column names, data types and so on should match.

But what if a few columns are missing from the SAS dataset which is to be appended? It can be possible to append data forcefully, by using the FORCE keyword. It ignores the missing variables and the append data for matching variables. 

Let’s first see what happens if the number of variables doesn’t match and you still try to use proc append.

To demonstrate this case, let me delete one of the variables from the work.game1 dataset. So we will have three variables in the game2 dataset but only two variables in the game1 dataset.

				/* delete "match" column from work.game1 dataset*/

data game1 (drop=match);
	set game1;
run;
			

In this situation if you try to use the proc append then SAS throws an error saying “No appending done because of anomalies”.  It can be solved using the FORCE option with the proc append procedure.

				/* append game2 data into date1 dataset */

proc append base=game1 data=game2;
run;
			
				 NOTE: Appending WORK.GAME2 to WORK.GAME1.
 WARNING: Variable match was not found on BASE file. The variable will not be added to the BASE file.
 ERROR: No appending done because of anomalies listed above. Use FORCE option to append these files.
 NOTE: 0 observations added.
 NOTE: The data set WORK.GAME1 has 5 observations and 2 variables.
 NOTE: Statements not processed because of errors noted above.
 NOTE: PROCEDURE APPEND used (Total process time):
       real time           0.00 seconds
			

PROC APPEND With FORCE option.

The FORCE option forces the APPEND statement to concatenate data sets when the DATA= data set contains variables that either:

  • are not in the BASE= data set
  • do not have the same type as the variables in the BASE= data set
  • are longer than the variables in the BASE= data set.
				/* proc append with FORCE option*/

proc append base=game1 data=game2 force;
run;

/* view game1 dataset after proc append */
proc print data=game1; run;

			

When you run the above query you’ll see this warning: “WARNING: Variable match was not found on BASE file. The variable will not be added to the BASE file.” written in the log which you can ignore it.

proc append with force option

APPEND Data With PROC DATASETS

As stated earlier proc append procedure function similar to proc datasets procedure. Hence it is important to also know how proc datasets with APPEND statement work. 

In this example we will try to do the same thing: appending game2 dataset to game1 dataset where we have miss-matching variables.

Here also we need to use the FORCE option in order to avoid any error while appending datasets.

				/* proc datasets with append statement */
proc datasets memtype=data;
	append base=game1 data=game2 force;
	run;
quit;


/* view work.game1 dataset */
proc print data=game1; 
	title 'Dataset : work.game1'; 
run;
			
How To append datasets using proc datasets

FAQ – How To Use PROC APPEND In SAS

What is PROC APPEND in SAS? 

PROC APPEND is a procedure in SAS used to add (append) observations from one dataset to the end of another dataset.

When should I use PROC APPEND in SAS? 

You should use PROC APPEND in SAS when you have two datasets with the same variables and you want to add the observations from one dataset to the other.

What are the requirements for using PROC APPEND in SAS? 

The main requirement for using PROC APPEND in SAS is that the datasets you are appending should have the same variables. If there are variables in the dataset being appended that do not exist in the base dataset, SAS will give an error.

Can I use PROC APPEND to append datasets with different structures? 

Yes, you can use PROC APPEND to append datasets with different structures using FORCE option, but you need to ensure that the variables you are appending are compatible.

What happens if there are conflicts when using PROC APPEND in SAS? 

If there are conflicts when using PROC APPEND in SAS, such as variables with the same name but different types, SAS will give an error. You need to resolve these conflicts before using PROC APPEND.