PROC DATASETS In SAS [ #7 Use Cases With Examples]

The PROC DATASETS procedure is used to manage SAS datasets. With this procedure, you can list, change, append, and repair datasets and create and maintain indexes.

The DATASETS procedure is a utility procedure that manages your SAS datasets. With PROC DATASETS, you can do the following:

  • copy SAS datasets from one SAS library to another
  • rename SAS datasets
  • repair SAS datasets
  • delete SAS datasets
  • list the SAS datasets that are contained in a SAS library
  • list details of a SAS dataset
  • append SAS data sets
  • modify SAS datasets
  • create and delete indexes on SAS datasets

In this article we will see #7 powerful use cases of using PROC DATASETS in SAS with the examples. 

Syntax

/* basic syntax */
libname mylib 'SAS-data-library';

proc datasets library=mylib;
	<datasets commands>;
	run;
quit;

The following sample datasets will be used to demonstrate different examples of how to use proc datasets in SAS.

/* create sample datasets  */
data work.class;
	set sashelp.class;
run;

data work.cars;
	set sashelp.cars;
run;

PROC DATASETS: View SAS Datasets And Library Details

You can view library or dataset details such as list of tables, data types, physical location of a library, and dataset details like physical name, variables, index, and so on.

The proc datasets procedure to be used without mentioning anything except library reference would generate library details.

We have already created two datasets (work.class and work.cars) in the work library. Let’s try to print details about the work library which should show information about all the available files under the WORK library.

/* view library details */

proc datasets lib=work;
	run;
quit;
proc datasets in sas to print library details

If you add a CONTENTS statement followed by a dataset name then this procedure returns with the dataset details.

In this example proc datasets return the detailed information about both the mentioned datasets class and cars.

/* view dataset details */

proc datasets lib=work memtype=data;
	contents data=class;
	contents data=cars;
	run;
quit;
proc datasets in sas with contents statement

PROC DATASETS: Copying and Moving SAS Datasets 

With the proc datasets procedure it becomes very easy to copy or move datasets from one library to another SAS library. You can perform four different operations with this method.

  • Copy all the datasets from one SAS library to another
  • Copy selected datasets from one SAS library to another
  • Move all the datasets from one SAS library to another
  • Move selected datasets from one SAS library to another

To demonstrate this scenario we need one more library where we can copy or move datasets. The following sample demo library mylib will be created and used throughout this article.

/* define SAS library */
libname mylib '/home/u61950255/data/mylib';

Copy all the datasets from one SAS library to another

This library is empty at this stage. The below example shows how you can copy all the datasets from the WORK library to MYLIB library.

/* define SAS library */
libname mylib '/home/u61950255/data/mylib';

/* copy all datasets */
proc datasets;
	copy in=work out=mylib;
	run;
quit;
copy all sas datasets from one library to another

Copy selected datasets from one SAS library to another

I have manually deleted everything from MYLIB which was copied in the previous step. Now this library is empty.

The below example shows how you can copy selected datasets, class and cars from the WORK library to MYLIB library.

/* copy selected datasets */
proc datasets;
	copy in=work out=mylib;
	select class cars;
	run;
quit;
copy selected sas datasets from one library to another

Move all the datasets from one SAS library to another

This library is empty at this stage. The below example shows how you can move all the datasets from the WORK library to MYLIB library.

/* move all datasets */

proc datasets;
	copy in=work out=mylib move;
	run;
quit;
move all sas datasets from one library to another

Move selected datasets from one SAS library to another

I have manually deleted everything from MYLIB which was moved in the previous step. Now this library is empty.

The below example shows how you can move selected datasets, class and cars from the WORK library to MYLIB library.

/* move selected datasets */
proc datasets;
	copy in=work out=mylib move memtype=data;
	select class cars;
	run;
quit;
Move selected sas datasets from one library to another

You can verify the results by viewing the details about MYLIB library using the proc datasets procedure.

/* view mylib library details */
proc datasets lib=mylib;
	run;
quit;

PROC DATASETS: SAVE, DELETE, and KILL Datasets

With the proc datasets procedure you can do: save, delete, or kill datasets from the SAS library. We will see each one of this with an example.

  • SAVE statement: You can save SAS datasets from deletion. It deletes all the tables in a library except the ones listed in the SAVE statement.
  • DELETE statement: You can delete selected SAS datasets. It deletes tables from a library.
  • KILL statement: You can delete all the datasets. It deletes all SAS datasets in the SAS library that are available for processing.

I have already made the preparation to create two datasets (cars & class) in the WORK library. We can play with these datasets to learn more about how this SAVE statement works.

SAVE statement

In this example proc datasets will keep only work.cars dataset and delete everything else from a WORK library.

/* SAVE - Saving SAS datasets from deletion */
proc datasets lib=work;
	save cars / memtype=data;
	run;
quit;
Proc datasets with SAVE statement

DELETE Statement

In the below example proc dataset will delete only mentioned dataset that is work.class using the DELETE statement. The remaining datasets will be untouched and remain there as-is.

/* DELETE - deleting SAS datasets */

proc datasets lib=work;
	delete class;
	run;
quit;
Deleting datasets with proc datasets procedure

KILL Statement

In this example you are attempting to kill the entire library. It means proc datasets will delete all the datasets available in the specified (WORK) library using the KILL statement.

/* KILL - deleting SAS datasets from work lib*/
proc datasets lib=work kill;
	run;
quit;
How To kill the entire SAS library

PROC DATASETS: Concatenating Datasets

You can append two datasets and concatenate all the rows from one dataset into another. It is often called the APPEND in SAS. There is a separate SAS procedure available that’s the PROC APPEND procedure which does exactly the same. 

It takes the dataset from data= and appends into the base= dataset. The base dataset is the master dataset where data gets appended.

Scenario 1: Base dataset doesn’t exist

If the base dataset doesn’t exist then the proc datasets creates a new empty base dataset and appends data from data=data-set. The below example demonstrates the same scenario. The work.result dataset doesn’t exist.

/* Concatenating datasets */
proc datasets;
	append base=result data=class ;
	run;
	contents data=result;
	run;
quit;
Append two datasets in SAS

Scenario 2: What if structure of base and data – datasets doesn’t match

Ideally base dataset and dataset to be appended should have the same structure. It means the same columns, their data types, formats, etc.

We have two exactly the same datasets, work.result and work.class. Now I have dropped a variable (Age) from the work.result dataset to demonstrate this scenario.

Now if you run the same above code and try to append the datasets it will throw an error.

NOTE: Appending WORK.CLASS to WORK.RESULT.

WARNING: Variable Age 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.

If you want SAS to ignore this and still continue with the appending datasets then the FORCE option can be used with the proc datasets procedure.

Forces the APPEND statement to concatenate SAS datasets or a CAS table to a SAS data set when the DATA= data set contains variables that meet one of the following criteria: 

  • 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.

Here is the revised code that will append the datasets for matching rows and ignores the anomalies.

/* append datasets with force optioin */
proc datasets;
	append base=result data=class force;
	run;
	contents data=result;
	run;
quit;
proc datasets appending datasets with FORCE option

PROC DATASETS: Modifying SAS Datasets

With the proc datasets procedure you can modify dataset attributes such as rename column names, remove or change column labels, remove formats, etc.

The following example shows how you can rename variable name from Age to new_Age, remove labels and formats for all the variables from the dataset.

/* before modifying SAS datasets */
proc datasets lib=work memtype=data;
	contents data=class;
	run;
quit;


/* Modifying SAS datasets */
proc datasets lib=work memtype=data;
	modify class;
	rename Age=new_Age;     /* rename column name */
	attrib _all_ label=' '; /* remove label */
	attrib _all_ format=;   /* remove formats */
	contents data=work.class;
	run;
quit;
Proc datasets with modifying sas datasets

PROC DATASETS: Rename Datasets

The proc datasets procedure is very flexible. It allows you to change the dataset names within the SAS library using the CHANGE statement. It renames one or more SAS datasets in the same SAS library.

This example renames the class dataset to new_class and cars dataset to new_cars which is there in the WORK library.

/* renaming SAS datasets */

proc datasets lib=work;
	change class=new_class /* changing dataset name from class to new_class */
	 	   cars =new_cars  /* changing dataset name from cars to new_cars */
	 	  ;	
	run;
quit;
Renaming SAS datasets using proc datasets

PROC DATASETS: Creating and Deleting Indexes

You can use proc datasets to create simple or composite indexes on SAS dataset or you can also delete them from SAS dataset using MODIFY statement.

A MODIFY statement in PROC DATASETS changes the attributes of a SAS dataset and, through the use of subordinate statements, the attributes of variables in the SAS dataset.

  • INDEX CREATE statement: It creates simple or composite indexes for the SAS dataset specified in the MODIFY statement.
  • INDEX DELETE statement: It deletes simple or composite indexes for the SAS dataset specified in the MODIFY statement.
  • UNIQUE keyword: It specifies that the combination of values of the index variables must be unique.

For the validation purpose we have also used CONTENTS statement to print dataset details to verify indexes available on the dataset.

/* create simple and composite index */
proc datasets lib=work;
	modify class;
	index create name; /* simple index */
	index create nameAgeindx=(name age) /unique; /* composite index */
	run;
	contents data=class;
	run;
quit;
Create simple and composite Index on sas datasets

As stated earlier you can use proc datasets with MODIFY and  INDEX DELETE statements you can delete specific indexes by listing them here or you _all_ keyword. It deletes all indexes, except for indexes that are owned by an integrity constraint.

/* delete simple and composite index */

proc datasets lib=work;
	modify class;
	index delete name;   /* delete simple index */
	index delete nameAgeindx; /* delete composite index */
	run;
	contents data=class;
	run;
quit;
Delete simple and composite Index on sas datasets

FAQ – PROC DATASETS In SAS

What is the difference between PROC DATASETS and PROC DELETE?

PROC DATASETS is a utility procedure that can perform various operations on SAS files, such as copying, renaming, deleting, and modifying them. PROC DELETE is a simpler procedure that can only delete SAS files from a SAS library. 

 

PROC DATASETS is more efficient and flexible than PROC DELETE, as it can process multiple files at once and can also delete other types of SAS files, such as catalogs and indexes

How can I rename a variable in a SAS dataset using PROC DATASETS?

You can use the MODIFY and RENAME statements in PROC DATASETS to rename a variable in a SAS dataset. For example, if you want to rename the variable AGE to NEW_AGE in the CLASS dataset, you can use the following code:

proc datasets library=work; 
modify class;
rename age=new_age_yrs;
run; quit;
How can I append one SAS dataset to another using PROC DATASETS?

You can use the APPEND statement in PROC DATASETS to append one SAS dataset to another. For example, if you want to append the CARS dataset to the CLASS dataset, you can use the following code:

proc datasets library=work; 
append base=class data=cars;
run; quit;