STAT1303 Data Management
4. Creating New Variables Using SAS Functions
4
Creating New Variables Using SAS Functions
In previous chapter, we have used the SAS procedures for data summarization in
which simple calculation is performed. In this chapter, the creation of new variables
using the SAS functions are discussed because more complicated calculation is
necessary for realistic data management and analysis.
4.1
Assignment Statement
To create or modify SAS variables, an assignment statement is used in Data Step.
Typically, an assignment statement is specified in the form of
variable
=
expression
;
The variable receives the new information and the required information /
calculations are specified in the expression. When the expression contains character
data, we must enclose the data in quotation marks or double quotation marks. To
define the required storage for variables (in particular for character variables), the
LENGTH
statement should be used.
Example 4.1.
Assignment statement.
* Example 4.1  assignment statement;
libname mylib ’D:/temp’;
data trade;
set mylib.trade1;
total = import+retained+reexport;
run;
proc print;
run;
Obs
year
category
import
retained
reexport
total
1
1995
1
61158
42953
19534
123645
2
1995
2
562415
142897
546247
1251559
3
1995
3
543075
225708
336550
1105333
...
TOTAL = IMPORT + RETAINED + REEXPORT;
Then, a new variable TOTAL which is the sum of IMPORT, RETAINED and
REEXPORT, is created.
4.2
Numeric Assignment Operators and Functions
4.2.1
Numeric Variables
A numeric variable is a variable whose values are numbers. In SAS, numeric variables
are stored and calculated in doubleprecision floating point representation. All kinds
of mathematical operations on numeric variables can be performed by SAS and SAS
functions extend the mathematical calculation capability for numeric expressions.
HKU STAT1303 (201112, Semester 1)
1
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
STAT1303 Data Management
4. Creating New Variables Using SAS Functions
4.2.2
Arithmetic Operators
Operation
Symbol
Example
Addition
+
x = y + z
Subtraction

x = y  z
Multiplication
⇤
x = y * z
Division
/
x = y / z
Exponentiation
⇤⇤
x = y ** z
The order of operations in arithmetic expressions follows rules in mathematical
expression
•
Parentheses
>
exponentiation
>
multiplication and division
>
addition and
subtraction.
Example 4.2.
Arithmetic operators.
* Example 4.2  Arithematic operators;
libname mylib ’D:/temp’;
data trade;
set mylib.trade1;
total = import+retained+reexport;
total2 = total**2;
run;
proc print;
run;
Obs
year
category
import
retained
reexport
total
total2
1
1995
1
61158
42953
19534
123645
15288086025
2
1995
2
562415
142897
546247
1251559
1.5663999E12
3
1995
3
543075
225708
336550
1105333
1.221761E12
...
4.2.3
Missing Values in Numeric Variables
By default, the missing value in data lines (raw data) are represented by a period (
.
).
In an expression, a missing numeric value is represented by a period, e.g.
export =
.;
means that a numeric variable EXPORT is created and initialized with missing
value. In the output of PROC, a missing numeric value is printed as a period (by
default).
For some SAS PROCs, missing numeric value is removed from analysis
while some of them do not. Indeed, some of them may have options to choose from.
This is the end of the preview.
Sign up
to
access the rest of the document.
 Spring '08
 SMSLee
 Statistics, Derivative, Data Management, PROC PRINT

Click to edit the document details