Lesson 5: Manipulating Numeric Values

[Pages:4]Lesson 5: Manipulating Numeric Values Summary

Main Points

Using Descriptive Statistics Functions function-name(argument-1, argument-2,...,argument-n)

Function SUM MEAN MIN MAX N NMISS CMISS

Returns the sum of the nonmissing arguments the arithmetic mean (average) of the arguments the smallest value from the arguments the largest value from the arguments the number of nonmissing arguments the number of missing numeric arguments the number of missing numeric or character arguments

A SAS function is a routine that performs a calculation on, or a transformation of, the arguments listed in parentheses and returns a value.

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. There are several types of variable lists including numbered ranges, name ranges, name prefixes, and special SAS names.

Variable List Description

Numbered Range

all variables x1 to xn, inclusive

Example Total = sum(of Qtr1-Qtr4);

SAS? Programming 2: Data Manipulation Techniques

1

Copyright ? 2010 SAS Institute Inc., Cary, NC, USA. All rights reserved.

Lesson 5: Manipulating Numeric Values

Variable List Description

Name Range

all variables ordered as they are in the program data vector, from x to a inclusive

Example Total = sum(of Qtr1--Fourth);

Name Prefix

all variables that begin with the same string

Total = sum(of Tot:);

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_);

Truncating Numeric Values ROUND(argument)

CEIL(argument)

FLOOR(argument)

INT(argument)

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. The CEIL function returns the smallest integer greater than or equal to the argument. The FLOOR function returns the greatest integer less than or equal to the argument.

SAS? Programming 2: Data Manipulation Techniques

2

Lesson 5: Manipulating Numeric Values

The INT function returns the integer portion of the argument.

Converting Values Between Data Types

INPUT(source, informat)

PUT(source, format)

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 conversion uses the w. informat, and it produces a numeric missing value from any character value that does not conform to standard numeric notation. You can use the INPUT function to explicitly convert character values to numeric values. The INPUT function returns the value that is produced when the source is read with a specified informat. Numeric data values are automatically converted to character values whenever they are used in a character context. For example, SAS automatically converts a numeric value to a character value when you use the concatenation operator. When SAS automatically converts a numeric value to a character value, SAS writes the numeric value with the BEST12. format and right aligns the value. The resulting value might contain leading blanks. You can use the PUT function to explicitly control the numeric-to-character conversion using a format.

SAS? Programming 2: Data Manipulation Techniques

3

Lesson 5: Manipulating Numeric Values

Sample Code

Using Descriptive Statistics Functions and Truncating Numeric Values

data donation_stats; set orion.employee_donations; keep Employee_ID Total AvgQT NumQT; Total = sum(of Qtr1-Qtr4); AvgQT = round(Mean(of Qtr1-Qtr4),1); NumQt = n(of Qtr1-Qtr4);

run;

proc print data=donation_stats; run;

Converting Values Between Data Types

data hrdata; keep EmpID GrossPay Bonus Phone HireDate; set orion.convert; EmpID = ID+11000; Bonus = input(GrossPay,comma6.)*.10; Phone = '(' !! put(Code,3.) !! ') ' !! Mobile; HireDate = input(Hired,mmddyy10.);

run;

proc print data=hrdata; format HireDate mmddyy10.;

run;

SAS? Programming 2: Data Manipulation Techniques

4

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download