BookmarkSubscribeRSS Feed
suresh123
Calcite | Level 5

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         

 

4 REPLIES 4
novinosrin
Tourmaline | Level 20

Try this

%let date =2017-01-12;
%let et=%sysfunc(intnx(month,%sysfunc(inputn(&date.,yymmdd10.)),1),yymmdd10.);
%put &et.;
novinosrin
Tourmaline | Level 20

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.;
Reeza
Super User

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)

Kurt_Bremser
Super User

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 1086 views
  • 1 like
  • 4 in conversation