Working with Dates in the SAS System |
Sorting Dates |
Because SAS date values are numeric variables, you can sort them and use them in calculations. The following example uses the data set MYLIB.TOURDATES to extract other information about the Tradewinds Travel data.
To help determine how frequently tours are scheduled, you can print a report with the tours listed in chronological order. The first step is to specify the following BY statement in a PROC SORT step to tell SAS to arrange the observations in ascending order of the date variable DepartureDate:
by DepartureDate;
By using a VAR statement in the following PROC PRINT step, you can list the departure date as the first column in the report:
options pagesize=60 linesize=80 pageno=1 nodate; proc sort data=mylib.fmttourdate out=sortdate; by DepartureDate; run; proc print data=sortdate; var DepartureDate Country Nights; title 'Departure Dates Listed in Chronological Order'; run;
The following output displays the results:
Departure Dates Listed in Chronological Order 1 Departure Obs Date Country Nights 1 03JUN1997 Russia 14 2 24OCT1998 Australia 12 3 17OCT1999 Greece 12 4 13MAY2000 Japan 8 5 27AUG2000 Ireland 7 6 10NOV2000 Venezuela 9 7 14JAN2001 Switzerland 9 8 03FEB2001 New Zealand 16 9 28FEB2001 Brazil 8 10 25APR2001 Italy 8
The observations in the data set SORTDATE are now arranged in chronological order. Note that there are no FORMAT statements in this example, so the dates are displayed in the DATE9. format you assigned to DepartureDate when you created the data set MYLIB.FMTTOURDATE.
Creating New Date Variables |
Because you know the departure date and the number of nights spent on each tour, you can calculate the return date for each tour. To start, create a new variable by adding the number of nights to the departure date, as follows:
Return = DepartureDate + Nights;
The result is a SAS date value for the return date that you can display by assigning it the DATE9. format, as follows:
options yearcutoff=1920 pagesize=60 linesize=80 pageno=1 nodate; data home; set mylib.tourdates; Return = DepartureDate + Nights; format Return date9.; run; proc print data=home; title 'Dates of Departure and Return'; run;
Dates of Departure and Return 1 Departure Obs Country Date Nights Return 1 Japan 14743 8 21MAY2000 2 Greece 14534 12 29OCT1999 3 New Zealand 15009 16 19FEB2001 4 Brazil 15034 8 08MAR2001 5 Venezuela 14924 9 19NOV2000 6 Italy 15090 8 03MAY2001 7 Russia 13668 14 17JUN1997 8 Switzerland 14989 9 23JAN2001 9 Australia 14176 12 05NOV1998 10 Ireland 14849 7 03SEP2000
Note that because the variable DepartureDate in the data set MYLIB.TOURDATES has no permanent format, you see a numeric value instead of a readable calendar date for that variable.
Copyright © 2012 by SAS Institute Inc., Cary, NC, USA. All rights reserved.