Javatpoint Logo
Javatpoint Logo

SAS Variable

Types | Properties | Creating Variables in SAS

In the last few topics, we have learned various operations on the data set. Now in this topic, we are going to learn, SAS variable and its creation, various types, and properties.

What is a variable?

In the SAS, a variable is a name given by the user to the column of a dataset.

The purpose behind variables in SAS is, to categorize observations under a particular characteristic like name, date of birth, height, weight, etc.

A variable name can be given on the basis of characteristics, which it has to represent. There are some rules of the SAS Basic Syntax article that must be followed while naming the variable.

Types of SAS Variable

There are two types of SAS variable:

  1. Numeric
  2. Character

SAS Numeric Variable

The numeric variable stores numeric values i.e., numbers. This variable is used to perform arithmetic calculations like addition, subtraction, multiplication, division, and all other types of calculations. Besides, this, numeric variable is also used to store date and time.

Missing values for a numeric variable are displayed by period (.).

Syntax:

Example:

SAS Character Variables

The character variable is used to store text. This variable is also known as a string variable. These variables are defined by putting a dollar sign ($) at the end of the variable name. They can contain special characters (such as &, %, (), $), letters, and even numbers.

Missing values for a text variable are displayed by blank ("").

Syntax:

Example:

Creating SAS Variable

As mentioned above, we can create variables on the basis of characteristics, so first consider the characteristics, and then give names to the variables according to the characteristics.

There are two ways to create SAS variables:

  • Create a new variable in a new data set
  • Add variable into the existing data set

Create a New Variable in New Data Set

Suppose we are creating a new data set named mensuration, which has two characteristics, length, and breadth.

Following example illustrate how we can create it:

The above code will create a dataset with variables length and breadth as columns. It will also show five observations. Let's execute this code in SAS Studio.

SAS Variable

Output:

SAS Variable

As we can see in the output, there are two variables length and breadth generating columns with five observations.

Add Variable to an Existing Dataset

Now, if we need to add a new variable which is based on length and breadth into the existing data set mensuration, we can add it in the following way:

First, specify the name of the new variable then put equal sign (=)with the parameters on which it depends.

area=length*breadth (It is a mathematical formula of Area)

We can use SAS to calculate a derived variable called "area" on the basis of two other variables i.e., length and breadth. Now, let's see how we can do this:

Execute the above code in SAS Studio:

SAS Variable

Output:

SAS Variable

As we can see in the output, the data set "mensuration" has added and calculated new variable area. Now, it has three variables, length, breadth, and area.

Note: Always create new variables between the input statement and the datalines statement.

Variable Properties

SAS Variable has four important properties, name, length, format, and label.

  • Name
  • Length
  • Format
  • Label

Properties help us to identify variables and define how they can be used.

Name

SAS variable follows some naming conventions, which are given below:

  • The variable name must not contain more than 32 characters.
  • The variable name should not start with a number, we can start it with a letter (a-z) or (A to Z) or an underscore(_). We can use numbers after the first character.
  • The blank or space should not exist between two words. For example, a variable name cannot be as "Last Name" because the blank or space will not be recognized. Instead of it, we can use "LastName" or "last_name."

Note: Avoid giving a large group of names to the variable. Also try to avoid common names like a1, a2, because it does not provide specific information and can be confusing.

SAS Variable Length

The SAS variable"length" corresponds to the number of bytes for variable storage. The default value of bytes for both character and numeric variables is 8.

SAS Variable Format

Format of variable instructs to SAS, how to print the output. Formats are very important because it helps readers to understand the output data.

SAS formatting for decimal place and date includes the following formats:

  • Decimal number format for the numeric variable is 1 or 1.00.
  • Date format for the date variable is 2013MAY16 or 05/16/2013 or 2013-05-16.

SAS Variable Label

A LABEL can be used if we need to provide additional information about variables. For example, consider an employee data where we took employee's Date of Birth and Date of Joining as a variable DOB and DOJ, respectively. Now, we want to provide more information about these variables because they are creating an illusion due to the similarity in Date. To overcome this confusion, we can use the label.

Syntax:

Let's understand through an example, how the LABEL works:

The PROC CONTENTS shows that the labels were actually assigned.

Execute the above code in SAS Studio:

SAS Variable

Output:

SAS Variable

As we can see in the output, all the variables have been labeled according to the instructions.

The LABEL can be used either in the data step or PROC step.

In the above code, LABEL is used in the data step, and when the LABEL is used in a data step, it becomes a permanent part of the dataset. The output of the above code is displaying labels of all the variables, and these labels are the permanent part of the data set.

When the label is used in the PROC step, it becomes a temporary part of the data set. This means we can label a variable name into the other form for temporary use. For example, DOB ="Date of Birth" or DOJ ="Date of Joining."

The labeled variable DOB will appear as a variable Date of Birth and DOJ as Date of Joining in the output.

Let's see in the example:

Execute the above code in SAS Studio:

SAS Variable

Output:

SAS Variable

In the above code, we used LABEL in the PROC PRINT procedure (or proc step). In the output, variables DOB and DOJ have displayed in the labeled form, i.e., Date of Birth and Date of Joining. The fact to remember is, we can use a LABEL statement in a proc step only to display it in a print procedure, it cannot change the label of the variable in the dataset.

The conclusion is when you want to change label permanently, then use the label in the data step, and when temporary, use it in PROC step.


Next TopicSAS String




Help Others, Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA