DATA Step, Macro, Functions and more

Date calculation

Accepted Solution Solved
Reply
Contributor
Posts: 26
Accepted Solution

Date calculation

Hello,

 

I want to build a macro which shows me current date in datetime format and a date -1 month.

 

%let date1 = %sysfunc(datetime(),DATETIME16.);

%let date_prev=%sysfunc(intnx(month,"%sysfunc(putn("&date1"d,DATETIME16.))"d,-1,s),DATETIME16.);

%put &date_prev.;

 

Date1 gives me good results but in date_prev i have 31DEC59:23:53:55.

 

How I should change this macro to have date_prev = 07AUG2017time ?


Accepted Solutions
Solution
‎09-07-2017 06:44 AM
PROC Star
Posts: 1,209

Re: Date calculation

[ Edited ]

Why not do it in a data step like this?

 

data _null_;
	date1 = datetime();
	date_prev = intnx('dtmonth', date1, -1, 's');
	format date1 date_prev datetime16.;
	put date1= date_prev=;
run;

View solution in original post


All Replies
Solution
‎09-07-2017 06:44 AM
PROC Star
Posts: 1,209

Re: Date calculation

[ Edited ]

Why not do it in a data step like this?

 

data _null_;
	date1 = datetime();
	date_prev = intnx('dtmonth', date1, -1, 's');
	format date1 date_prev datetime16.;
	put date1= date_prev=;
run;
Super User
Super User
Posts: 9,400

Re: Date calculation

Its been asked many times here.  The question is why would you need to?  You can always get yesterdays date by doing intnx('year',today,-1); so why bother going through all the effort of calculating it, then storing it as text - macro is always text - then trying to use that text in your code.  Its madness.  

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 114 views
  • 1 like
  • 3 in conversation