Reading a Date from Raw Data - SAS Programming

There are many ways to represent dates in a raw data file. For example, February 13, 1982, could be found as:

02/13/82
02-13-82
02+13,82
13FEB82
021382
02/13/1982
13/02/82
82044 (Julian Date)
1982044 (Julian Date)

SAS software can read all these forms and more. For example,if each of these date values starts in column 8, you can read each of them with the appropriate informat as follows:

Reading a Date from Raw Data

Now, here is the first example.You want to read in the following raw data:

Raw data file HOSP

Write a SAS program to read in these data and compute the number of days between the admission and discharge dates. You will then print out a report showing: ID, ADMIT, DISCHRG, LOS,and COST, where LOS is the length of stay that you computed. Here is the program:

Example

DATA PATIENT;
INFILE 'HOSP'?
INPUT @i ID $2.
€5 ADMIT MMDDYY8,
€15 DISCHRG MMDDYY8.
€25 COST 5.;
LOS * DISCHRG-ADMIT+1?
LABEL ADMIT *'Admission Date'
DISCHRG-'Discharge Date'
COST ='Cost of Treatment'
LOS »'Length of Stay';
FORMAT ADMIT DISCHRG MMDDYY8. COST DOLLARS.;
RUN;
PROC PRINT LABEL DATA=PATIENT;
TITLE 'Hospital Report';
ID ID;
VAR ADMIT DISCHRG LOS COST;
RON;

Why do we add one day (ADMIT + 1) to the LOS calculation? This is something that most people forget to do when calculating a range in general. In this case, when patients come in for same-day surgery,if you just take DISCHRG-ADMTT as the LOS, you would lose all of those days.Now, here is the output:

Output from Example- Reading a Date from Raw Data

Output from Example- Reading a Date from Raw Data

The FORMAT statement assigning the format MMDDYY8. to the variables ADMIT and DISCHRG is necessary to make the displayed date values understandable. Also note the use of the DOLLAR8,format applied to COST to enhance its readability. Had you omitted the FORMAT statement, the listing would have looked like this:

Hospital report

The numbers you see under the Admission Date and Discharge Date columns are the number of days from January 1,1960, corresponding to each of the dates.When you see output like this,you should be reminded to include date formats for your date variables. Also note that the values for COST are not easily readable without the dollar signs and commas.


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

SAS Programming Topics