• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / SAS / How to Combine Datasets Vertically in SAS

How to Combine Datasets Vertically in SAS

April 26, 2022 Leave a Comment

To combine datasets vertically in SAS, the easiest way is to “set” the datasets in a SAS data step.

data new;
    set dataset1 dataset2;
run;

You can also use PROC APPEND to append a dataset at the end of another dataset.

proc append base=dataset1 data=dataset2;
run;

When working with data in SAS, the ability to create new datasets is valuable. Various operations, such as merging and appending, allow us to create new datasets from existing datasets.

The easiest way to combine datasets vertically is by “setting” datasets with the set statement.

“Setting” datasets stacks the given datasets vertically and allows you to create a new dataset.

With this method, you can stack as many datasets as you want and append many datasets on top of one another.

Below is a simple example of how you can append two SAS datasets in a SAS Data Step.

data dataset1;
	input num;
	datalines;
4
1
5
;
run;

data dataset2;
	input num;
	datalines;
1
6
3
;
run;

data new;
    set dataset1 dataset2;
run;

The resulting dataset “new” is shown below.

num
  4	
  1	
  5	
  1	
  6	
  3

Appending SAS Datasets in Data Step with Different Variables and Data Types

When you go to combine multiple SAS datasets in a SAS Data Step and you have different variables, there are a few different things to understand.

First, if you have different columns, then in the newly created dataset you will have missing values for the records where the column didn’t exist in the input dataset.

Second, if you have columns with the same name and different data types, then you will get an error.

Below is an example of the output in SAS of combining datasets with “set” when you have different variables.

data dataset1;
	input num1;
	datalines;
4
1
5
;
run;

data dataset2;
	input num2;
	datalines;
1
6
3
;
run;

data new;
    set dataset1 dataset2;
run;

The resulting dataset is shown below.

num1  num2
   4     .	
   1     .	
   5     .	
   .     1	
   .     6	
   .     3

Using PROC APPEND to Combine Datasets Vertically in SAS

You can also use PROC APPEND to combine two datasets and append one dataset to another.

To use PROC APPEND, you pass two arguments. The first argument is the dataset you want to append to, and the second argument is the dataset you want to append.

With PROC APPEND, you don’t create a new dataset. Instead, you are appending the second dataset to the base dataset.

Below is an example of how you can append datasets with PROC APPEND in SAS using the same datasets as the first example from above.

proc append base=dataset1 data=dataset2;
run;

The resulting dataset “dataset1” is shown below.

num
  4	
  1	
  5	
  1	
  6	
  3

One other consideration is that with PROC APPEND, by default, the variables MUST be the same. If the variables in the two datasets aren’t the same, you can pass FORCE to force PROC APPEND to append the datasets.

proc append base=dataset1 data=dataset2 force;
run;

Hopefully this article has been useful for you to learn how to combine multiple datasets vertically in SAS.

Other Articles You'll Also Like:

  • 1.  SAS right() Function – Right Align Character Variables in Data Step
  • 2.  SAS year function – Get Year from Date Variable
  • 3.  SAS Delete Dataset – Use PROC Datasets to Delete SAS Files
  • 4.  SAS round – Rounding Numbers in a SAS Data Step
  • 5.  SAS select when – Evaluating Character Values for Conditional Processing
  • 6.  SAS Power Function – Exponentiate Numbers with ** in a Data Step
  • 7.  =">SAS Greater Than or Equal to with GE or >=
  • 8.  SAS trim – Remove All Trailing Blanks from String Variable in Data Step
  • 9.  SAS rand – Generate Random Numbers in a SAS Data Step
  • 10.  Round Number to Nearest Integer in SAS

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy