DATA Step, Macro, Functions and more

Data _null_ and date formatting

Reply
Frequent Contributor
Posts: 143

Data _null_ and date formatting

data null;

BPM = INTNX('Month',&Rundate,-1,'b');  /*first day last month*/

EPM = INTNX('Month',&Rundate,-1,'e'); /*last day last month*/

CALL SYMPUT('pmb',BPM);

CALL SYMPUT('pme',EPM);

run;

%put &pmb &pme ;

The above datastep will return a 5 digit number that represents the dates (ie 19663)

data date1;

last_month_begin=&pmb;

last_month_end=&pme;

format last_month_begin last_month_end mmddyy10.;

run;

I ran this datastep to convert the results from a number to an actual date format (ie 19663 becomes 11/1/2013)

Is there a way to avoid this datastep date1 and address the desired date formatting in the data null process instead?

Respected Advisor
Posts: 3,156

Re: Data _null_ and date formatting

You can avoid using data step at all if you want,

%let pmb=%sysfunc(INTNX(Month,&Rundate,-1,b),mmddyy10.);

%let pme=%sysfunc(INTNX(Month,&Rundate,-1,e),mmddyy10.);

Haikuo

Valued Guide
Posts: 2,177

Re: Data _null_ and date formatting

Have you heard about date constants? They make date parameters so much easier to handle - they really are worth the effort of the learning.

They allow your code to look much more readable with proper dates rather than a number of days since 1960. Not just more readable but also much better for checking.

They work like:

WHERE dateVariable  < "22dec2013"d

So it is really helpful that %SYSFUNC() can so easily provide the formatted dates like

%LET end_lastM = %SYSFUNC( INTNX( month, "&sysdate9"d, -1, E ), date9 ) ;

%LET beg_lastM = %SYSFUNC( INTNX( month, "&end_lastM"d, 0, B ), date9 ) ;

which might be used like

where tran_date between "&beg_lastM"d and "&end_lastM"

Ask a Question
Discussion stats
  • 2 replies
  • 431 views
  • 0 likes
  • 3 in conversation