SAS OnlineTutor HomeFAQ PageSuggested Learning PathsContents+Searchback||next

Reading Date and Time Values
Using Dates and Times in Calculations


In this lesson so far, you've learned how date and time informats read common date and time expressions in specific forms. Now you will see how converting date and time expressions to numeric SAS date values can be useful, particularly when determining time intervals or performing calculations.

Suppose you work in the billing department of a small community hospital. It's your job to create a SAS data set from the raw data file that is referenced by the fileref Aprdata. A portion of the raw data file below shows data values that represent each patient's

  • last name
  • date checked in
  • date checked out
  • daily room rate
  • equipment cost.

Raw Data File Aprdata
1---+----10---+----20---+----30---+----40
Akron    04/05/99 04/09/99 175.00 298.45
Brown    04/12/99 05/01/99 125.00 326.78
Carnes   04/27/99 04/29/99 125.00 174.24
Denison  04/11/99 04/12/99 175.00  87.41
Fields   04/15/99 04/22/99 175.00 378.96
Jamison  04/16/99 04/23/99 125.00 346.28 


The data set that you create must also include variable values that represent the number of days each person stayed in the hospital, total room charges, and total of all expenses incurred by each patient. Name the data set, identify the raw data file Aprdata, and use formatted input to read the data.

Alert: The following examples are shown with the YEARCUTOFF= system option. When you work with two-digit year data, remember to check the default value of the YEARCUTOFF= option and change it if necessary.

     options yearcutoff=1920;
     data perm.aprbills;
        infile aprdata;
        input LastName $8.

Notice that the values in the second and third fields are in the form mmddyy. To complete the input statement, add instructions to read the values for RoomRate (third field) and EquipCost (fourth field) and add a semicolon.

     options yearcutoff=1920;
     data perm.aprbills;
        infile aprdata;
        input LastName $8. @10 DateIn mmddyy8. +1 DateOut
              mmddyy8. +1 RoomRate 6. @35 EquipCost 6.;

Now that the INPUT statement is complete, calculate the number of days each patient was hospitalized. Because DateIn and DateOut are numeric variables, you can simply subtract to find the difference. But because the dates should be inclusive (patients are charged for both the first and last days), you must add 1 to the difference. Call this new variable Days.

     options yearcutoff=1920;
     data perm.aprbills;
        infile aprdata;
        input LastName $8. @10 DateIn mmddyy8. +1 DateOut
              mmddyy8. +1 RoomRate 6. @35 EquipCost 6.;
        Days=dateout-datein+1;

You can calculate a total room charge by multiplying the variable values for Days and RoomRate.

     options yearcutoff=1920;
     data perm.aprbills;
        infile aprdata;
        input LastName $8. @10 DateIn mmddyy8. +1 DateOut
           mmddyy8. +1 RoomRate 6. @35 EquipCost 6.;
        Days=dateout-datein+1;
        RoomCharge=days*roomrate;

Calculating the total cost for each patient is easy. Create a variable, named Total, whose value is the sum of RoomCharge and EquipCost. Then add a PROC PRINT step and RUN statement to view the new data.

     options yearcutoff=1920;
     data perm.aprbills;
        infile aprdata;
        input LastName $8. @10 DateIn mmddyy8. +1 DateOut
           mmddyy8. +1 RoomRate 6. @35 EquipCost 6.;
        Days=dateout-datein+1;
        RoomCharge=days*roomrate;
        Total=roomcharge+equipcost;
     run;
     proc print data=perm.aprbills;
     run;

Obs LastName DateIn DateOut RoomRate EquipCost Days RoomCharge Total
1 Akron 14339 14343 175 298.45 5 875 1173.45
2 Brown 14346 14365 125 326.78 20 2500 2826.78
3 Carnes 14361 14363 125 174.24 3 375 549.24
4 Denison 14345 14346 175 87.41 2 350 437.41
5 Fields 14349 14356 175 378.96 8 1400 1778.96
6 Jamison 14350 14357 125 346.28 8 1000 1346.28


If the values for DateIn and DateOut look odd to you, remember that these are SAS date values. Applying a format such as MMDDYY displays them as they appeared in Aprdata.


back||next


Copyright © 2002 SAS Institute Inc., Cary, NC, USA. All rights reserved.

Terms of Use & Legal Information | Privacy Statement