4.5 C
London
Thursday, December 19, 2024
HomeSASData Munging in SASHow to Create an Empty Dataset in SAS

How to Create an Empty Dataset in SAS

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

There are two common ways to create an empty dataset in SAS:

Method 1: Create Empty Dataset from Scratch

data empty_data;
attrib 
    var1 length=8 format=best12. label="var1"
    var2 length=$30 format=$30. label="var2"
    var3 length=8 format=best12. label="var3"
stop;
run;

Method 2: Create Empty Dataset from Existing Dataset

data empty_data;
    set existing_data;
    stop;
run;

In both methods, the stop statement prevents SAS from actually processing any rows.

This results in an empty dataset with variable names but no rows.

The following examples show how to use each method in practice.

Example 1: Create Empty Dataset from Scratch

We can use the following code to create an empty dataset called empty_data that contains four variables:

/*create empty dataset*/
data empty_data;
    attrib 
    employee_ID length=8 format=best12. label="Employee ID"
    employee_Name length=$30 format=$30. label="Employee Name"
    sales length=8 format=best12. label="Sales"
    sales_date length=8 format=date9. label="Sales Date";
    stop;
run;

We can then use proc contents to view the contents of the dataset:

/*view contents of dataset*/
proc contents data=empty_data;

From the output we can see that the dataset has four variables but zero observations, i.e. zero rows.

At the bottom of the output we can also see the names of the four variables we created:

Example 2: Create Empty Dataset from Existing Dataset

We can use the following code to create an empty dataset called empty_data that is generated from an existing dataset called Comet, which is a dataset built into SAS:

/*create empty dataset from existing dataset*/
data empty_dat;
    set sashelp.Comet;
    stop;
run;

We can then use proc contents to view the contents of the dataset:

/*view contents of dataset*/
proc contents data=empty_data;

From the output we can see that the dataset has four variables but zero observations.

At the bottom of the output we can also see the names of the four variables created from the existing dataset:

Additional Resources

The following tutorials explain how to perform other common tasks in SAS:

How to Use Proc Summary in SAS
How to Rename Variables in SAS
How to Create New Variables in SAS

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories