SAS How To's

5 Simple Ways to Create an Empty Dataset in SAS

If you work with data, sometimes you need to define a data set for later use. In this article, we discuss 5 simple ways to create an empty dataset in SAS. We demonstrate how to do this based on an existing dataset and how to do this defining a new table structure.

Define an Empty Dataset with a New Structure

In this section, we show how to create an empty dataset in SAS with a new table structure, i.e., the new dataset isn’t based on an existing dataset. You can do this with a SAS data step or with SQL code.

Using a Data Step

There are two easy ways to define the structure of an empty data set with a SAS data step. You can do this with the attrib statement or the format statement.

With both methods, you can define the variable name, the variable length, and the variable format. However, with the attrib statement, you can also define the variable label.

Finally, when you have defined all the variables of the new data set, you need the stop statement to prevent SAS from continuing processing. If you omit the stop statement, SAS will create a data set with the desired column, but with one empty row.

data work.empty_ds1;
attrib 
	customer_id	length=8 format=best12. label="Customer ID"
	customer_name length=$50 format=$50. label="Customer Name"
	customer_birthday length=8 format=date9. label="Customer Birthday";
stop;
run;
data work.empty_ds2;
format customer_id best12.
	customer_name $50.
	customer_birthday date9.;
stop;
run;

The image below shows that both methods generate tables with the same structure. However, only with the first method (work.empty_ds1) you can define the column labels.

Comparison of 2 Methods to Create an Empty Data Set
Comparison of 2 Methods to Create an Empty Data Set

Using PROC SQL

You can also use PROC SQL to create an empty data set with a new table structure. This method is very similar to the first data step method. That is to say, you can use SQL to define the variable’s name, length, format, and label. However, compared to the Data Step methods, it’s necessary to explicitly define the variable type.

proc sql;
create table work.empty_ds3
	(
	customer_id	num length=8 format=best12. label="Customer ID",
	customer_name varchar length=50 format=$50. label="Customer Name",
	customer_birthday num length=8 format=date9. label="Customer Birthday"
	);
quit;

Define an Empty Dataset Based on an Existing Dataset

In this section, we demonstrate two ways to create an empty data set based on an existing table. In order words, we create an empty copy of this table.

Using a Data Step

You can use the SAS Data Step to create this empty table. With the set statement you specify the table you want to copy and with the stop statement, you prevent SAS from processing any rows. Below we show an example with code.

data work.empty_ds4;
	set sashelp.iris;
	stop;
run;

Using PROC SQL

The last of 5 simple ways to create an empty dataset in SAS, is using PROC SQL. First, you specify the name of your new, empty table. Then, you use the like statement to define the table structure. For example, with the code below we create an empty data set that has the same structure as the Iris data set from the SASHELP library.

proc sql;
	create table work.empty_ds5
	like sashelp.iris;
quit;

One thought on “5 Simple Ways to Create an Empty Dataset in SAS

Comments are closed.