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

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

 

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

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.

View solution in original post

11 REPLIES 11
Tom
Super User Tom
Super User

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',...);

 

SASlearner97
Obsidian | Level 7
when i try

data _null_;
call symput('week_begins',input((today()weekday(today())+2-(weekday(today())=0)),anydtdte10. )) ;
run;

it doesnt work either
Tom
Super User Tom
Super User

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))

 

SASlearner97
Obsidian | Level 7
by that part of the code i want to get the first day of the week's date,
but actually that's not a valid SAS syntax
Tom
Super User Tom
Super User

Use INTNX().  This will return the date for the Sunday of the current week.

intnx('week',today(),0,'b')
SASlearner97
Obsidian | Level 7
i want the starting day of the week to be Monday
Tom
Super User Tom
Super User

Monday is the day after Sunday.

intnx('week',today(),0,'b')+1
FreelanceReinh
Jade | Level 19

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.

Tom
Super User Tom
Super User

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
mkeintz
PROC Star

@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.

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
SASlearner97
Obsidian | Level 7
data _null_;
call symputx('week_begins',put(intnx('week.2',today(),0,'b'),date9.));
run;

this what works for me

thank you everyone

SAS Innovate 2025: Register Now

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!

Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 11 replies
  • 2222 views
  • 4 likes
  • 4 in conversation