Using Dates and Times in Calculations

Suppose you work in the billing department of a small community hospital. It is 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 the following patient data:
  • last name
  • date checked in
  • date checked out
  • daily room rate
  • equipment cost
Figure 19.1 Raw Data File Aprdata
Raw data file Aprdata.
The data set that you create must also include variable values that represent how many days each person stayed in the hospital, the total room charges, and the total of all expenses that each patient incurred. To create the SAS program, you must first name the data set, identify the raw data file Aprdata, and use formatted input to read the data.
The following example is 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=1926; 
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 (fourth field) and EquipCost (fifth field), and add a semicolon.
options yearcutoff=1926; 
data perm.aprbills; 
   infile aprdata;
   input LastName $8. @10 DateIn mmddyy8. +1 DateOut 
         mmddyy8. RoomRate 6. @34 EquipCost 6.;
Now that the INPUT statement is complete, calculate how many 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=1926; 
data perm.aprbills; 
   infile aprdata; 
   input LastName $8. @10 DateIn mmddyy8. +1 DateOut 
         mmddyy8. RoomRate 6. @34 EquipCost 6.; 
   Days=dateout-datein+1;
You can calculate a total room charge by multiplying Days times RoomRate.
options yearcutoff=1926; 
data perm.aprbills; 
   infile aprdata; 
   input LastName $8. @10 DateIn mmddyy8. +1 DateOut 
         mmddyy8. RoomRate 6. @34 EquipCost 6.; 
   Days=dateout-datein+1; 
   RoomCharge=days*roomrate;
To calculate the total cost for each patient, create a variable named Total whose value is the sum of RoomCharge and EquipCost. Then add a PROC PRINT step and a RUN statement to view the new data.
options yearcutoff=1926; 
data perm.aprbills; 
   infile aprdata; 
   input LastName $8. @10 DateIn mmddyy8. +1 DateOut 
   mmddyy8. RoomRate 6. @34 EquipCost 6.; 
   Days=dateout-datein+1; 
   RoomCharge=days*roomrate; 
   Total=roomcharge+equipcost; 
run; 
proc print data=perm.aprbills; 
run;
Figure 19.2 Table Created with PROC PRINT
Table created with PROC PRINT that shows input values and the values for the Total variable.
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.
When the DATA step executes the following program, the values for DateIn and DateOut are converted to SAS date values.
options yearcutoff=1926; 
data perm.aprbills; 
   infile aprdata; 
   input LastName $8. @10 DateIn mmddyy8. +1 DateOut 
         mmddyy8. RoomRate 6. @35 EquipCost 6.; 
   Days=dateout-datein+1; 
   RoomCharge=days*roomrate; 
   Total=roomcharge+equipcost; 
run;
Here is how the data moves through the program data vector in order to create the report.
Figure 19.3 Raw Data File Aprdata and Program Data Vector
Raw data file Aprdata and Program Data Vector
Now, the variable Days is created by subtracting DateIn from DateOut and adding 1.
options yearcutoff=1926; 
data perm.aprbills; 
   infile aprdata; 
   input LastName $8. @10 DateIn mmddyy8. +1 DateOut 
      mmddyy8. RoomRate 6. @34 EquipCost 6.; 
   Days=dateout-datein+1; 
   RoomCharge=days*roomrate; 
   Total=roomcharge+equipcost; 
run;
Figure 19.4 Program Data Vector
Program Data Vector.
The value for RoomCharge is calculated next. RoomCharge is the product of Days and RoomRate.
options yearcutoff=1926; 
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;
Figure 19.5 Raw Data File Aprdata and Program Data Vector
Raw data file Aprdata and Program Data Vector.
The value for Total is the final calculation. Total is the sum of EquipCost and RoomCharge.
options yearcutoff=1926; 
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;
Figure 19.6 Raw Data File Aprdata and Program Data Vector
Raw data file Aprdata and Program Data Vector.
Last updated: January 10, 2018
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.217.254.118