*Copyright @ www.mycsg.in;
Conditional processing in a DATA step
Conditional processing allows SAS to execute different logic depending on the value of a variable or expression
The most common forms are `if then else` and `select when`
These structures are used constantly when deriving analysis flags, groups, labels, and categories
Create a new variable based on the values present in another variable using IF THEN ELSE
We read the dataset `class` and create a character variable named `asex`
If `sex` is `M`, we assign `Male`
If `sex` is `F`, we assign `Female`
This is the simplest form of conditional assignment when only one statement is needed for each condition
data output; set class; length asex $6; if sex="M" then asex="Male"; else if sex="F" then asex="Female"; run;
Copy Code
View Log
SAS Log
data output; set class; length asex $6; if sex="M" then asex="Male"; else if sex="F" then asex="Female"; run; NOTE: There were 19 observations read from the data set WORK.CLASS. NOTE: The data set WORK.OUTPUT has 19 observations and 6 variables. NOTE: Compressing data set WORK.OUTPUT increased size by 100.00 percent. Compressed is 2 pages; un-compressed would require 1 pages. NOTE: DATA statement used (Total process time): real time 0.00 seconds cpu time 0.00 seconds
The output dataset contains the original variables plus the new variable `asex`
Review the data and confirm that `M` became `Male` and `F` became `Female`
View Data
Dataset View
Create two new variables based on the values present in another variable using IF THEN DO
When more than one statement must run for a condition, we use `do` and `end`
In this example, we create both a descriptive sex variable and a numeric code variable
This pattern is especially useful in real programming where one condition may require several assignments
data output; set class; length asex $6; if sex="M" then do; asex="Male"; asexn=1; end; else if sex="F" then do; asex="Female"; asexn=2; end; run;
Copy Code
View Log
SAS Log
data output; set class; length asex $6; if sex="M" then do; asex="Male"; asexn=1; end; else if sex="F" then do; asex="Female"; asexn=2; end; run; NOTE: There were 19 observations read from the data set WORK.CLASS. NOTE: The data set WORK.OUTPUT has 19 observations and 7 variables. NOTE: Compressing data set WORK.OUTPUT increased size by 100.00 percent. Compressed is 2 pages; un-compressed would require 1 pages. NOTE: DATA statement used (Total process time): real time 0.00 seconds cpu time 0.01 seconds
Compare this output with the earlier example and note that one condition now assigns two variables
Check that the values of `asex` and `asexn` are consistent for each student
View Data
Dataset View
Create a new variable based on the values present in another variable using SELECT WHEN
`select when` is another way to express conditional logic
It is often easier to read when the same variable is compared against multiple possible values
In this example, the logic is functionally similar to the earlier `if then else` step
data output; set class; length asex $6; select(sex); when ("M") asex="Male"; when ("F") asex="Female"; otherwise; end; run;
Copy Code
View Log
SAS Log
data output; set class; length asex $6; select(sex); when ("M") asex="Male"; when ("F") asex="Female"; otherwise; end; run; NOTE: There were 19 observations read from the data set WORK.CLASS. NOTE: The data set WORK.OUTPUT has 19 observations and 6 variables. NOTE: Compressing data set WORK.OUTPUT increased size by 100.00 percent. Compressed is 2 pages; un-compressed would require 1 pages. NOTE: DATA statement used (Total process time): real time 0.00 seconds cpu time 0.01 seconds
The resulting values should match the first example
This shows that multiple syntax styles can solve the same conditional task
View Data
Dataset View
Create two new variables based on the values present in another variable using SELECT WHEN DO
Just like `if then do`, the `select when` structure can also execute multiple statements per condition
This is useful when many categories must be handled, and each category needs several assignments
data output; set class; length asex $6; select(sex); when ("M") do; asex="Male"; asexn=1; end; when ("F") do; asex="Female"; asexn=2; end; otherwise; end; run;
Copy Code
View Log
SAS Log
data output; set class; length asex $6; select(sex); when ("M") do; asex="Male"; asexn=1; end; when ("F") do; asex="Female"; asexn=2; end; otherwise; end; run; NOTE: There were 19 observations read from the data set WORK.CLASS. NOTE: The data set WORK.OUTPUT has 19 observations and 7 variables. NOTE: Compressing data set WORK.OUTPUT increased size by 100.00 percent. Compressed is 2 pages; un-compressed would require 1 pages. NOTE: DATA statement used (Total process time): real time 0.01 seconds cpu time 0.01 seconds
Review the output and confirm that the two derived variables are created correctly
Compare the readability of this approach with the earlier `if then do` example
View Data
Dataset View
Key points to remember
Use `if then else` for straightforward conditional logic
Use `do` and `end` when more than one statement must execute for the same condition
Use `select when` when comparing one expression against several possible values
Always define sufficient character length before assigning character results