L5 Functions Manipulating Numeric Values
L5 Functions Manipulating Numeric Values
/*******************************************************************************
1. Using Descriptive Statistics Functions
*******************************************************************************/
/* 1.1 Descriptive Statistics Functions */
/*
A SAS function is a routine that performs a calculation on, or a transformation of,
the arguments listed in parentheses and returns a value.
function-name(argument-1, argument-2,…,argument-n)
You can list all the variables in the function, or you can use a variable list by
preceding the first variable name in the list with the keyword OF.
Special SAS Name List all of the variables, all of the character variables, or
all of the numeric variables that are already defined in the current DATA step
Total=sum(of _All_);
Total=sum(of _Character_);
Total=sum(of _Numeric_);
*/
/*******************************************************************************
2. Truncating Numeric Values
*******************************************************************************/
/* 2.1 truncation functions - ROUND, CEIL, FLOOR, INT*/
/*
There are four truncation functions that you can use to truncate numeric values.
They are the ROUND, CEIL, FLOOR, and INT functions.
The ROUND function returns a value rounded to the nearest multiple of the round-off
unit. If you don't specify a round-off unit, the argument is rounded to the nearest
integer.
ROUND(argument<,round-off-unit>)
The argument must be a number or a numeric expression.
The round-off unit must be numeric and positive. If you don't specify a round-off
unit, the argument is rounded to the nearest integer.
For example, this assignment statement rounds the values of TotalSales to the
nearest integer.
Rnd_TotalSales = ROUND(TotalSales);
*/
DATA truncate;
NewVar1 = ROUND(12.12);
NewVar2 = ROUND(42.65, .1);
NewVar3 = ROUND(-6.478);
NewVar4 = ROUND(96.47, 10);
NewVar5 = ROUND(12.69, .25);
NewVar6 = ROUND(42.65, .5);
RUN;
/* NewVar1 = 12 */
/* NewVar2 = 42.7 */
/* NewVar3 = -6 */
/* NewVar4 = 100 */
/* NewVar5 = 12.75 */
/* NewVar6 = 42.5 */
/* 2.2 Exploring the CEIL, FLOOR, and INT Functions */
/*
The CEIL function returns the smallest integer greater than or equal to the
argument.
CEIL(argument)
The FLOOR function returns the greatest integer less than or equal to the argument.
FLOOR(argument)
DATA truncate;
Var1 = 6.478;
CeilVar1 = CEIL(Var1);
FloorVar1 = FLOOR(Var1);
IntVar1 = INT(Var1); /* integer portion of the argument */
Var2 = -6.478;
CeilVar2 = CEIL(Var2);
FloorVar2 = FLOOR(Var2);
IntVar2 = INT(Var2); /* integer portion of the argument */
RUN;
/* CeilVar1 = 7 */
/* FloorVar1 = 6 */
/* IntVar1 = 6 */
/* CeilVar2 = -6 */
/* FloorVar2 = -7 */
/* IntVar2 = -6 */
/*******************************************************************************
3. Converting Values Between Data Types
*******************************************************************************/
/* 3.1 Automatic Character-to-Numeric Conversion */
/*
You can allow SAS to automatically convert data to a different data type for you,
but it can be more efficient to use SAS functions to explicitly convert data to a
different data type. By default, if you reference a character variable in a numeric
context, SAS tries to convert the variable values to numeric. Automatic character-
to-numeric conversion uses the w. informat, and it produces a numeric missing value
from any character value that does not conform to standard numeric notation.
*/
INPUT(source, informat)
Example:
Test = INPUT(SaleTest, comma9.);
The INPUT function uses the numeric informat COMMA9. to read the values of the
character variable SaleTest. Then the resulting numeric values are stored in the
variable Test.
When you use the INPUT function, be sure to select a numeric informat that can read
the form of the values. You must specify a width for the informat that is equal to
the length of the character variable that you need to convert.
*/
/* The first step is to use the RENAME= data set option to rename the variable you
want to convert. */
DATA hrdata;
SET orion.convert(rename=(GrossPay=Char_GrossPay));
GrossPay = INPUT(Char_GrossPay, comma6.);
(DROP=Char_GrossPay)
RUN;
/* The next step is to use the INPUT function in an assignment statement to create
a new variable with the original name of the variable you just renamed. */
DATA hrdata;
SET orion.convert(rename=(GrossPay=Char_GrossPay));
GrossPay = INPUT(Char_GrossPay, comma6.);
RUN;
/* You can then use a DROP= data set option in the DATA statement to exclude
CharGross from the output data set. */
DATA hrdata (DROP=Char_GrossPay);
SET orion.convert(rename=(GrossPay=Char_GrossPay));
GrossPay = INPUT(Char_GrossPay, comma6.);
RUN;
Example:
Since SAS automatically converts a numeric value to a character value when you use
the concatenation operator, you might try concatenating the value of 'Code' with
parentheses and the value of 'Mobile'.
*/
DATA hrdata;
KEEP Phone Code Mobile;
SET orion.convert;
Phone = '(' || Code || ') ' Mobile;
RUN;
/* The resulting value might contain leading blanks. */
/* Phone = ' (303) 123456' */
/* 3.5 Explicit Numeric-to-Character Conversion */
/*
You can use the PUT function to explicitly control the numeric-to-character
conversion using a format.
PUT(source, informat)
Source indicates the numeric variable, constant, or expression to be converted to a
character value.
A format matching the data type of the source must also be specified.
*/
DATA hrdata;
KEEP Phone Code Mobile;
SET orion.convert;
Phone = '(' || PUT(Code, 3.) || ') ' Mobile;
RUN;
/*
or you can use the CAT function as an alternative to the PUT function.
Remember that the CAT function returns a value that is the concatenation of the
named strings. This assignment using the CAT function produces the same results as
this assignment using the PUT function.
*/
DATA hrdata;
KEEP Phone Code Mobile;
SET orion.convert;
Phone = CAT('(',Code,') ',Mobile);
RUN;
/* If you use the CAT function, no note is written to the log. */
/*******************************************************************************
Sample Programs
*******************************************************************************/