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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
%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;

View solution in original post

8 REPLIES 8
Vigneswar
Obsidian | Level 7

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

PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
Vigneswar
Obsidian | Level 7
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?

Thanks for your response.
PaigeMiller
Diamond | Level 26

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

--
Paige Miller
DanielLangley
Quartz | Level 8

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

 

Ksharp
Super User
%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;
Vigneswar
Obsidian | Level 7
Thank you so much Ksharp!

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 8 replies
  • 6334 views
  • 2 likes
  • 5 in conversation