Transforming Data with SAS Functions | |
Manipulating SAS Date Values with
Functions![]() ![]() |
![]() |
Now let's complete a task using the YEAR function.
Suppose you need to create a subset of the data set Hrd.Temp that contains information about all temporary employees hired during a specific year, such as 1998. Hrd.Temp contains the dates that employees began work with the company and their ending dates. However, there is no year variable. |
SAS Data Set Hrd.Temp
City | State | Zip | Phone | BeginDate | EndDate | PayRate | Days | Hours |
CARY | NC | 27513 | 6224549 | 14567 | 14621 | 10 | 11 | 88 |
CARY | NC | 27513 | 6223251 | 14524 | 14565 | 8 | 25 | 200 |
CHAPEL HILL | NC | 27514 | 9974749 | 14570 | 14608 | 40 | 26 | 208 |
RALEIGH | NC | 27612 | 6970450 | 14516 | 14527 | 15 | 10 | 80 |
To determine the year in which employees were hired, you can apply the
YEAR function to the variable that contains the employee start date,
BeginDate . You write the YEAR function as
year(begindate) Then, to create the new data set, you include this function in a subsetting IF statement within a DATA step. This subsetting IF statement specifies that only observations in which the YEAR function extracts a value of 1998 are placed in the new data set. data hrd.temp98; set hrd.temp; if year(begindate)=1998; run;
Finally, you add a PROC PRINT step to the program so you can view the new
data set. Notice that the PROC PRINT step includes a FORMAT statement to
display the variables data hrd.temp98; set hrd.temp; if year(begindate)=1998; proc print data=hrd.temp98; format begindate enddate date9.; run; Here is a portion of the PROC PRINT output that is created by your program. Notice that the new data set contains informaton only for those employees that were hired in the year 1998. |
City | State | Zip | Phone | BeginDate | EndDate | Pay Rate |
Days | Hours |
NC | 27514 | 9972070 | 02AUG1998 | 17AUG1998 | 12 | 12 | 96 | |
DURHAM | NC | 27713 | 3633020 | 06OCT1998 | 10OCT1998 | 10 | 5 | 40 |
![]() ![]() ![]() ![]() ![]() ![]() |
|
![]() |
![]() |
Copyright © 2002 SAS Institute Inc.,
Cary, NC, USA. All rights reserved.