DATA Step, Macro, Functions and more

I've been using SAS for about 2 months and I need help with Auto Calling Dates and the formats.

Reply
Occasional Contributor
Posts: 13

I've been using SAS for about 2 months and I need help with Auto Calling Dates and the formats.

Hello!

I started this new job in the middle of February and this is my first time using SAS. 'm trying to create macro dates in different formats for a program that we run monthly at our company. This is what I've come up with so far. It runs fine, but I'm unsure if this is the most efficient way to create the dates and with the different formats. Any advice would be greatly appreciated! Thank you!!

DATA Dates1;

       Rundate = INTNX('DAY', TODAY(), 0, 'B');  /* Today */

       MC = INTNX('MONTH', rundate, 0, 'B');  /* Month Current*/

       MP = INTNX('MONTH', rundate, -1, 'B'); /* Month Past*/

/* FORMAT DATE9 */

       CALL SYMPUT ('MC_DATE9',  CATS(PUT(MC,  DATE9.)));

       CALL SYMPUT ('MP_DATE9',  CATS(PUT(MP,  DATE9.)));

/* FORMAT DATE11 */

       CALL SYMPUT ('MC_DATE11',  CATS(PUT(MC,  DATE11.)));

       CALL SYMPUT ('MP_DATE11',  CATS(PUT(MP,  DATE11.)));

/* FORMAT YYMMN6 */

       CALL SYMPUT ('MC_YYMMN6',  CATS(PUT(MC,  YYMMN6.)));

       CALL SYMPUT ('MP_YYMMN6',  CATS(PUT(MP,  YYMMN6.)));

/* FORMAT QOUTED DATE9 */

       CALL SYMPUT ('MC_QDATE9',  CATS("'",PUT(MC,  DATE9.),"'"));

       CALL SYMPUT ('MP_QDATE9',  CATS("'",PUT(MP,  DATE9.),"'"));

/* FORMAT QOUTED DATE11 */

       CALL SYMPUT ('MC_QDATE11',  CATS("'",PUT(MC,  DATE11.),"'"));

       CALL SYMPUT ('MP_QDATE11',  CATS("'",PUT(MP,  DATE11.),"'"));

/* FORMAT QOUTED YYMMN6 */

       CALL SYMPUT ('MC_QYYMMN6',  CATS("'",PUT(MC,  YYMMN6.),"'"));

       CALL SYMPUT ('MP_QYYMMN6',  CATS("'",PUT(MP,  YYMMN6.),"'"));

              put MC     = DATE11.;   /* Month Current  */

              put MP     = DATE11.;   /* Month Past 1 Date Beginning *

RUN;


Respected Advisor
Posts: 3,156

Re: I've been using SAS for about 2 months and I need help with Auto Calling Dates and the formats.

The following does not use data step, so it seems to be less verbose.

%let mc_date9=%sysfunc(intnx(month,%sysfunc(today()),0,b),date9.);

%put &mc_date9;

Haikuo

Super User
Posts: 7,771

Re: I've been using SAS for about 2 months and I need help with Auto Calling Dates and the formats.

With Haikuo's method you avoid the messages from the data step.

Aside from that, just put your date-generating code in a separate file and %inc it everywhere you need the dates.

However you do it, this piece of code will not use more than just a few milliseconds, so you should do what feels most readable to you.

Efficiency is a point of consideration when you start dealing with massive data.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Occasional Contributor
Posts: 13

Re: I've been using SAS for about 2 months and I need help with Auto Calling Dates and the formats.

Thank you both for great advice. I am trying to format the dates as in Ha Kui's example just to see the difference. I’m sure I’ll have more questions soon!  

Ask a Question
Discussion stats
  • 3 replies
  • 276 views
  • 3 likes
  • 3 in conversation