BookmarkSubscribeRSS Feed
Sandy10
Calcite | Level 5

Hi

 

How to assign jan 1 as start of week 1 of a year and a week should contain 7 days. i.e, Jan 1 to 7 is a week. 

 

Pls advise.

 

Thanks

5 REPLIES 5
Patrick
Opal | Level 21

Does that mean the last week of the year can have less than 7 days? If so then below should work:

data have;
  format dt date9.;
  input dt:date9.;
  week_in_year=ceil((dt-intnx('year',dt,0,'b')+1)/7);
  datalines;
29dec2019
30dec2019
31dec2019
01jan2020
02jan2020
07jan2020
08jan2020
29dec2020
30dec2020
31dec2020
01jan2021
;

proc print;
run;
 

Patrick_0-1585204170027.png

 

 

 

 

 

 
andreas_lds
Jade | Level 19

Please explain what you want as output. The week-function has a parameter to define the rules for numbering the first week of a year.

Kurt_Bremser
Super User

@andreas_lds wrote:

Please explain what you want as output. The week-function has a parameter to define the rules for numbering the first week of a year.


But those only determine if Sunday or Monday is the first day of a week, and if there's a week 0 or those days belong to the previous year.

ChrisNZ
Tourmaline | Level 20

You cam make it this into a function and forget about the logic.

Reusing @Patrick's formula:

proc fcmp outlib=WORK.FUNCS.DATE;
   function week_from_1jan (DATE);
     return( ceil((DATE-intnx('year',DATE,0,'b')+1)/7) );
   endsub;
run;
   
options cmplib=WORK.FUNCS;

data _null_;
  format DATE date9.;
  input DATE date9.;
  WEEK_IN_YEAR=week_from_1jan(DATE);
  putlog DATE= WEEK_IN_YEAR= ;
datalines;
29dec2019
30dec2019
31dec2019
01jan2020
02jan2020
07jan2020
08jan2020
29dec2020
30dec2020
31dec2020
01jan2021
run;

 

DATE=29DEC2019 WEEK_IN_YEAR=52
DATE=30DEC2019 WEEK_IN_YEAR=52
DATE=31DEC2019 WEEK_IN_YEAR=53
DATE=01JAN2020 WEEK_IN_YEAR=1
DATE=02JAN2020 WEEK_IN_YEAR=1
DATE=07JAN2020 WEEK_IN_YEAR=1
DATE=08JAN2020 WEEK_IN_YEAR=2
DATE=29DEC2020 WEEK_IN_YEAR=52
DATE=30DEC2020 WEEK_IN_YEAR=53
DATE=31DEC2020 WEEK_IN_YEAR=53
DATE=01JAN2021 WEEK_IN_YEAR=1
 

Don't store the function in the WORK library though.

Kurt_Bremser
Super User

Use the julday. format:

data test;
input dt yymmdd10.;
format dt yymmddd10.;
week = ceil(input(put(dt,julday.),3.)/7);
datalines;
2019-12-31
2020-01-01
2020-01-02
2020-01-03
2020-01-04
2020-01-05
2020-01-06
2020-01-07
2020-01-08
2020-03-26
2020-12-31
;

Result:

dt	week
2019-12-31	53
2020-01-01	1
2020-01-02	1
2020-01-03	1
2020-01-04	1
2020-01-05	1
2020-01-06	1
2020-01-07	1
2020-01-08	2
2020-03-26	13
2020-12-31	53

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
  • 5 replies
  • 453 views
  • 4 likes
  • 5 in conversation