Skip to Main Content

An Introductory Guide to SAS

An introductory online tutorial for beginners to use SAS.

Defining SAS Libraries

Syntax:

libname libref 'SAS-library';

Example:

libname sample 'C:\temp';

This statement creates a SAS library named "sample" and links to the computer directory "C:\temp."

Data Steps

Creating a dataset

Syntax

data dataset-name;

run;

Example

data test;

run;

The above code creates a dataset named "test."

 

Creating a new dataset based on existing dataset

Syntax

data new-dataset-name;

   set existing-dataset-name;

run;

Example

data test1;

   set test;

run;

The above code copies the existing dataset "test" to a new dataset called "test1." This is highly recommended when you want to modify your existing datasets and avoid making changes on the original data. 

 

Modifying a dataset

1. Renaming variables

Syntax

rename = (var_old=var_new)

Example

data test2 (rename = (Number=ID Gen=gender));

   set test1;

run;

The above code renames two variables in dataset "test1." It changes variable "Number" to "ID" and variable "Gen" to "gender."

 

2. Keeping variables

Syntax

keep=var1 var2 var3

Example

data test3 (keep = ID name gender);

   set test2;

run;

The above example creates a new dataset "test3" from previous dataset "test2" while only keeping three variables: ID, name, and gender. 

 

3. Dropping variables

Syntax

drop=var1 var2 var3

Example

data test4 (drop = ID name gender);

   set test2;

run;

The above example creates a new dataset "test4" from previous dataset "test2" while dropping three variables: ID, name, and gender. 

 

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.