Hi,
I am trying to create a macro variable that generates a date (which will be Monday of that week) when I run the code on any given day of that week.
So here is an example,
lets take I am running the macro today(29_01_2019), output should be Jan_29_2019 .
Suggestions are appreciated.
Thanks in advance!
Vigneswar
%let t=%sysfunc(today());
%let want=%sysfunc(intnx(week.2,&t,0,b),date9.);
%let want2=%substr(&want,3,3)_%substr(&want,1,2)_%substr(&want,6);
%put &want &want2;
Hi,
I am trying to create a macro variable that generates a date (which will be Monday of that week) when I run the code on any given day of that week.
So here is an example,
lets take I am running the macro today(29_01_2019), output should be Jan_29_2019 .
Suggestions are appreciated.
Thanks in advance!
Vigneswar
output should be Jan_29_2019 .
But January 29, 2019 is not a Monday.
Also, you haven't really defined the problem in a way specific enough to be programmed. How do you determine the "week" in order to determine the Monday of the week? Do weeks begin on Sunday? Do weeks begin on Monday? Something else?
It's very hard to generalize and write actual code given your brief comments.
And ... NO MACRO NEEDED, regardless.
@Vigneswar wrote:
Paige Miller,
Sorry that was a good catch. I wanted my output as Jan_28_2019 .
Let me summarize again.
Lets take I am running a ODS code that generates an xml file and the name of the file should be in this format mon_dd_yyyy. Let it be I run the code on any day in a week, the output date should be monday of that week.
Is the requirement clear enough?
No, it is not clear. You have not defined what you mean by "week". Does it begin on Sunday? Does it being on Monday? Or something else? Please tell us the algorithm by which you have an input date, and you wind up with another date which should be Monday.
Please keep your questions in a single thread. It makes it much easier to keep track of answers and proposed solutions.
Datasteps are far better at manipulating this sort of information and you would be better off not using a macro for this at all. Regardless this should do what you want. I have used the weekdate format so that you can see the Monday. Feel free to modify it to how you want your result to be.
%macro datein(date="1JAN2019"d);
data _null_;
/*weekdate format allows us to see the day of the week*/
format HaveDate weekdate.
WantDate weekdate.
;
HaveDate = &date.;
/*find out how many days away from previous monday*/
WeekDayNum = mod(weekday(HaveDate) + 5,7);
/*subtract those dates*/
WantDate = HaveDate - WeekDayNum;
/* format the date as desired */
result=put(WantDate,weekdate.);
/*save as a macro result*/
call symputx('WantDate',result,'L');
run;
%put &WantDate.;
%mend;
%datein(date="1JAN2019"d);
%datein(date="10JAN2019"d);
%let t=%sysfunc(today());
%let want=%sysfunc(intnx(week.2,&t,0,b),date9.);
%let want2=%substr(&want,3,3)_%substr(&want,1,2)_%substr(&want,6);
%put &want &want2;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.