Creating Variables

Assignment Statements

Use an assignment statement in any DATA step in order to modify existing values or create new variables.
Syntax, assignment statement:
variable=expression;
  • variable names a new or existing variable
  • expression is any valid SAS expression
Tip:The assignment statement is one of the few SAS statements that do not begin with a keyword.
For example, here is an assignment statement that assigns the character value Toby Witherspoon to the variable Name:
Name='Toby Witherspoon';

SAS Expressions

You use SAS expressions in assignment statements and many other SAS programming statements to do the following:
  • transform variables
  • create new variables
  • conditionally process variables
  • calculate new values
  • assign new values
An expression is a sequence of operands and operators that form a set of instructions.
  • Operands are variable names or constants. They can be numeric, character, or both.
  • Operators are special-character operators, grouping parentheses, or functions.

Using Operators in SAS Expressions

Use the following arithmetic operators to perform a calculation.
Table 9.1 Arithmetic Operators
Operator
Action
Example
Priority
-
negative prefix
negative=-x;
1
**
exponentiation
raise=x**y;
1
*
multiplication
mult=x*y;
2
/
division
divide=x/y;
2
+
addition
sum=x+y;
3
-
subtraction
diff=x-y;
3
The order of operation is determined by the following conditions:
  • Operations of priority 1 are performed before operations of priority 2, and so on.
  • Consecutive operations that have the same priority are performed in this order:
    • from right to left within priority 1
    • from left to right within priority 2 and 3
  • You can use parentheses to control the order of operations.
Note: When a value that is used with an arithmetic operator is missing, the result of the expression is missing. The assignment statement assigns a missing value to a variable if the result of the expression is missing.
Use the following comparison operators to express a condition.
Table 9.2 Comparison Operators
Operator
Meaning
Example
= or eq
equal to
name='Jones, C.'
^= or ne
not equal to
temp ne 212
> or gt
greater than
income>20000
< or lt
less than
x=5000
x<8000
>= or ge
greater than or equal to
x=5000
x>=2000
<= or le
less than or equal to
pulse le 85
Use logical operators to link a sequence of expressions into compound expressions.
Table 9.3 Logical Operators
Operator, symbol
Description
AND or &
and, both. If both expressions are true, then the compound expression is true.
OR or |
or, either. If either expression is true, then the compound expression is true.
Note: In SAS, any numeric value other than 0 or missing is true, and a value of 0 or missing is false. Therefore, a numeric variable or expression can stand alone in a condition.
  • 0 = False
  • . = False
  • 1 = True

Examples: Assign Variables

Example 1: Create a New Variable

The assignment statement in the DATA step below creates a new variable, TotalTime, by multiplying the values of TimeMin by 60 and then adding the values of TimeSec.
data work.stresstest;
  set cert.tests;
  TotalTime=(timemin*60)+timesec;
run;
proc print data=work.stresstest;
run;
Output 9.1 Assignment Statement Output (partial output)
Partial Output: Assignment Statement Output

Example 2: Re-evaluating Variables

In the following example, the assignment statement contains the variable RestHR, which appears on both sides of the equal sign. This assignment statement evaluates each observation to redefine each RestHR observation as 10% higher. When a variable name appears on both sides of the equal sign, the original value on the right side is used to evaluate the expression. The result is assigned to the variable on the left side of the equal sign.
data work.stresstest;
  set cert.tests;
  resthr=resthr+(resthr*.10);
run;
proc print data=work.stresstest;
run;
Output 9.2 PROC PRINT Output of Work.StressTest (partial output)
Partial Output: PROC PRINT Output of Work.Stresstest

Date Constants

You can assign date values to variables in assignment statements by using date constants. SAS converts a date constant to a SAS date. To represent a constant in SAS date form, specify the date as 'ddmmmyy' or 'ddmmmyyyy', immediately followed by a D.
Syntax, date constant:
'ddmmmyy'd
or
'ddmmmyy'd
  • dd is a one- or two-digit value for the day.
  • mmm is a three-letter abbreviation for the month (JAN, FEB, and so on).
  • yy or yyyy is a two- or four-digit value for the year, respectively.
Tip:Be sure to enclose the date in quotation marks.
Tip
You can also use SAS time constants and SAS datetime constants in assignment statements.
Time='9:25't; 
DateTime='18jan2018:9:27:05'dt;

Example: Assignment Statements and Date Values

In the following program, the second assignment statement assigns a date value to the variable TestDate.
data work.stresstest;
  set cert.tests;
  TotalTime=(timemin*60)+timesec;
  TestDate='01jan2015'd;
run;
proc print data=work.stresstest;
run;
Notice how the values for TestDate in the PROC PRINT output are displayed as SAS date values.
Output 9.3 PROC PRINT Output of Work.StressTest with SAS Date Values (partial output)
PROC PRINT Output of Work.StressTest with SAS Date Values (partial output)
You can use a FORMAT statement in the PROC PRINT step to modify the TestDate values and change them to another format. To apply formats to your output, see SAS Formats and Informats.
Last updated: August 23, 2018
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.21.19.253