If-Then-Else statement in SAS Programming
Comparison Operators used while using conditional statements.
| Symbol | Mnemonic | Meaning |
|---|---|---|
| = | EQ | equals |
| ^= or ~= | NE | not equal |
| > | GT | greater than |
| < | LT | less than |
| >= | GE | greater than or equals |
| <= | LE | less than or equals |
| in | IN | selecting multiple values |
-
IF statement
Syntax:
IF (condition is true) => It means subsetting a dataset.
Example:
Data readin;Input R_Num Subj1-Subj3;cards;112 21 22 23105 23 24 2695 25 25 26125 26 26 24122 15 25 1686 26 16 15;Data readin1;Setreadin;IF R_Num GE 100;run;chevron_rightfilter_noneIF R_Num GE 100 => This would tell SAS to retain only those Roll numbers whose values are greater than or equal to 100. In other words, you are removing Roll numbers whose values are less than or equal to 100.
It could be also done using the IF-THEN DELETE statement.
-
IF-THEN DELETE
Syntax:
IF (condition is true) THEN (delete the given statements);
Example:
Data readin;Input R_Num Subj1-Subj3;cards;112 21 22 23105 23 24 2695 25 25 26125 26 26 24122 15 25 1686 26 16 15;Data readin1;Setreadin;IF R_Num LT 100THENDELETE;run;chevron_rightfilter_noneIF R_Num LT 100 THEN DELETE => This would tell SAS to remove all the Roll numbers whose values are less than 100.
-
IF-THEN-ELSE Statement
Task 2: Suppose you want to set a tag on all the R_Num. The condition is:
If the value of R_Num is less than or equal to 100 sets "Old" tag otherwise set "New" tag.
IF (condition is true) THEN (perform this action); ELSE (perform the set of statements that is set when condition is false);
Data readin;Input R_Num Subj1-Subj3;cards;112 21 22 23105 23 24 2695 25 25 26125 26 26 24122 15 25 1686 26 16 15;Data readin1;Setreadin;IF R_Num LE 100THENTAG ="Old";ELSETAG ="New";run;chevron_rightfilter_none -
IF-THEN-ELSE IF Statement
Task 3: Let us assume you are asked to update the TAG column.
The conditions for tagging are as follows:
- If value of R_Num is less than 75 then TAG = “Old”
- If value of R_Num is greater than or equal to 75 and less than 100 then TAG = “New”
- If value of R_Num is greater than or equal to 100 then TAG = “Unchecked”
IF (condition is true) THEN (perform this action); ELSE IF (perform the set of statements when second condition is true); ELSE IF (perform the set of statements when third condition is true);
Data readin;Input R_Num Subj1-Subj3;cards;112 21 22 23105 23 24 2695 25 25 26125 26 26 24122 15 25 1686 26 16 15;Data readin1;Setreadin;length TAG $20;IF R_Num < 75THENTAG ="Old";ELSEIF 75 <= R_Num < 100THENTAG ="New";ELSEIF R_Num >= 100THENTAG ="Unchecked";run;chevron_rightfilter_none
Recommended Posts:
- Where Statement in SAS Programming
- Introduction to SAS programming
- Use of FLAG in programming
- Like Operator in SAS Programming
- Contains and Between-And operators in SAS Programming
- Which Programming Language to Choose?
- Introduction to Swift Programming
- Swift Programming Language
- Introduction to Programming Languages
- Object Oriented Programming in Ruby | Set-2
- Object-Oriented Programming in Ruby | Set 1
- How to Create or Modify a Variable in SAS Programming?
- How to drop variables from a dataset in SAS Programming?
- 5 Most Difficult Programming Languages of the World
- Comparing Ruby with other programming languages
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.






