Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MONNAME changes results 1

Status
Not open for further replies.

tharpa

Programmer
Sep 22, 2009
3
0
0
US
When I run the following code, it works fine:
Code:
libname learn 'C:\books\learning';

data frequency;
set learn.hosp;
DayOfTheWeek = weekday(AdmitDate);

monthOfTheYear = month(AdmitDate) ;

year = year(AdmitDate);
run;

proc freq data=frequency;
tables DayOfTheWeek monthOfTheYear year;
format DayOfTheWeek DOWNAME9. ;

run;
However, when I format the month using MONNAME like so, it thinks that all of the dates are in January, which is incorrect:

Code:
libname learn 'C:\books\learning';

data frequency;
set learn.hosp;
DayOfTheWeek = weekday(AdmitDate);

monthOfTheYear = month(AdmitDate) ;

year = year(AdmitDate);
run;

proc freq data=frequency;
tables DayOfTheWeek monthOfTheYear year;
format DayOfTheWeek DOWNAME9. [COLOR=red]monthOfTheYear MONNAME9.[/color];

run;


What am I misunderstanding?
 
I have reduced the problem to something that illustrates the anomaly more clearly. :

Code:
data today;

b = today();
DayofTheWeek=weekday(b);

put b = date.;
format DayOfTheWeek DOWNAME9.;
run;

proc print data=today;
run;

The result is off by a day. This example uses the system date, but for whatever date I use, the weekday() is off by a day.

Is it a bug, or am I doing something wrong?
 
I've added a field to try to demonstrate why you are seeing this. First, let me explain how SAS handles dates. SAS holds dates as numeric values starting at 0 and adding 1 for each following day. Additional, the counting starts with Jan 01 1960.
So to SAS
Jan 1, 1960 = 0
Jan 2, 1960 = 1
Jan 3, 1960 = 2
..etc
Oct 5, 2009 = 18175.

As shown above, Oct 05 2009 is day number 18175 to SAS. The Weekday() function converts that number (18175) to just the number 2.
As listed above, SAS thinks Jan 2 1960 = 2, so when the format function is run on "weekday(b)" is returns, Sunday Jan 2 1960 as shown in variable DayofTheWeek2. It just happens to be that the days of the week in 2009 are one day from the ones in 1960.

Code:
data today;
b = today();
DayofTheWeek=weekday(b);
DayofTheWeek2 = DayofTheWeek;
format DayofTheWeek date9.;
format DayofTheWeek2 weekdate.;
run;

proc print data=today;
run;

I hope this helps.
 
Basically, to put it simply, the format DOWNAME9. needs to be applied to the original date, not the day of the week number.
Code:
data today;

b = '7OCT2009'd;
DayofTheWeek=weekday(b);

put b = date.;
put b = DOWNAME9.;
run;

Chris
Business Analyst, Code Monkey, Data Wrangler.
SAS Guru.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top