Hello everyone, I hopeyou are doing great!
i want to make a date throught this code:
data _null_;
call symput('week_begins',put(input((today()-weekday(today())+2-(weekday(today())=0)),anydtdte10. ),date9.)) ;
run;
%put week_begins;
I want to get the first day of the current week
the result of %put week_begins; would be 07/12/2020
please help,
thank you
Hello @SASlearner97,
If you consider Monday the first day of the week, use
intnx('week.2',today(),0,'b')
or shorter (since 'b' is the default):
intnx('week.2',today(),0)
An equivalent of this expression using the WEEKDAY function is
today()-weekday(today())+2-7*(weekday(today())=1)
Perhaps you meant that (but you definitely wouldn't want to apply the INPUT function with a date informat to a SAS date value).
Edit: Note that just adding 1 to the Sunday (which SAS considers the first day of the week, i.e., of the 'week' interval of the INTNX function) is not equivalent to using the shifted interval 'week.2': If "today" is Sunday, the "+1" will lead to the first day of (your) next week.
If you want the first day of the week using the INTNX() function.
week_begins=intnx('week',today(),0,'b');
If you want the string of characters in the macro variable WEEK_BEGINS to be 07/12/2020 why would you use the DATE9 format to generate the string? That will make either 07DEC2020 or 12JUL2020 depending on whether 7 in you example is the day or the month.
call symputx('week_begins',put(...,mmddyys10.));
call symputx('week_begins',put(...,ddmmyys10.));
How are you planning to USE the macro variable? If you are going to use the value in code to assign or test date values it would be better to just have the raw integer that represents that date.
call symputx('week_begins',...);
Please explain with "doesn't work" means. Did you get an error message? Post the lines from the SAS log with the data step and any error message.
What did you think this part of the code meant? It is currently not valid SAS syntax.
(today()weekday(today())+2-(weekday(today())=0))
Use INTNX(). This will return the date for the Sunday of the current week.
intnx('week',today(),0,'b')
Monday is the day after Sunday.
intnx('week',today(),0,'b')+1
Hello @SASlearner97,
If you consider Monday the first day of the week, use
intnx('week.2',today(),0,'b')
or shorter (since 'b' is the default):
intnx('week.2',today(),0)
An equivalent of this expression using the WEEKDAY function is
today()-weekday(today())+2-7*(weekday(today())=1)
Perhaps you meant that (but you definitely wouldn't want to apply the INPUT function with a date informat to a SAS date value).
Edit: Note that just adding 1 to the Sunday (which SAS considers the first day of the week, i.e., of the 'week' interval of the INTNX function) is not equivalent to using the shifted interval 'week.2': If "today" is Sunday, the "+1" will lead to the first day of (your) next week.
The difference is how it treats Sunday.
4875 data _null_; 4876 do today=3 to 10; 4877 week_b1 = intnx('week',today,0,'b')+1; 4878 week_b2 = intnx('week.2',today,0,'b'); 4879 put today downame.-l +1 (today week:) ( = date9.); 4880 end; 4881 run; Monday today=04JAN1960 week_b1=04JAN1960 week_b2=04JAN1960 Tuesday today=05JAN1960 week_b1=04JAN1960 week_b2=04JAN1960 Wednesday today=06JAN1960 week_b1=04JAN1960 week_b2=04JAN1960 Thursday today=07JAN1960 week_b1=04JAN1960 week_b2=04JAN1960 Friday today=08JAN1960 week_b1=04JAN1960 week_b2=04JAN1960 Saturday today=09JAN1960 week_b1=04JAN1960 week_b2=04JAN1960 Sunday today=10JAN1960 week_b1=11JAN1960 week_b2=04JAN1960 Monday today=11JAN1960 week_b1=11JAN1960 week_b2=11JAN1960
@SASlearner97 wrote:
Hello everyone, I hopeyou are doing great!
i want to make a date throught this code:
data _null_;
call symput('week_begins',put(input((today()-weekday(today())+2-(weekday(today())=0)),anydtdte10. ),date9.)) ;
run;
%put week_begins;
I want to get the first day of the current week
the result of %put week_begins; would be 07/12/2020
please help,
thank you
As @Tom said, the results you would get in your code once you fix the INTNX function would be 12jul2020 (i.e. the date9 format), which doesn't match your desired format. However, I think you're better off staying with the date9. format. The reason is that it can be used, without further transformation as a date literal.
For instance, I would suggest:
data _null_;
call symputx('week_begins',put(today(),date9.));
run;
%put week_begins;
because then all you have to do to, (for instance, get a subset) is:
data subset;
set mylib.mydata;
if date>="&week_begins"d;
run;
This is simply a matter of enclosing the macrovar in double quotes and appending the letter d. It would be slightly messier if the macrovar were in any other date format.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.