BookmarkSubscribeRSS Feed
alepage
Barite | Level 11

Hello,

 

I have found an example on the web how to use the week function and for 2021-09-23, I obtain week number 38.  But when I use a macro variable and sysfunc, I am getting 23.  

How do we solve that issue?

data work.dates1;
	format my_date YYMMDD10.;
	input my_date date9.;
	datalines;
08JAN2019
26JUN2020
05DEC2020
23SEP2021
;
run;

data work.dates_extract;
	set work.dates1;
 
	my_year = year(my_date);
	my_quarter = qtr(my_date);
	my_month = month(my_date);
	my_week = week(my_date);
	my_day = day(my_date);
run;

%let date=2021-09-23;
%put %sysfunc(week(&date.,w));
6 REPLIES 6
Reeza
Super User
%let date=2021-09-23;

That is not a SAS date, and will not be interpreted as such. 

 

 

%put %sysfunc(week(&date.,w));

Your function here has a second parameter, W

 

	my_week = week(my_date);

 However your function in the data step does not.

 

Fixing those will resolve your issues.

 


@alepage wrote:

Hello,

 

I have found an example on the web how to use the week function and for 2021-09-23, I obtain week number 38.  But when I use a macro variable and sysfunc, I am getting 23.  

How do we solve that issue?

data work.dates1;
	format my_date YYMMDD10.;
	input my_date date9.;
	datalines;
08JAN2019
26JUN2020
05DEC2020
23SEP2021
;
run;

data work.dates_extract;
	set work.dates1;
 
	my_year = year(my_date);
	my_quarter = qtr(my_date);
	my_month = month(my_date);
	my_week = week(my_date);
	my_day = day(my_date);
run;

%let date=2021-09-23;
%put %sysfunc(week(&date.,w));

 

Tom
Super User Tom
Super User

If you perform the subtraction you requested you get the number 1,989.

To see why that is week number 23 check what calendar date the number 1,989 represents.

227   data _null_;
228     date=2021-09-23;
229     week= week(date,'w');
230     put date= week=;
231     put date= date9. +1 date yymmdd10. ;
232   run;

date=1989 week=23
date=12JUN1965  1965-06-12

So week number 23 sounds about right for the middle of June.

alepage
Barite | Level 11

I am looking the the week number of September, 23 2021 which is suppose to be 38 while I am getting a 23

 

Tom
Super User Tom
Super User

@alepage wrote:

I am looking the the week number of September, 23 2021 which is suppose to be 38 while I am getting a 23

 


Then why didn't you tell SAS to use that date instead of the one from 1965 you did use?

233   data _null_;
234     date='23SEP2021'd;
235     week= week(date,'w');
236     put date= week=;
237     put date= date9. +1 date yymmdd10. ;
238   run;

date=22546 week=38
date=23SEP2021  2021-09-23

You can either put the date literal or the actual number of days into your macro variable.

247   %let date="23SEP2021"d;
248   %let week=%sysfunc(week(&date.,w));
249   %put &=week;
WEEK=38
250   %let date=22546;
251   %let week=%sysfunc(week(&date.,w));
252   %put &=week;
WEEK=38

 

Reeza
Super User

@alepage wrote:

I am looking the the week number of September, 23 2021 which is suppose to be 38 while I am getting a 23

 


%let date=2021-09-23;

That is not a SAS date, and will not be interpreted as such. 

 

That is interpreted as SUBTRACTION -> 2021-9-23 = 1989. 

This is true in macros or non-macro code. 

 

Try using the SAS literal that is used elsewhere instead or the MDY function if you want.

Then the rest of your code will work. 

%let date=%sysfunc(mdy(9, 23, 2021));

 

 

PaigeMiller
Diamond | Level 26
%let date=2021-09-23;

&date is a text string whose value is 2021-09-23, it is not interpreted as a date because that's not how SAS works with dates. So, it's not a date, and thus you shouldn't expect the right answer from the WEEK function. In SAS, dates are the number of days since 01JAN1960. 

 

If you want a macro variable that has the date value of September 23, 2021, which is 22546 days since 01JAN1960, you can use

 

%let date= %sysevalf('23SEP2021'd);

and now SAS can work with this value, and produce the proper week calculation.

--
Paige Miller

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

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
  • 6 replies
  • 1614 views
  • 2 likes
  • 4 in conversation