How To Create New Variables In SAS (With Examples) - - Statology
Maybe your like
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: How to Rename Variables in SAS
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 print data=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 print data=new_data;
We created the new variables half_points and avg_pts_rebs using variables that already existed in our original dataset.
Additional Resources
The following tutorials explain how to perform other common tasks in SAS:
How to Normalize Data in SAS How to Replace Characters in a String in SAS How to Replace Missing Values with Zero in SAS How to Remove Duplicates in SAS
Tag » How To Add Variables In Sas
-
Creating Variables - SAS OnlineDoc, V8
-
Ways To Create Variables - SAS Help Center
-
How Do I Sum Two Variables Into One New ... - SAS Communities
-
Creating New Variables - SPH - Boston University
-
How To Create A New Variable In SAS - SAS Example Code
-
SAS Tutorials: Computing New Variables - LibGuides
-
SAS : Creating Or Modifying A Variable - ListenData
-
[PDF] Creating New Variables In An Existing Dataset:
-
SAS Creating New Variables In A Dataset - YouTube
-
SAS Data Tutorial - Creating New Variables - YouTube
-
Creating And Recoding Variables In SAS | SAS Learning Modules
-
[PDF] DSCI 325: Handout 3 – Creating And Redefining Variable In SAS
-
14.2 - The DROP= And KEEP= Options | STAT 481 - STAT ONLINE
-
[PDF] Adding One Value To All Records In A SAS® Data Set - Lexjansen