SAS Functions by Example - Herman Lo
SAS Functions by Example - Herman Lo
SAS Functions by
Example
Ron Cody
Herman Lo
Technical Analyst, RBC Capital
Agenda
Book Structure
Examples from the Book
Character Functions (CATS, CATX)
Date and Time Functions (INTCK, INTNX)
Descriptive Stats (IQR, SMALLEST/LARGEST)
Special Functions (INPUT, PUT)
Macro Functions (CALL SYMPUT, CALL
SYMPUTX)
The Verdict
Book Structure
List of Functions
Purpose
Syntax
Examples/Anticipated Outputs
A Sample Program
At the back
List of Functions
Index (Alphabetical)
Character Functions
CATS(string-1, string-2 )
CATX(string-1, string-2 )
Example
A = Star
B = Wars
CATS(A, B) = StarWars
CATX(A, B) = Star Wars
INTCK(interval<Multiple><.shift>,
date1,date2)
Returns number of Intervals between
date1 and date2
INTCK(interval<Multiple><.shift>,
date1,date2)
Interval=SEMIMONTH,HOUR,MINUTE,SECOND Shift=Interval
but only Multi-intervals can be shifted (Multiple must be
specified)
HOUR8.6=8-HOUR intervals starting at 6AM (6AM,2PM,10PM)
INTCK(interval<Multiple><.shift>,
date1,date2)
Examples
INTCK(YEAR.7,05MAY2002d, 15JUL2002d)=1
INTCK(WEEK,01JAN1960d, 04JAN1960d)=1
01JAN1960 is a Sunday, so the week counter is triggered
because default WEEK starting point is Sunday.
CAREFUL:
Results may surprise you (off-by-one problems). Watch where
the starting point is.
INTNX(interval,startdate,increment<,alignment>)
default is BEGINNING
Example
INTNX(WEEK,01JAN1960d,1,MIDDLE)=06JAN,1960d
Descriptive Stats
IQR(<of> numeric-values)
Computes the interquartile range (25th
percentile and 75th percentile) in a list
of values
Use of to define a list of values
Examples
X1=1,X2=2,X3=3,X4=.
IQR(of X1-X4)=2
IQR(X1,X2,X3,X4)=2
Descriptive Stats
SMALLEST/LARGEST(N,<of> numeric-values)
X1=1,X2=2,X3=3,X4=.,X5=.
SMALLEST(3,X2,X3,X4)=.
LARGEST(1,X1,X2,X3,X4)=3
Special Functions
INPUT(value, informat)
PUT(value, format)
Special Functions
***Primary functions: PUT, INPUT;
PROC FORMAT;
VALUE ITEM
1=APPLE
2=PEAR
3=GRAPE
OTHER=UNKNOWN;
VALUE $COST
A C = 44.45
D = 125.
OTHER = ;
RUN;
DATA TABLE;
INPUT ITEM_NO CODE $ @@;
ITEM_NAME = PUT(ITEM_NO,
PUT(ITEM_NO, ITEM.);
AMOUNT = INPUT(PUT(CODE,
INPUT(PUT(CODE, $COST.), 9.);
DATALINES;
1 B2 D 3 X 4 C
;
PROC PRINT DATA=TABLE NOOBS;
TITLE Listing of Data Set TABLE;
RUN;
CODE
ITEM_
NAME
AMOUNT
APPLE
44.45
PEAR
125.00
GRAPE
UNKNOWN
44.45
Macro Functions
Macro Functions
Example
DATA TEST;
INPUT STRING $CHAR10. ;
CALL SYMPUT(StringWithBlanks,STRING);
CALL SYMPUTX(StringWithoutBlanks,STRING);
DATALINES;
ABC
;
DATA _NULL_;
WITHBLANKS = ---- || &StringWithBlanks || ----;
WITHOUTBLANKS = ---- || &StringWithoutBlanks ||
----;
PUT Value of StringWithBlanks is WITHBLANKS;
PUT Value of StringWithBlanks is WITHOUTBLANKS;
RUN;
SAS LOG
Value of WITHBLANKS is ---ABC
Value of WITHOUTBLANKS is ----ABC----
----
File I/O functions are used to obtain information about SAS data
sets
dsid = OPEN(data-set-name<,mode>)
Opens a SAS data set with the name data-set-name and
return a data set ID dsid
A Data set ID is necessary for File I/O Functions
If data set cannot be opened, OPEN returns a 0
EXIST(dsid)
Returns 1 if Data set exists and a 0 otherwise
CLOSE(dsid)
Closes SAS data set after it has been opened by the OPEN
function
ATTRC(dsid,attribute)
Returns the character value of a character type attribute
i.e. ATTRC(DSID, sortedby) = name of the BY variable.
Empty if not sorted.
ATTRN(dsid,attribute)
Returns the numeric value of a character type attribute
CODE
ITEM_
NAME
AMOUNT
APPLE
44.45
PEAR
125.00
GRAPE
UNKNOWN
44.45
SAS LOG
ANY=1
(indicates that dataset has both observations and variab
NLOBS=4 (dataset has 4 logical observations)
NVARS=4 (dataset has 4 variables)
The Verdict