BookmarkSubscribeRSS Feed
fsuzhang
Fluorite | Level 6

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:

"

31DEC2018
2890
ERROR: Argument 1 to function YEAR referenced by the %SYSFUNC or %QSYSFUNC macro function is not a number.
ERROR: Invalid arguments detected in %SYSCALL, %SYSFUNC, or %QSYSFUNC argument list. Execution of %SYSCALL statement or %SYSFUNC
or %QSYSFUNC function reference is terminated.
 
1967

"

 

 

4 REPLIES 4
Astounding
PROC Star
You're making life very difficult. Why not:

%let year = %substr(&end_dt, 1, 4);
fsuzhang
Fluorite | Level 6

Thanks, 

 

I know this is simple way, but sometime if the input date is not in that format, it doesn't work.

ChrisNZ
Tourmaline | Level 20

>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?

34reqrwe
Quartz | Level 8

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);

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 5367 views
  • 2 likes
  • 4 in conversation