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

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 3958 views
  • 2 likes
  • 4 in conversation