Extracting Day of Week and Day of Month from a SAS Date - SAS Programming

The SAS System contains functions to compute day of the week and day of the month from a SAS date. It is easy to confuse the two. The DAY function returns a number from 1 to 31 which represents the day of the month corresponding to a given date; the WEEKDAY function returns a number from 1 to 7,corresponding to the day of the week.Suppose you want to chart the frequency of hospital admissions by day of the week and day of the month using the HOSP data from . Here is the program:

Exarrmle

PROC FORMAT; O
VALUE DAYFMT 1='SUN' 2='MON' 3='TUE' 4='WED
5='THU' 6='FRI' ?='SAT';
RON;
DATA PATIENT;
INF1LE 'HOSP';
INPUT €1 ID $2.
85 ADMIT MMDDYY6.
115 DISCHRG MMDDYY8,
€25 COST 5.?
DAY_WEEK * WEEKDAY (ADMIT); ©
DAYJ10N - DAY (ADMIT); ®
tABBL ADMIT ='Admission Date'
DISCHRG ='Discharge Date'
COST ='Cost of Treatment'
DAYJ=EEK-'Day of the Week'
DAYJION -'Day of the Month';
FORMAT ADMIT DISCHRG MMDDYY8, COST DOLLARS,
DAYJfEEK DAYFMT,;
RUN;
PROC CHART DATA=PATIENT;
VBAR DAY_WEEK DAY MON/DISCRETE;
RUN;

Line © computes DAY_WEEK which is the day of the week, a number from 1 to 7.Line © computes DAY_MON,the day of the month, a number from 1 to 31.PROCFORMAT 0 creates a format for DAY_WEEK.As is usually the case with the SAS System, there is more than one way to achieve the same results.In the current example, DAY_WEEK could have been calculated in an alternative manner which would not necessitate the PROCFORMAT step.The following line of code,

DAY_WEEK=PUT(ADMIT,WEEKDATE3.);

creates a three-character variable (DAY_WEEK) which has as its value SUN,MON,TUE, etc.The only other difference is that DAY_WEEK is now a character variable as opposed to the earlier method which yields a numeric value for DAY_WEEK.In all likelihood, however, this would not present a major difficulty, if at all.This is just another testament to the versatility and power of the SAS System.


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

SAS Programming Topics