Hi,
I'm trying to create a macro that returns the number of months elapsed between two dates. The macro would be called in a datastep pulling in the dates from two variables and returning the months difference into a new variable.
Here is what I have so far:
*************************
%macro months_elapsed(date1, date2);
%let year1 = year(&date1);
%let year2 = year(&date2);
%let month1 = month(&date1);
%let month2 = month(&date2);
%let day1 = day(&date1);
%let day2 = day(&date2);
%let month_diff = &year2*12 + &month2 - &year1*12 - &month1;
%if &day1<&day2 %then
%let month_diff = &month_diff - 1;
&month_diff.;
%mend;
data test2;
set test;
month_diff = %months_elapsed(firstdate,seconddate);
run;
*************************
Everything works splendidly up until the if statement, where SAS cannot resolve my macro variable to be the value of the variables firstdate and seconddate, but instead thinks that &day1 = "firstdate" and &day2 = "seconddate". How do I make sure that the values of firstdate and seconddate are probably carried through into the macro?
I'm trying to create a macro that returns the number of months elapsed between two dates. The macro would be called in a datastep pulling in the dates from two variables and returning the months difference into a new variable.
Here is what I have so far:
*************************
%macro months_elapsed(date1, date2);
%let year1 = year(&date1);
%let year2 = year(&date2);
%let month1 = month(&date1);
%let month2 = month(&date2);
%let day1 = day(&date1);
%let day2 = day(&date2);
%let month_diff = &year2*12 + &month2 - &year1*12 - &month1;
%if &day1<&day2 %then
%let month_diff = &month_diff - 1;
&month_diff.;
%mend;
data test2;
set test;
month_diff = %months_elapsed(firstdate,seconddate);
run;
*************************
Everything works splendidly up until the if statement, where SAS cannot resolve my macro variable to be the value of the variables firstdate and seconddate, but instead thinks that &day1 = "firstdate" and &day2 = "seconddate". How do I make sure that the values of firstdate and seconddate are probably carried through into the macro?