I tried to extract year and month from macro function such as
%Get_year(2018-12-31);
can return
&year=2018
&month=12
I tried the following syntax but it return year 1967 instead of 2018 after calling the function %Get_year(2018-12-31);
%macro Get_year(end_dt);
%let edate=%sysfunc(inputn(&end_dt, yymmdd10.), date9.);
%put &edate.;
%let policy_dt = %sysfunc(intnx(month,&end_dt.,+30,end));
%put &policy_dt.;
%let year0 =%sysfunc(year(&policy_dt.));
%let year1 =%sysfunc(year(&edate.));
/* %let month=%sysfunc(month(&end_dt.)); */
%put &year0.;
%put &year1.;
/* %put &month.; */
%mend;
%Get_year(2018-12-31);
output:
"
"
Thanks,
I know this is simple way, but sometime if the input date is not in that format, it doesn't work.
>sometime if the input date is not in that format, it doesn't work.
1. If it's not in this format then this won't work
%let edate=%sysfunc(inputn(&end_dt, yymmdd10.), date9.);
2. What other strings formats do you expect?
Is this what you want ? Previously your &edate. was not a date
%macro Get_year(end_dt);
%let edate=%sysfunc(inputn(&end_dt, yymmdd10.));
%let policy_dt = %sysfunc(intnx(month ,&edate., 30,end));
%let year0 =%sysfunc(year(&policy_dt.));
%let year1 =%sysfunc(year(&edate.));
%let month=%sysfunc(month(&edate.));
%put &year0.;
%put &year1.;
%put &month.;
%mend;
%Get_year(2018-12-31);
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.