0
votes

My question is as follows - I have a code that adds one month to the macro variable and the code works fine:

%let month=1;
%let act_dt = %sysfunc(MDY(&month,1,2016));
%let x_akt=%sysfunc(intnx(MONTH,&act_dt,1),yymmdd10.);
%put current month: &act_dt;
%put plus one month: &x_akt;

giving me the output:

current month: 20454
plus one month: 2016-02-01

But if I add a type of format to the first macro variable, then the function intnx does not work properly.

%let month=1;
%let act_dt = %sysfunc(MDY(&month,1,2016),yymmdd10.);
%let x_akt=%sysfunc(intnx(MONTH,&act_dt,1),yymmdd10.);
%put current month: &act_dt;
%put plus one month: &x_akt; 

with the outcome:

current month: 2016-01-01
27         %put plus one month: &x_akt;
plus one month: 1965-08-01     

Thank you for any advice, why is it like that and if there is a way how to present both macro variables in the same format.

1

1 Answers

1
votes

You can see most of the reason where your first log gives 'current month' as 20454. That's clearly not a human-readable date. In fact, it's SAS's internal representation of the date 1st Jan 2016, represented as the number of days since 1st January 1960.

Your first example works because it passes that numeric value to the INTNX() function, which is what that function needs and expects. Your second example passes the character value '2016-01-01' to the function, which SAS tries to handle by partially converting it to the numeric value 2016, which (taken as a number of days since 1st Jan 1960) is 9th July 1965. The INTNX() function then moves that forward by one month as before.

All SAS date/time function expect to receive numeric input. There is no separate date/time data type in SAS, just numerics with particular date/time formats applied to them. To work in macro variables, your code must either store and manipulate the date values as numbers and then only convert them with a PUT() function for display, or else store them as formatted dates but always convert them back to numeric with a %SYSFUNC(INPUT()) when passing the value to a date/time function.

It's often clearer to do this kind of manipulation in a short DATA _NULL_ step than to have lots of macro code full of %SYSFUNCS().