Creating and Modifying 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. The instructions are performed to produce a new value:
  • 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 6.3 Arithmetic Operators
Operator
Action
Example
Priority
-
negative prefix
negative=-x;
I
**
exponentiation
raise=x**y;
I
*
multiplication
mult=x*y;
II
/
division
divide=x/y;
II
+
addition
sum=x+y;
III
-
subtraction
diff=x-y;
III
The order of operation is determined by the following conditions:
  • Operations of priority I are performed before operations of priority II, and so on.
  • Consecutive operations that have the same priority are performed in this order:
    • from right to left within priority I
    • from left to right within priority II and III
  • 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 6.4 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
partno lt "BG05"
>= or ge
greater than or equal to
id>='1543'
<= or le
less than or equal to
pulse le 85
Use logical operators to link a sequence of expressions into compound expressions.
Table 6.5 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.

Examples: Assignment Statements

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 sasuser.stress; 
   infile tests; 
   input ID $ 1-4 Name $ 6-25 RestHR 27-29 MaxHR 31-33 
         RecHR 35-37 TimeMin 39-40 TimeSec 42-43 
         Tolerance $ 45; 
    TotalTime=(timemin*60)+timesec; 
run;
Figure 6.12 Assignment Statement Output
Assignment Statement Output
The expression can also contain the variable name that is on the left side of the equal sign, as the following assignment statement shows. This statement redefines the values of the variable RestHR as 10% higher.
data sasuser.stress;
   infile tests; 
   input ID $ 1-4 Name $ 6-25 RestHR 27-29 MaxHR 31-33 
         RecHR 35-37 TimeMin 39-40 TimeSec 42-43 
         Tolerance $ 45; 
   resthr=resthr+(resthr*.10); 
run;
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 sasuser.stress; 
   infile tests; 
   input ID $ 1-4 Name $ 6-25 RestHR 27-29 MaxHR 31-33 
         RecHR 35-37 TimeMin 39-40 TimeSec 42-43 
         Tolerance $ 45; 
   resthr=resthr+(resthr*.10); 
run;  ^       ^

 result      original value

Date Constants

You can assign date values to variables in assignment statements by using date constants. 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.

Example: Assignment Statement and Date Value

In the following program, the second assignment statement assigns a date value to the variable TestDate.
data sasuser.stress;
   infile tests; 
   input ID $ 1-4 Name $ 6-25 RestHR 27-29 MaxHR 31-33 
         RecHR 35-37 TimeMin 39-40 TimeSec 42-43 
         Tolerance $ 45; 
   TotalTime=(timemin*60)+timesec; 
   TestDate='01jan2015'd; 
run;
Tip
You can also use SAS time constants and SAS datetime constants in assignment statements.
Time='9:25't; 

DateTime='18jan2015:9:27:05'dt;
Last updated: January 10, 2018
..................Content has been hidden....................

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