Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: THEend8_
CHAPTER 2:
DATA STEP PROGRAMMING
Learning Outcomes
•Upon completing this chapter, you should be able to do
the following:
• create new variables that are not originally contained in raw data
• perform calculations across records
• keep only selected variables in output SAS data set
• execute SAS DATA step statements conditionally
• execute SAS DATA step statements repeatedly
• put selected records into multiple data sets within one DATA step
• read hierarchical raw data records
• use SAS functions in DATA step (self-study)
2
Creating New Variables
• Assignment statements in DATA step
• New information can be added to a SAS data set by creating new
variables with assignment statements in a Data step.
• Syntax
DATA datasetname;
[Other DATA step statements]
variablename = expression; /*an assignment statement */
[Other DATA step statements]
RUN;
• Descriptions:
• Valid within DATA step. It is an executable statement in SAS DATA
step.
• The left-hand side of the statement must be a variable name.
• expression on the right-hand side may contain combinations of
numeric or non-numeric constants, variables, SAS functions, and
mathematical operators.
• When the expression contains character constants, each value must be
enclosed in a pair of single (or double) quotation marks.
3
Creating New Variables
•Assignment statements in DATA step
• Descriptions:
• SAS assigns variablename’s type and length based on its first
occurrence in the DATA step.
• SAS assigns variablename the same type and length as the
expression on the right side of the assignment operator.
• When instream data input method is used, all assignment
statements must appear before the DATALINES statement.
• Value of a created variable may be affected by the placement of
the concerned statement.
4
Creating New Variables
•Assignment statements in DATA step
• Some commonly used operators:
Addition + Subtraction – Multiplication *
Division / Exponentiation ** Parentheses ( )
• SAS performs exponentiation first, then multiplication and
division, followed by addition and subtraction.
• One can use parentheses to override the order of the
operation.
• Examples:
Statement: var1 = 10 + 4 * 3 ** 2 ; Outcome: var1 = 46
Statement: var1 = ((10 + 4) * 3) ** 2 ; Outcome: var1 = 1764
5
Creating New Variables
•Assignment statements in DATA step
• Example 2.1:
•If a variable (such as Cucumber)
has already been assigned a value
in PDV, SAS replaces the original
value with the new one.
•The variable Pea in the last record
is missing. Consequently, the
values of Total & Tomato_percent
are therefore also set to missing
for the same observation.
•SAS executes each
assignment statement once
during each round of DATA
step iteration.