Professional Documents
Culture Documents
Objectives
Learn to use a variety of SAS functions to perform the following
tasks:
Convert character (numeric) data to numeric (character)
data
Create SAS date values
Extract time intervals from a SAS date value
Perform calculations with date, datetime and time values
Extract, edit, concatenate, and search the values of
character strings
Replace, remove occurrences of a particular word within a
character string
General From of SAS Functions
Data quiz;
input name $ 1-5 q1 6-9 q2 10-12 q3 13-15 q4 16-18 q5 19-21;
/* COMPUTE SUM AND AVERAGE OF QUIZ SCORES FOR EACH STUDENT USING FUNCTIONS */
INPUT(Source, Informat);
Source is the character variable, constant, or expression to be
converted.
Informat is the format to INPUT (to read) the character into numeric.
Run the following program 2 and observe the results and variable attributes.
data N_C_Conv;
var1=245; var2=124.6; var3=1245;
C_var1 =put(var1, 4.); C_var2 = put(var2,6. ); C_var3 = put(var3,
7.1);
proc contents varnum; run; proc print; run;
Manipulating SAS Date Values with
Functions
Recall :
SAS date is numeric data value defined starting at 1/1/1960 as
date value 0.
Ex: 1/30/1960 has the date value 29.
SAS time defines the relative time in a given date in 24 hours
span, and store the time as the number of seconds since mid-
night (00:00:00 to be 0 second of the date).
Ex: For any given date, say today, 1:30:25 am has the time value in
seconds: 5425 seconds.
SAS datetime is the absolute time counting in seconds starting
from the mid-night on 1/1/1960.
SAS Date, Time Functions to create
numeric SAS date, time values
MDY(Mon, Day, Year) : result in a SAS date value
NOTE: if you use two digits year, the default year-cutoff is applied
(1920).
Ex: MDY(11, 1, 15); is the date value of Nov 1st, 2015.
MDY(11, 1, 35); is the date value of Nov 1st, 1935.
MDY (11, 1, 1915); is the date value of Nov 1st, 1915.
DATA USE_DIF;
DATEDF1=DATDIF('01SEP1984'D,'01NOV2010'D, '30/360');
DATEDF2=DATDIF('01SEP1984'D,'01NOV2010'D, 'ACT/ACT');
YEARDF1=YRDIF('01SEP1984'D,'01NOV2010'D, '30/360');
YEARDF2=YRDIF('01SEP1984'D,'01NOV2010'D, 'ACT/ACT');
PROC PRINT; RUN;
Results:
Obs DATEDF1 DATEDF2 YEARDF1 YEARDF2
1 9420 9557 26.1667 26.1662
Exercise 3
Run the following program and observe how functions
TODAY, YEAR, MONTH, QTR. WEEKDAT, DAY work.
data datefunctions;
date1='25DEC2010'd;
date2=TODAY();
YEAR_date1=YEAR(date1);
MONTH_Date1=MONTH(Date1);
QTR_Date1=QTR(Date1);
WEEKDAY_Date1=WEEKDAY(Date1);
DAY_Date1=DAY(date1);
proc print; format date1 date2 date9.; run;
Exercise 4
Run the following program and observe how INTCK and INTNX functions work.
data dateFunc2;
date1 = '25DEC2010'd;
date2 = TODAY();
NDAYS=INTCK('DAY', Date1, Date2);
NYEARS=INTCK('YEAR', date1, date2);
NMONTH=INTCK('MONTH', date1, date2);
NQTR=INTCK('QTR',date1, date2);
NWEEK=INTCK('WEEK', date1, date2);
Incmonth1=INTNX('MONTH', today(),6, 'b');
incmonth2=INTNX('MONTH', today(),6, 'm');
incmonth3=intnx('month', today(), 6, 'e');
Datediff=DATDIF(date1, date2,'ACT/ACT');
Yeardiff=YRDIF(date1, date2, 'ACT/ACT');
proc print;
format date1 date2 Incmonth1 incmonth2 incmonth3 date9.;
run;
Modify Character Values using SAS
Functions
This section focuses on manipulating character
strings. The objectives include:
Replace the contents of a character value
Trim trailing or leading blanks from a character
value
Search a character value and extract a
proportion of the value
Covert a character values to UPPER, lower and
Proper cases.
SAS Functions for manipulating Character Values
There are many SAS functions for manipulating character strings.
This section will discuss the following functions:
Function Purpose
SCAN Look for a specific word from a character string
SUBSTR Extract a substring or replaces character values
TRIM Trim trailing blanks from character values
LEFT Left align the string that is right-aligned to allow for TRIM the traling blanks
UPCASE Convert the character value to UPPER case
LOWCASE Convert the character value to lower case
PROPCASE Convert the character value to Proper case
CATX Concatenate strings, remove leading, trailing blanks and insert separator
MidName=SCAN(name, 3);
Midinit = SUBSTR(MidName, 1, 1);
Example of using Left-side SUBSTR function
TRIM(Variable);
In case there are LEADING blanks, we can use the function
LEFT(variable), which turn the variable to Left-align, and create
Trailing blank, instead. We can then apply TRIM function:
TRIM(LEFT(Variable));
Converting character values into UPPER,
Lower, and Proper cases
UPCASE(character value) returns the character values all in UPPER
case.
Ex: UPCASE( Mission street) returns MISION STREET
LOWCASE(character value) returns the strings all in lower case.
Ex: LOWCASE( Mission street) returns mission street
PROPCASE(character value) returns the value with 1st character
upper case and the rest in lower cases.
Ex: PROPCASE( MISSION street) returns Mission Street
These functions are very useful when dealing with character values,
especially when we use IF statement that involving character
values, especially when values are stored in mixed cases.
CATX Function
When concatenating character strings, it often requires to trim
leading and trailing blanks, and provides separator to separate
words in order to obtain the correct new character strings. One
can use TRIM, LEFT, concatenating separators to do the task.
Starting SAS 9.1, a new SAS function, CATX is created to handle all
of these at the same step.
The following program reads the data set and creates the address
label for each individual using CATX function
Data job;
INFILE ;
input name $ 1-20 jobtype $ 22-40 city $ 42-53 State $ 55-63
zipcode 65-71;
Address = CATX(, , PROPCASE(city), PROPCASE(state), ZIPCODE);
Exercise 5
Open c13_1 program
Run program 1, observe how SCAN function works, and
see the variable attributes, especially the variable length.
Then, add the Length statement to define the length for
Fname0 to Fname10 the length of 10. Run the program,
check the length again.
Run program 2 and observe the results and the variable
attributes. Then add Length statement for Name with
length 20. Run the program, and see the results.
Run program 3 and observe the results and the variable
attributes. Then add Length statement for Name with
length 20. Run the program, and see the results.
INDEX Function
INDEX function is used to search a character value for a specified
string.
It searches from Left to Right, looking for the first occurrence of the
string, and returns the POSITION of the strings first character. If
the string does not exist, it returns 0.
General syntax:
INDEX(source, excerpt);
Source specifies the character variables or expression to search.
Excerpt is a character string that is enclosed in quotation marks,
to be searched from the source.
Data dmjob;
Set alljobs;
IF FIND(lowcase(job), data mining, t) > 0;
Run;
TRANWRD Function
TRANWRD function replaces or removes all occurrences of a
pattern of characters from within a character string.
A situation using TRANWRD is to update existing variables in
place, such as change MISS to MS., change Doctor to Dr.
and so on.
General Syntax:
TRANWRD(source, target, replacement);
1.259 1 1.3 2 1
-1.259 -1 -1.3 -1 -2
20.934 20 20.9 21 20
-20.934 -20 -20.9 -20 -21