• 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 / Set Multiple Datasets in SAS Data Step

Set Multiple Datasets in SAS Data Step

September 14, 2022 Leave a Comment

In SAS, you can set multiple datasets in a single data step. If you have multiple datasets in a set statement, you will combine the datasets vertically.

data new;
    set dataset1 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.

In SAS, you can “set” multiple datasets in a single data step. If you have multiple datasets in a set statement, you will combine the datasets vertically.

“Setting” datasets stacks the given datasets vertically and creates 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 “set” 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

Using SET with Mutiple 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

Hopefully this article has been useful for you to learn what happens if you use set with multiple datasets in SAS.

Other Articles You'll Also Like:

  • 1.  SAS let – Create User-Defined Macro Variables in Your SAS Code
  • 2.  SAS year function – Get Year from Date Variable
  • 3.  SAS where in – Subset Data by Multiple Values in Data Step
  • 4.  Date Format ddmmmyyyy in SAS
  • 5.  SAS _n_ – How to Use the Automatic Variable _n_ in a Data Step
  • 6.  SAS Not In – How to Check if Variable is Not in List of Values
  • 7.  SAS max() Function – Find Maximum Value Across Columns in Data Step
  • 8.  SAS contains() – Check if Variable Contains a String in Where Statement
  • 9.  yyyy-mm-dd Date Format in SAS
  • 10.  SAS Proper Case – Capitalize First Letter of Word with propcase Function

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