Writing First code in SAS Part 1

Welcome to another session of Mindful Data Minds! This post continues from our previous discussion about the different windows or tabs available in SAS Studio. Today, we’ll take our first steps into writing SAS code using the Data Step.

Watch the Full Tutorial

What You Will Learn

  • The difference between Data Step and Proc Step.
  • How to write comments in SAS.
  • The syntax of a Data Step.
  • How to create and use a library.
  • How datasets are stored in WORK vs. permanent libraries.

Data Step vs. Proc Step

In SAS, there are two main types of steps:

  • Data Step: Used to create or modify data. It always generates a dataset (a table with rows and columns).
  • Proc Step: Short for Procedure. These are predefined codes that take input, process it, and generate output. We’ll cover Proc Steps in later sessions.

For now, let’s focus on the Data Step.

General Syntax of a Data Step

Here’s the basic structure:

data a;
   set b;
run;
  • data a; → Creates a new dataset named a.
  • set b; → Tells SAS to take data from an existing dataset b.
  • run; → Marks the end of the Data Step.

Each statement ends with a semicolon. This tells SAS that the statement is complete.

Writing Comments in SAS

Comments are notes you add to your code to explain what’s happening. They don’t affect the program.

  • You can write comments like this:
/* This is a comment */
  • Or like this:
* This is also a comment;

Remember: if you use an asterisk, you must end the comment with a semicolon.

Creating a Library

Libraries in SAS are like folders where datasets are stored. By default, datasets go into the WORK library, which lasts only until you log out. But you can create your own permanent library.

  • To create a library, use the LIBNAME statement:
libname test "C:\Users\YourFolder\tutorial";
  • libname → Keyword to define a library.
  • test → Alias (library name).
  • “C:\Users\YourFolder\tutorial” → Path to the folder where datasets will be stored.

Rules for library names:

  • Not more than 8 characters.
  • Must start with a letter or underscore (not a number).

Saving Data in a Library

Once the library is created, you can store datasets in it:

data test.a;
   set b;
run;

Now the dataset a will be saved inside the test library folder. You’ll see a file with extension .sas7bdat — that’s the default SAS dataset format.

Viewing Libraries

You can check your libraries in SAS Studio under the Libraries tab.

  • Datasets in WORK are temporary (available until you log out).
  • Datasets in your custom library (like test) are permanent.

Double-clicking on a dataset will open it for viewing.

Next Step

Continue learning by exploring the next tutorial in this series. Also subscribe to get notified about new lessons.

Have a Question?

Drop your doubts in the comments below or contact us.

Leave a Reply

Your email address will not be published. Required fields are marked *