Table of Contents
SAS (Statistical Analysis System) is a widely used software for data analysis and manipulation. In order to perform various analytical tasks, it is often necessary to create new variables in SAS. This can be done using the DATA step, which is the primary programming language in SAS. To create a new variable, the programmer must first define its name, type, and length. Once defined, the variable can be assigned a value using various SAS functions and operators. The variable can then be used in subsequent analyses or data manipulations. Creating new variables in SAS allows for a more efficient and customizable approach to data analysis and is an essential skill for any SAS programmer.
Create New Variables in SAS (With Examples)
Here are the two most common ways to create new variables in SAS:
Method 1: Create Variables from Scratch
data original_data;
input var1 $ var2 var3;
datalines;
A 12 6
B 19 5
C 23 4
D 40 4
;
run;
Method 2: Create Variables from Existing Variables
data new_data;
set original_data;
new_var4 = var2 / 5;
new_var5 = (var2 + var3) * 2;
run;The following examples show how to use each method in practice.
Related:
Example 1: Create Variables from Scratch
The following code shows how to create a dataset with three variables: team, points, and rebounds:
/*create dataset*/
data original_data;
input team $ points rebounds;
datalines;
Warriors 25 8
Wizards 18 12
Rockets 22 6
Celtics 24 11
Thunder 27 14
Spurs 33 19
Nets 31 20
;
run;
/*view dataset*/
proc printdata=original_data;
Note that you can simply list the variable names after the input function and you can create their values from scratch after the datalines function.
Note: SAS assumes each new variable is numeric. To create a character variable, simply type a dollar sign “$” after the variable name like we did for the team variable in this example.
Example 2: Create Variables from Existing Variables
The following code shows how to use the set function to create a new dataset whose variables are created from existing variables in another dataset:
/*create new dataset*/
data new_data;
set original_data;
half_points = points / 2;
avg_pts_rebs = (points + rebounds) / 2;
run;
/*view new dataset*/
proc printdata=new_data;
Additional Resources
The following tutorials explain how to perform other common tasks in SAS:
Cite this article
stats writer (2024). How do you create new variables in SAS?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-do-you-create-new-variables-in-sas/
stats writer. "How do you create new variables in SAS?." PSYCHOLOGICAL SCALES, 1 Jul. 2024, https://scales.arabpsychology.com/stats/how-do-you-create-new-variables-in-sas/.
stats writer. "How do you create new variables in SAS?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-do-you-create-new-variables-in-sas/.
stats writer (2024) 'How do you create new variables in SAS?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-do-you-create-new-variables-in-sas/.
[1] stats writer, "How do you create new variables in SAS?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, July, 2024.
stats writer. How do you create new variables in SAS?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
