Datetime macro

Accepted Solution Solved
Reply
New Contributor
Posts: 2
Accepted Solution

Datetime macro

Hello,

I'm trying to create previous month beg of month and end of month dates as macro variable in datetime format from the below  start_mon macro variable . 

%Let start_mon = NOV2012;

The output I am trying to get is

%Let  Beg_Dte = 2012-10-01 00:00:00;

%Let End_Dte = 2012-10-31 23.59:59;

I found the below picture format in SAS forum but  have not be able to work out the code. I would greatly appreciate any help.

proc format;

picture dtfmt

other='%Y-%0m-%0d %0H:%0M:%0S'(datatype=datetime);

run;


Accepted Solutions
Solution
‎10-23-2012 09:00 PM
Super User
Super User
Posts: 6,498

Re: Datetime macro

Use the INTNX function.  You will want to use the DTMONTH interval type.

3    proc format;

4      picture dtfmt

5        other='%Y-%0m-%0d %0H:%0M:%0S'(datatype=datetime)

6      ;

NOTE: Format DTFMT has been output.

7    run;


10   %let start_mon = NOV2012;

11   %let beg_dte = %sysfunc(intnx(dtmonth,"01&start_mon:00:00:00"dt,0,B),dtfmt.);

12   %let end_dte = %sysfunc(intnx(dtmonth,"01&start_mon:00:00:00"dt,0,E),dtfmt.);

13   %put &start_mon &beg_dte &end_dte;

NOV2012 2012-11-01 00:00:00 2012-11-30 23:59:59

View solution in original post


All Replies
Solution
‎10-23-2012 09:00 PM
Super User
Super User
Posts: 6,498

Re: Datetime macro

Use the INTNX function.  You will want to use the DTMONTH interval type.

3    proc format;

4      picture dtfmt

5        other='%Y-%0m-%0d %0H:%0M:%0S'(datatype=datetime)

6      ;

NOTE: Format DTFMT has been output.

7    run;


10   %let start_mon = NOV2012;

11   %let beg_dte = %sysfunc(intnx(dtmonth,"01&start_mon:00:00:00"dt,0,B),dtfmt.);

12   %let end_dte = %sysfunc(intnx(dtmonth,"01&start_mon:00:00:00"dt,0,E),dtfmt.);

13   %put &start_mon &beg_dte &end_dte;

NOV2012 2012-11-01 00:00:00 2012-11-30 23:59:59

New Contributor
Posts: 2

Re: Datetime macro

Thank you!!

☑ This topic is SOLVED.

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

Discussion stats
  • 2 replies
  • 222 views
  • 1 like
  • 2 in conversation