DATA Step, Macro, Functions and more

SAS date issue

Reply
Occasional Contributor
Posts: 18

SAS date issue

Iam trying to get the next month of a date.

 

 

%let date =2017-01-12;
%let et=%sysfunc(intnx(month,&date.,1),yymmdd10.);
%put &et.;

 

But result is different. What went wrong

1          OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
 61         
 62         %put &et.;
 1965-07-01
 63         
 64         OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
 77         

 

PROC Star
Posts: 1,356

Re: SAS date issue

Posted in reply to suresh123

Try this

%let date =2017-01-12;
%let et=%sysfunc(intnx(month,%sysfunc(inputn(&date.,yymmdd10.)),1),yymmdd10.);
%put &et.;
PROC Star
Posts: 1,356

Re: SAS date issue

Posted in reply to novinosrin

For same dates instead of 1st of month-->

%let date =2017-01-12;
%let et=%sysfunc(intnx(month,%sysfunc(inputn(&date.,yymmdd10.)),1,s),yymmdd10.);
%put &et.;
Super User
Posts: 22,874

Re: SAS date issue

Posted in reply to suresh123

SAS dates need to be specified as date literals to be automatically recognized as dates. 

 

How is the compiler supposed to know if you want 2017-01-12 = 2017-1-12 = 2017-13=2004

or that it's a date?

 

The way to do that is specify dates in a date literal format:

 

%let date = '12Jan2017'd;
%let et=%sysfunc(intnx(month,&date.,1),yymmdd10.);
%put &et.;

This works. If you want to specify the date in a different format, then YOU need to handle the conversion, which is demonstrated in @novinosrin code using the INPUTN()  function. (INPUT() does not work with %SYSFUNC, but INPUTN() does)

Super User
Posts: 9,611

Re: SAS date issue

Posted in reply to suresh123

2017-01-12 used as a date parameter will be evaluated as 2004 (2017 minus 1 minus 12), which means 2004 days after 01jan1960.

Use a proper informat as @novinosrin suggests.

Hint: macro variables to be used as dates should contain raw date values (5-digit numbers as of now); human-readable form is not needed. Makes everything easier.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Ask a Question
Discussion stats
  • 4 replies
  • 132 views
  • 1 like
  • 4 in conversation