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));
%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));
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.
I am looking the the week number of September, 23 2021 which is suppose to be 38 while I am getting a 23
@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
@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));
%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.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.
Ready to level-up your skills? Choose your own adventure.