Master SAS in 30 days!

A jargon-free, easy-to-learn SAS base course that is tailor-made for students with no prior knowledge of SAS.

A Complete SAS Tutorial for Beginners

Are you keen to learn SAS? You have come to the right place.

This tutorial covers the following topics:

  • SAS Interface [1-5]: Code Tab
  • SAS Interface [2-5]: Log Tab
  • SAS Interface [3-5]: Error and Warning Messages
  • SAS Interface [4-5]: Library
  • SAS Interface [5-5]: Data Set
  • Data Set [1-14]: Create a Data Set
  • Data Set [2-14]: Character Variable
  • Data Set [3-14]: Creating Multiple Observations
  • Data Set [4-14]: Character Variable II
  • Data Set [5-14]: Data Truncation
  • Data Set [6-14]: Copying a Data Set
  • Data Set [7-14]: Subsetting a Data Set
  • Data Set [8-14]: Accessing Other Libraries
  • Data Set [9-14]: Accessing Your Own Data Set I
  • Data Set [10-14]: Assessing Your Own Data Set II
  • Data Set [11-14]: Saving a Data Set
  • Data Set [12-14]: Exporting a Data Set I
  • Data Set [13-14]: Exporting a Data Set II
  • Data Set [14-14]: Exporting a Data Set III

Important Note

This tutorial is taken from the first two modules of our Practical SAS Training Course for Absolute Beginners.

You can access the full training course here.

How to start

Before we start the tutorial, you will need access to at least one of the following programs:

You can run SAS Studio for free on SAS OnDemand for Academics.  

Please ensure you have access to SAS before you start the tutorial. 

Set up the Training

This is a coding-oriented tutorial.

We recommend that you have both the training webpage and the SAS software opened side-by-side for the duration of the course.

Below is the screen shot of the suggested screen setup:

Watch the video instruction on how to set up your screen properly. 

MODULE 1: SAS INTERFACE

Introduction

In this training, you will learn the four major components of the SAS interface:

  1. Code Tab
  2. Log Tab
  3. Library
  4. Data Set

By the end of the module, you will be able to enter, check and submit code in SAS.

You will also be able to review your data from any SAS data set.

SAS INTERFACE [1-5]

Code Tab

The Code tab is located in the middle of your SAS Studio interface. It is where you enter your program code. 

Run/Submit Button

The submit button is located above the coding area. It is an icon with the image of a running man. You can click it to run your program.

Exercise

Enter the following code into the Code tab:

Data Test;
    a = 1;
Run;

Click the submit button to run the program.

Need some help? Watch the video clip below:

SAS INTERFACE [2-5]

Log Tab

Once you click the submit button, your code will be executed. 

Now, click the Log tab located on the right-hand side of the Code tab.

The Log tab shows you whether or not there is any problem with your code. 


Rule of thumb: your program is generally in good shape if the Log tab shows only blue text.

Exercise

Go back to the Code tab.

Remove the semi-colon at the end of line 2. 

You should have the following code in the Code tab:

Data Test;
    a = 1
Run;

Submit the code. 

Need some help? Watch the video clip below:

SAS INTERFACE [3-5]

Error and Warning Messages

The code you submitted in the previous exercise is missing a semi-colon. 

If your program contains any errors, such as a missing semi-colon, SAS will show two types of messages:

  • Error message (in red)
  • Warning message (in green)

If you scroll down the Log tab, you should see the actual error and warning messages. 


The error message indicates a serious problem(s) with your code. SAS stops the execution whenever there is an error message. The results generated by your code are unlikely to be correct. 

The warning message shows less serious problems than an error message. SAS continues to run despite the presence of the warning message.

However, it is always good programming practice to make sure the Log tab does not show any error or warning message at all. 

Exercise

Now, go back to the Code tab and re-insert the semi-colon on line 2. 

Re-submit the code. 

Do you still see the error and warning messages?

Need some help? Watch the video clip below: 

SAS INTERFACE [4-5]

Library

Library is the folder that contains the SAS data sets. You should see the Libraries tab in the bottom left corner of the SAS Interface:


After you click the Libraries tab, you now should see a number of libraries (e.g. SASHELP, SASUSER, WORK).

Expand the WORK library.

You should now see the TEST data set listed.

Exercise

Expand to the SASHELP library and open the Cars data set.


How many rows and columns are there in the data set?

Click the "Get Solution" button below to view the answer.

Get Solution

SAS INTERFACE [5-5]

Data Set

A data set is similar to an Excel spreadsheet. It is divided into rows (observations) and columns (variables).

The data set displays 100 observations at a time.

You can click on the arrows on the right-hand side to view more observations.

Exercise

What is the MAKE of the car in row 351?

Get Solution

Do you have a hard time learning SAS?

Take our Practical SAS Training Course for Absolute Beginners and learn how to write your first SAS program!

MODULE 2: CREATING A DATA SET

Introduction

80% of the SAS programming work involves creating, modifying and transforming data sets.

In this module, we will walk you through the different ways of creating a data set.

After the completion of the module, you will be able to create any type of data set you want.

Data Set [1-14]

Creating a Data Set

You can create a data set using a set of code called a Data step. 

Example

Data Test;
    a = 1;
Run;

Using a data step is fairly simple. It starts with the syntax 'Data' and ends with 'Run'. Each line in SAS ends with a semi-colon. You can create contents for the data set within the DATA Step.


Now, run the data step from the example above.

This should create a data set called Test that can be found in the WORK library.



A variable called a is also created in the Test data set containing a single value of 1.


Exercise

Create a data set that contains the following three variables:

Var1 = 123
Var2 = 356
Var3 = 923

Name the data set as Number.

Get Solution

Programming Tips 1

How to check your code/program?

Unfortunately, we cannot process and verify your code when you are running it on your installation of SAS Studio or SAS Base.

However, there are a few things you can do to make sure your code is correct:

1. Click the "Get Solution" button

Clicking on the "Get Solution" button below each exercise will show you the correct code. You can verify your code by comparing it with the solution.

Get Solution

2. Check your data set/result

Each exercise has a clearly defined objective. For example, you might be asked to create a data set that contains certain variables. You can check your data set and make sure you have achieved the desired outcome.

3. Check Log Tab

Finally, you should always check the Log tab when you are unsure of your program code.

Make sense? Let's continue.

Programming Tips 2

Before we continue, there are two things about SAS programming you need to know:

1. SAS is insensitive to case

In other words, the following code sets return exactly the same result in SAS:

Data Test;
    a = 1;
Run;

OR

data test;
    a = 1;
run;

OR

daTa tESt;
    A = 1;
RuN;

2. SAS is insensitive to indentation

The two code sets also return exactly the same result in SAS:

Data Test;
a = 1;
Run;

OR

Data Test;
    a = 1;
Run;

Data Set [2-14]

Creating Character Variables

Unlike other programming languages such as R, Java or C++, there are only two types of variable in SAS:

  • Numeric and
  • Character

Creating a character variable is easy. You simply need to enclose them with quotation marks. 

Either a single quotation ('John') or double quotation ("John") works. 

Example

Data Test;
Name = "John";
Class = "Mathematics";
Run;

This DATA Step will create two character variables: Name and Class. They both contain character values.



(Try it!)

Exercise

Create a data set that contains the following variables:

Restaurant: Burger King
NumEmploy: 5
Location: Toronto

Name the data set as Food.

Get Solution

Data Set [3-14]

Creating Multiple Observations

The majority of the SAS data sets contain multiple observations.


In order to create a data set that contains more than one observation, you need the Input and Datalines statements

Input and Datalines Statement

Example

Data Test;
    Input a b c;
    Datalines;
    1 3 4
    2 7 9
    12 444 123
    ;
Run;

(1) Input Statement

The Input statement lists the variables to be created.

In our example, the Input statement tells SAS to create three variables: a, b and c.

(2) Datalines

The Datalines statement signals SAS to begin reading the data on the next line.

Together with these statements, you can create a data set with multiple observations. 


Exercise

Create a data set that contains the following variables:

Name the data set as SCORE

Get Solution

Data Set [4-14]

Creating Character Variables II

Creating character variables in SAS generally requires a little more attention. 

When using the Input statement, SAS assumes all of the variables to be numeric (unless specified otherwise). 

Example

Data Test;
    Input Name Gender Age;
    Datalines;
    John Male 31
    Mary Female 22
    Mark Male 45
    ;
Run;

The Name and Gender variables contain character values. However, they are not captured in the data set created.

(Try it!)


To create the character variables properly, you must include the dollar sign ($) in the Input statement after each character variables:

Example

Data Test;
    Input Name $ Gender $ Age;
    Datalines;
    John Male 31
    Mary Female 22
    Mark Male 45
    ;
Run;

The dollar sign ($) tells SAS to create Name and Gender as character variables.

Exercise

Create a data set that contains the following variables:


Name the data set as PROFILE.

Get Solution

Data Set [5-14]

Data truncation often happens when you create a character variable that is longer than eight characters.

Example

Data Test;
    Input Name $ Gender $;
    Datalines;
    Christopher Male
    Elizabeth Female
    MacDonald Male
    ;
Run;

Each of the names above is longer than 8 characters.

The default length of a character variable is eight. Any variable that contains more than eight characters will be truncated (cut off).


(Try it!)

The data truncation problem can be easily solved by adding a Length statement.

Example

Data Test;
    Length Name $12;
    Input Name $ Gender $;
    Datalines;
    Christopher Male
    Elizabeth Female
    MacDonald Male
    ;
Run;

The Length statement changes the length of the Name variable from eight to twelve. 

As a result, the complete name is now captured in the data set.



Note:

Length Name $12

1. A dollar sign ($) is added before the number 12. This is needed when adjusting the length of a character variable.

Exercise

Create a data set that contains the following variables:


Name the data set as PROFILE.

Get Solution

Data Set [6-14]

Copying Data Set

Copying a data set is a very common SAS programming task. It can be done easily by using the Set statement.

From our previous example, we have created a data set called Test.


Now, we will copy this data set into a new data set called Test2:

Example

Data Test2;
    Set Test;
Run;


Test2 will contain exactly the same data as Test. 


Exercise

From our last exercise, we created a data set called PROFILE.

Create a new data set called NEW_PROFILE from PROFILE using the Set statement.

Get Solution

Data Set [7-14]

Subsetting a Data Set

In some cases, you may want to filter a data set and keep only a subset of the original data. 

This can be done by using the IF statement.


The Test data set above contains three observations: Two males and one female.

Example

Data TEST3;
    Set Test;
    IF Gender = "Male";
Run;

The IF statement allows you to keep only the observations that meet the specified condition(s).


In this example, only the "Male" observations are kept in the Test3 data set. The "Female" observations are removed.

(Try it!)

Exercise

Create a new data set called Enrol based on the Profile data set. Enrol should contain only the patients enrolled in the study (Enrol = YES)

*The PROFILE data set should have been created in Module 5-14. If you haven't done so, please go back and create the data set before continuing on this exercise.

Get Solution

Data Set [8-14]

Accessing Other Libraries

So far, we have only worked with data sets in the WORK library. 

You can access data sets from other libraries using the LIBNAME statement.



Example

Data Cars;
    Set SASHELP.Cars;
Run;

The SASHELP reference above is called the library reference. It is needed when referring to a data set that is not in the WORK library. 

In this example, the Cars data set is created in the WORK library based on the same data set from the SASHELP library. 

You will find that the Cars data set in the WORK library is a carbon copy of the one in SASHELP


Exercise

Locate the HOLIDAY data set from SASHELP

Create a subset of the HOLIDAY data set that contains only the holidays that fall in January.

Name the new data set as JanHol and create it in the WORK library.

How many observations are there in the subset?

Get Solution

Programming Tips 3

Work vs. Permanent Libraries

In SAS, there are only two types of libraries: 

Work vs. Permanent

The work library is a temporary library. It contains all the working data sets. No data set from this library will be saved after the program has run.

All other libraries (whether it is a built-in library or one that you create yourself) are permanent libraries. 

Permanent libraries contain permanent data sets. You will need the library reference to access these libraries. However, a permanent library allows you to retain the data sets even after the program is closed.

We’ll learn more about permanent libraries in the next session.

Data Set [9-14]

Accessing your own data sets – Part I

In this section, you will learn how to access your own data files on SAS Studio.

If you are using SAS Studio on SAS OnDemand for Academics, you must first upload the data files to the SAS server.

Let’s walk through how to do that.

Example

Download the Car data set below and save it on your local computer.


Download Car Data Set

On SAS Studio, go to Server Files and Folders on the left panel.

You will see the Files (Home) folder:

 

Click the Files (Home) folder and then click the upload icon to upload the file.

A window will pop up. 

You are asked to upload a file to the server.

Note: the pop-up window displays the destination path of the file to be uploaded.

On my SAS Studio, the file will be uploaded to '/home/kisumsam'.

This is the actual directory path of the Files (Home) folder on the SAS server.

Your path will be different and it will be in the form of:

'/home/your_user_name'

Now, select the Car data set, and click upload.

Done! The Car data set is uploaded to your home directory!

Exercise

Download Income Data Set

Under your home directory, create a new folder called DS1.

Upload the Income data set to the DS1 folder.

Need some help? Watch the instructional video below:

Data Set [10-14]

Accessing your own data sets – Part II

In the last section, you have uploaded the Car data set to the Files (Home) folder on the SAS server.


The Files (Home) folder is the home directory of the SAS server.

Inside this folder, you can have multiple sub-folders.

DS1 is a sub-folder that we created in the exercise in the previous section.

Now, let's look at how you can use SAS code to access the Car data set in the Files (Home) folder. 

We will first have to get the corresponding directory path, where the data set is located.

Example

First, right click on Files (Home).

Then, click Properties.

You will find the location of this folder on the server.

The directory path of the Files (Home) folder is:

'/home/kisumsam'

Please note that your path will be different.

Your path will include your unique user name.

It will be in the form of:

'/home/your_user_name'

 

Creating a Library

Now we know the directory path to where the Car data set is located.

We must now create a library. 

This can be done using a Libname statement. 

Example

LIBNAME Proj1 "/home/kisumsam";

The LIBNAME statement consists of three parts:

1. LIBNAME
This tells SAS to create a new library.

2. Library Name
The second part of the LIBNAME statement is the library name. In our example, it is PROJ1

3. Directory Path
Lastly, you will have to specify the path that is associated with the library. 

In our example, the library is associated with "/home/kisumsam".

Now, run the LIBNAME statement with your own unique directory path:

LIBNAME Proj1 "/home/your_user_name";

A new library called PROJ1 is created:

The Car data set can be found in the PROJ1 library:

Double click the Car data set.

The data set will open up:

 

Exercise

In our last exercise, we have saved the INCOME data set in the DS1 shared folder. 

Write a SAS program to copy the INCOME data set into the WORK library.

Get Solution

Data Set [11-14]

Saving a SAS Data Set

If you want to save a SAS data set, you will have to copy the data set into a library that is connected to a folder on your SAS server.

Example

Libname Proj1 '/home/kisumsam';

Data Test;
    a = 1;
Run;

Data Proj1.Test;
    Set Test;
Run;

 

The data set will then be saved on the SAS server:


To download the file to your PC, you can simply right click on the data set, and click Download File:

 

Select the location. Your file will then be downloaded to your PC.

Exercise

Locate the CP951 data set in the SASHELP library. 

Save the CP951 data set into the home directory of the SAS server.

Get Solution

Data Set [12-14]

Export a SAS Data Set into an Excel spreadsheet

You will often need to export a data set into an Excel spreadsheet. 

This can be done easily by using a procedure called PROC EXPORT.

Proc Export Data=Income 
OutFile='/home/your_user_name/Income.xlsx'
Replace
Dbms=xlsx;
Run;

[Note: you must change the directory path when running the code above.]

Proc Export is a procedure that allows you to export a SAS data set into an external file such as an Excel spreadsheet or a text file. 

You should include these three options when using Proc Export:

1. OUTFILE option
The OUTFILE option lets you specify the file to be exported. In our example, the data is exported into Income.xlsx

2. REPLACE option
The REPLACE option allows you to overwrite the file if it already exists in the stated directory path. It is recommended to always include this option when using PROC EXPORT.

3. DBMS option
The DBMS option specifies the file format (file type) to be exported. In our example, we use the DBMS option to tell SAS to export the data set into an Excel spreadsheet (.xlsx). 

----

Now, run the code in the example above with your own directory path.

The Income data set (created in one of our previous exercises) will then be exported into an Excel spreadsheet:

Exercise

Locate the ELECTRIC data set from the SASHelp library.

Export ELECTRIC to an Excel spreadsheet.

Ensure the Excel spreadsheet contains the same rows and columns as the SAS data set.

Get Solution

Data Set [13-14]

Export a SAS data set to a text file

Exporting data into a text file is similar to exporting data to an Excel spreadsheet.

Example

Proc Export Data=Income
OutFile='/home/your_user_name/Income.txt'
Replace
Dbms=tab;
Run;


Pay attention to the following when exporting to a text file:

1. File Extension
Use (.txt) as the file extension when exporting to a text file.

2. DBMS option
You can use the DBMS option to select the type of delimiter when exporting to a text file.

DBMS=tab
This will create a tab-delimited file.

DBMS=csv
This will create a comma-delimited file.

(try it!)

Exercise

Export the ELECTRIC data set into a text file.

Use a tab as the field delimiter.

Get Solution

Data Set [14-14]

Short Cut to Exporting Data Sets

SAS Studio provides a shortcut for exporting data sets. If you prefer not to write code, you can simply follow the steps below to export the data.

3 Steps to Export Data:

Step 1: Right click the data set that you'd like to export and click Export

Step 2: Select the folder where the data set should be exported to.

Step 3: Name the file to be exported (from Filename) and select the type of file to be exported (Excel, Text, CSV, etc.)

Done! The file is exported:

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
iconmail

Get latest articles from SASCrunch

SAS Base Certification Exam Prep Course

Two Certificate Prep Courses and 300+ Practice Exercises