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:
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;
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.
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;
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;
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;