BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
WilliamB
Obsidian | Level 7

Hello,

I want to get the week number from a column with a format (DATETIME20.). I want the year before the week number.
Precision the first day of the week must be Monday

Like this:

Table        
DT_EXTR     DT_EXTR week number
16FEB2022:00:00:00     16FEB2022:00:00:00 202207
25JAN2022:00:00:00     25JAN2022:00:00:00 202204
      20MAR2022:00:00:00 202211

 

Thanks for your help

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

Hello @WilliamB,

 

You can also use one of the two SAS formats YYWEEKV. or YYWEEKW., as shown below. I've added 01JAN2022 to the HAVE dataset to include an example where the two formats differ: With YYWEEKW. the value of week_number would be '202200'.

data have;
input dt_extr :datetime.;
format dt_extr datetime20.;
cards;
01JAN2022:00:00:00
16FEB2022:00:00:00
25JAN2022:00:00:00
20MAR2022:00:00:00
;

data want;
set have;
length week_number $6;
week_number=compress(put(datepart(dt_extr),yyweekv.),'W');
run;

Result:

                             week_
Obs         dt_extr          number

 1     01JAN2022:00:00:00    202152
 2     16FEB2022:00:00:00    202207
 3     25JAN2022:00:00:00    202204
 4     20MAR2022:00:00:00    202211

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26

You use the WEEK function. Please be aware that some years will have 53 Mondays.

 

data want;
    set have;
    week_number=cats(year(datepart(dt_extr)),week(datepart(dt_extr,'w')));
run;
--
Paige Miller
FreelanceReinh
Jade | Level 19

Hello @WilliamB,

 

You can also use one of the two SAS formats YYWEEKV. or YYWEEKW., as shown below. I've added 01JAN2022 to the HAVE dataset to include an example where the two formats differ: With YYWEEKW. the value of week_number would be '202200'.

data have;
input dt_extr :datetime.;
format dt_extr datetime20.;
cards;
01JAN2022:00:00:00
16FEB2022:00:00:00
25JAN2022:00:00:00
20MAR2022:00:00:00
;

data want;
set have;
length week_number $6;
week_number=compress(put(datepart(dt_extr),yyweekv.),'W');
run;

Result:

                             week_
Obs         dt_extr          number

 1     01JAN2022:00:00:00    202152
 2     16FEB2022:00:00:00    202207
 3     25JAN2022:00:00:00    202204
 4     20MAR2022:00:00:00    202211
ballardw
Super User

You can roll your own format to display a date time value in that manner:

proc format;
picture dtyrwk (default=6)
low-high = '%Y%W' (datatype=datetime)
;
run;

data example;
  input dt :datetime18.;
  format dt datetime18.;
  yrwk = put(dt,dtyrwk.);
datalines;
16FEB2022:00:00:00 	
25JAN2022:00:00:00 
20MAR2022:00:00:00 
;

proc print data=example noobs;
  format dt dtyrwk.;
run;

The format may mean that you don't need to add a different variable, just use the format as needed.

Ksharp
Super User
data have;
input dt_extr :datetime.;
format dt_extr datetime20.;
cards;
01JAN2022:00:00:00
16FEB2022:00:00:00
25JAN2022:00:00:00
20MAR2022:00:00:00
;

data want;
set have;
length week_number $6;
week_number=nldate(datepart(dt_extr),'%Y%W');
run;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1594 views
  • 6 likes
  • 5 in conversation