BookmarkSubscribeRSS Feed
Bright
Obsidian | Level 7

Hello,

I have a "date" like 2015-02-22 (format is DATE9.) and I want to have the week number of this date. In this example, the output should be 15W08 (format is WEEKW5.). I am using function week(date) but the output for this example is 60W01, which is incorrect. Any idea of what is going wrong?

Thanks!

7 REPLIES 7
Tom
Super User Tom
Super User

Usually when dates get converted to 1960 is is because you are trying to interpret a DATE value (number of days) as a DATETIME value (number of seconds).  Since there are 86,400 seconds in a day (24*60*60) and only 365 days in a year that means that any reasonable date value will look like some early morning time on 01JAN1960 when treated as a datetime.

Bright
Obsidian | Level 7

@Tom Thanks for the explanation. So what can I do to resolve this issue? Thanks!

Tom
Super User Tom
Super User

Show how you created the variable that you applied the WEEKW format to.

If the value is a DATE value then it should work.  But if you started with a date value and then used the DATEPART() function on it then you will get 01JAN1960 since the DATEPART() function is essentially just dividing the value by 86,400.

 

Bright
Obsidian | Level 7

@Tom I imported this date from a CSV file. The original format of data was yymmdd10. which I changed it to DATE9.

Tom
Super User Tom
Super User

Something else has happened then.  If by import you mean PROC IMPORT then it should have read the value as a date.

You can test by looking that value with different formats.

So if your dataset is named HAVE and the variable is named DATE then run a step like this to look at the values of DATE in the first 5 observations.

data _null_;
  set have (obs=5);
  put date=  +1 date comma10. +1 date yymmdd10.  +1 date weekw5. ;
run;

 

Kurt_Bremser
Super User

The WEEK format is meant to be applied to a SAS date value. If you feed it the result of the WEEK() function (which lies in the range 0-53), the format will return a year/week early in 1960. Apply the format directly to the date, without using the WEEK() function.

PaigeMiller
Diamond | Level 26

I have a "date" like 2015-02-22 (format is DATE9.)

 

No clue what this means. A SAS date with format DATE9. will appear as 22FEB2015. It will never appear as 2015-02-22 if it has that format. Perhaps you have assigned a date value like this

mydate = 2015-02-22;
format mydate date9.;

which would be 100% wrong, and gives the wrong week number you showed us. But I'm guessing. Explain your situation with much more detail. SHOW US your SAS data set, and SHOW US the code you used to create this SAS date. (Just to be clear, we need you to SHOW US the two things mentioned, not one or the other)

--
Paige Miller

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 643 views
  • 2 likes
  • 4 in conversation