SAS Variable – Types, Properties & Creating Variables in SAS
We discussed SAS Data Set in our last tutorial, now we will be discussing SAS Variable, properties of SAS Variable, SAS Variable types and how we can create variables in SAS. The new variable is based on other variables. SAS creates new character variables and uses them in a program.
Moreover, we will see some examples of Variables in SAS Programming Language.
Stay updated with latest technology trends
Join DataFlair on Telegram!!
1. What is a Variable in SAS?
SAS Variable is a name given by the user to any column of a dataset. The basic motive behind this is to categorize all observations under a particular characteristic like height, weight, name, date of birth and so on. Any name came to be given to a variable depending upon the characteristic, it has to represent. But, some rules should be followed.
You can refer to rules to be kept in mind while naming variables in the SAS Basic syntax article.
2. Creating SAS Variable
Here, we can create the SAS variable in two ways:
i. New Variable
Suppose we have a data set called length which has length and breadth data.
Data length; input length breadth; cards; 65 130 70 150 67 145 72 180 62 110 ; run;
The above code will form a dataset with variable names length and breadth and show 5 observations. Here, an input keyword is used for defining variables and cards keyword for assigning values to the variables.
Understand the First. & Last. Variables in SAS
ii. Adding Variables to an Existing Dataset
We would like to create a new data set with a new variable, Area, based on length and breadth. To create a new variable, specify the name of the variable in the DATA step and put a (=) sign with the parameters on which it depends.
- YearAge62 = byear+62;
- income = salary + interest;
- Parentsprice = (momsprice + dadsprice)/2;
Area is equal to length*breadth.
So in this case, if we had a data set that contained length in meters and breadth in meters, we could use SAS to compute a derived variable called “area” based on these two other variables. Here’s how we can do this in SAS:
data length; input length breadth; area = length*breadth; cards; 65 130 70 150 67 145 72 180 62 110 ; run;
The data set “length” has three variables, length, breadth, and area. Note that the statement creating the new variable, area, is between the input statement and the statement of the card. New variables are always created within a DATA step.
Must Read: SAS Concatenate Data Sets
3. SAS Variable Properties
Variables have 5 important properties, name, type, length, format and label, that help us to identify them and that define how they can be used.
i. SAS Variable Name
When naming a variable, there are a few rules you must follow:
- The name should not contain more than 32 characters.
- The name cannot start with a number, it has to start with a letter (a-z) or an underscore(_). Numbers can be used after the first character.
- No Blanks or spaces should be present. For example, you cannot name a variable “Last Name” because it will not recognize the blank. Instead, use “LastName” or “last_name”.
Avoid giving long names to your variable. Also, try and avoid generic variable names like a1, a2, because that does not give much information and can be confusing.
Don’t forget to check Output Delivery Systems
ii. SAS Variable Type
There is two types of SAS variable: numeric and character.
a. SAS Numeric Variable
This SAS Variable store numbers. The variable can be used for performing arithmetic calculations like addition, subtraction. Additionally, date-time variables are also considered numeric in SAS. Missing values for numeric variables appear as a period (.).
INPUT VARIABLE1 VARIABLE2 ; #Define numeric variables in the data set.
INPUT ID, SALARY, COMMISSION;
b. SAS Character Variables
This SAS Variable is also known as string variables, contain information that the system recognizes as text. These variables in SAS are defined by placing a dollar sign ($) at the end. They can include letters, special characters (such as &,%,(),$), and even numbers. Missing values for character variables appear as a blank (“”).
INPUT VAR1 $ VAR2 $ VAR3 $; #Define character variables in the data set.
INPUT NAME $ LNAME $ ADDRESS $;
Understand the concept – SAS Correlation Analysis of variables
iii. SAS Variable Length
The “length” of a SAS variable corresponds to the number of bytes for storing variables. The default number of bytes for both numeric and character variables is 8.
iv. SAS Variable Format
A variable’s format instructs SAS how it has to get print in the SAS output. In SAS, this includes:
- Number of decimal places to show for numeric variables (e.g., 1 versus 1.00)
- Date format for dates (e.g. 05/16/2013 versus 2013MAY16 versus 2013-05-16)
Formats are crucial for helping readers understand your data and your output. Variable formats have enough details that we’ve split them into their own tutorials.
Must learn – How to create SAS Scatter Plot
v. SAS Variable Label
Adding a label to a SAS variable is an interesting way to make dataset look easier and interpret it easily. A label provides extra information. For example, consider a student data where we have taken student’s birth date as one variable and student’s date of enrollment as the second variable.
Now, suppose we want to create two variables in SAS, one for their current age and one for their age when they started attending college, we will name it something like “age_now” and “age_start”.
However, if someone wants to look at our data, he will confuse between the two variables because such variable names can be difficult to understand. We might, therefore, want to name them as “student’s current age” and “ student’s age at the time of starting college”.
SAS Variable labels can assign either in a data step or a proc step. When a label assigns in a proc step, it is temporary; it will only associate with that variable during the execution of the proc step it contains within. (That is, the label will only appear in the output of the procedure it includes in.) When a label assigns in a data step, it becomes a permanent part of the dataset.
A variable label statement looks like this:
LABEL variable_name = "Variable label";
Explore the SAS Linear Regression in detail
Variable Label Example:
Let’s add a label to the variable bday in our sample dataset.
Let’s now temporarily add a label to the “bday” variable in the proc step. We’ll demonstrate this using PROC PRINT statement. The important thing to note is that you can put a label statement in a proc step to give the variable a label for the output you produce in the proc step. This will not change the label of the variable in the dataset.
PROC PRINT DATA=sample LABEL; VAR bday; LABEL bday = "Date of Birth"; RUN;
Note that in the output, the variable name that was “bday” earlier change to “ date of birth”. This was temporary, and therefore, the variable name will not change in the dataset.
Now, if we want to permanently add a label to the dataset, we will put it in a data step.
DATA students; LABEL bday = "Date of Birth"; RUN;
Hence, today we learned what is SAS variable, types of Variable in SAS: Numeric Variable, SAS Character Variables, Different properties of Variable in SAS Programming: SAS Variable name, length, format & label and how we can create variables in SAS. Stay tuned for more interesting things to learn SAS Programming.
If you have any queries or feedbacks, just enter in the comment section.
Crack your SAS interview through Tricky SAS Interview Questions.