sas conditional variable assignment

SAS Basics - Part 1

  •   Page:
  •   1  
  • |   2  
  • |   3  
  • |   4  
  • |   5  
  • |   6  
  • |   7  
  • |   8  
  • |   9  
  • Module 2: SAS Programming Basics - Part I
  • Introduction
  • Learning Objectives
  • The Data Step
  • Procedure Steps
  • More Basics
  • The "id" statement
  • Temporary & Permanent SAS Datasets & the Libname Statement
  • Importing a Data set
  • Creating New Variables
  • A New Variable
  • Creating New Data Sets
  • The Set Statement
  • New Data set From a Permanent SAS Data Set
  • Creating New Variables Using if-then;if-then-else; and if-then-else-then Statements
  • Accounting for Missing Data
  • Creating a Derived Variable
  • The 'length' Statement
  • Mathematical Expressions and SAS Functions
  • Sum Function
  • Mean Function
  • Square Root Function
  • Natural Logarithm Function

Creating New Variables Using if-then ; if-then-else ; and if-then-else-then Statements

An if-then statement can be used to create a new variable for a selected subset of the observations.

For each observation in the data set, SAS evaluates the expression following the if . When the expression is true, the statement following then is executed.

if age ge 65 then older=1;

When the expression is false, SAS ignores the statement following then. For a person whose age is less than 65, the variable older will be missing.

Note that the above statement could equivalently be written

if age >= 65 then older=1;

An optional else statement can be included (if-then-else) to provide an alternative action when the if expression is false.

else older=0;

For a person whose age is less than 65, the variable older will equal 0.

An optional else-if statement can follow the if-then statement. SAS evaluates the expression in the else-if statement only when the previous expression is false. else-if statements are useful when forming mutually exclusive groups. 

if 40 < age <= 50 then agegroup=1;

else if 50 < age <= 60 then agegroup=2;

else if age > 60 then agegroup=3;

  • A person whose age is between 40 and 50 (notice the strict inequality: those aged exactly 40 will not be included) will be in agegroup 1.
  • A person aged between 50 and 60 will be in agegroup 2 (again, notice the strict inequality: those aged exactly 50 will not be included in this agegroup, but will be in agegroup 1).
  • A person whose age is greater than 60 will be in agegroup 3.
  • A person whose age is 40 or younger will not be assigned to an agegroup, and their agegroup variable will be missing.

Note that this if-then-else-if statement could equivalently be written

if 40 lt age le 50 then agegroup=1;

else if 50 lt age le 60 then agegroup=2;

else if age gt 60 then agegroup=3;

An if statement can be followed by exactly one else statement or by many else-if statements. SAS will keep evaluating the if-then-else-if statements until it encounters the first true statement. 

The following code creates a new variable called group from an existing variable called gpa. The new variable called group takes on one of two values: "good standing" if a person's gpa is greater than or equal to 3.0 and "not good standing" if a person's gpa is less than 3.0.

  

data grades;

input name $ gpa;

if gpa< 3.0 then group = "not good standing" ;

if gpa>= 3.0 then group = "good standing" ;

proc print;

 This results in:

sas conditional variable assignment

Note that SAS does not generally distinguish between upper and lower case (you can use either). The exception is in the value of character variables. The value "Good standing" is not the same as the value "good standing".

Suppose we want to create a variable called gpagroup which takes on one of 3 values:

  • "Excellent Grades" for those with a gpa greater than or equal to 3.5,
  • "Good" for those with a gpa greater than or equal to 3.0 and
  • "Satisfactory" for those with a gpa greater than or equal to 2.5.

We run the following code:

if gpa>= 3.5 then gpagroup = "Excellent Grades" ;

if gpa>= 3.0 then gpagroup = "Good" ;

if gpa >= 2.5 then gpagroup = "Satisfactory" ;

sas conditional variable assignment

return to top | previous page | next page

IMAGES

  1. Conditional statements in SAS

    sas conditional variable assignment

  2. conditional statements

    sas conditional variable assignment

  3. SAS Variable

    sas conditional variable assignment

  4. SAS Variables

    sas conditional variable assignment

  5. 9 Performing Conditional Logic in SAS

    sas conditional variable assignment

  6. How to Master SAS Conditional Statements : A Comprehensive Guide

    sas conditional variable assignment

VIDEO

  1. Creating Data Science Assignment using AI

  2. VLSI Design Flow: RTL to GDS Week 9 || NPTEL ANSWERS || MYSWAYAM #nptel #nptel2024 #myswayam

  3. Variable Assignment in R

  4. SAS BASE certification prep course for SAS Beginners (Udemy coupon): ASSIGNMENT and SET statement

  5. 7. BASE SAS- Arithmetic Operations & Variable creation

  6. Experts in the Loop Conditional Variable Selection Based on Deep Learning for Accelerating Post Sili

COMMENTS

  1. PDF Beyond IF THEN ELSE: Techniques for Conditional Execution of SAS® Code

    In certain situations, the FORMAT procedure can serve as a form of conditional logic. Suppose we have a dataset containing a variable PLANET_NAME which is populated with the names of planets. Further, suppose we wish to derive a new variable PLANET_ORDER which contains the corresponding ordinal position of each planet (e.g.

  2. Solved: Create new variables with condition

    The SUM of the variable in a procedure such as proc means, summary, tabulate or report would be the number of X's recorded and the Mean would be the percentage chosen for each variable. Also some modeling procedures require a numeric value, most will by default drop records with missing values for any of the variables on the model, class or by ...

  3. how to assign values based on condition

    2 things. 1) Since you use explicit output statements, you are not guaranteed to get the same number of rows in the two data sets. It will depend on the EXT variable.. 2) Since you want to randomly assign values to the EXD variable if "EXT has pine in it", then your if statement is not sufficient.. There is not much to go on, but I think this is closer to what you want

  4. Do Loops: Conditionally Assigning a Variable

    Learn how use the CAT functions in SAS to join values from multiple variables into a single value. Find more tutorials on the SAS Users YouTube channel . SAS Training: Just a Click Away

  5. 5. SAS Variables and Assignment Statements

    SAS Variables and Assignment Statements ... If the condition is true, SAS takes the action that follows the keyword THEN — in this case, change the student's status to 'Failed.' If the conditon is false, SAS ignores the THEN clause and proceeds to the next statement in the DATA step. The condition always involves a comparison of some ...

  6. Creating New Variables Using if-then;if-then-else; and if-then-else

    For each observation in the data set, SAS evaluates the expression following the if. When the expression is true, the statement following then is executed. Example: if age ge 65 then older=1; When the expression is false, SAS ignores the statement following then. For a person whose age is less than 65, the variable older will be missing.

  7. How to Use IF-THEN-ELSE in SAS (With Examples)

    Assume you have a dataset `mydata` with variables `age` and `income`, and you want to create a new variable `category` based on the value of `age`. #### Sample Data "`sas data mydata; input age income; datalines; 25 50000 45 70000 70 30000 18 20000; run; "` #### Using `if-then-else` to Assign Values. You want to create a new variable ...

  8. sas

    I have a dataset in SAS with . a variable condition, that can take values in "01",...,"20"; several variables indexed by i, for instance var01, ..., var20; What i want to do is to create a new variable total which is equal to vark if condition=k.I can do it by several if... else statement (it is what I do for now), but I did not manage to do it in a more compact and elegant way.

  9. Value assignment based on multiple if conditions

    So the table below can be seen as a map that has the assignment rules branching like tree to the "Point" value. Some observations about the map table (below) and desired target dataset: The variables "Sport" and "Tournament" will always have values populated and the other variables can have blank values.

  10. check series of conditional statements to assign ...

    I have variable called mdotsp, contains values of missing number of tablets but it has different keywords such as "pill", "TAB", etc. I am trying to create numeric variable called mnmtb to assign a value from mdotsp which contains a number of missing pills. I am using below code to check series of conditional statements based on keywords.