BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
BCNAV
Quartz | Level 8

I have the following macro to calculate the number of days in a year...

 

%macro leapyear;
	%global lyear_curr lyear_prev;

	%let sysyear_curr = %sysfunc(year("&sysdate"d));
	%let sysyear_prev = %sysevalf(&sysyear_curr - 1);

	%let days_curr_yr = intck('days',(mdy(1,1,&sysyear_curr)),(mdy(1,1,(&sysyear_curr))),'continuous');
	%let days_prev_yr = intck('days',(mdy(1,1,&sysyear_prev)),(mdy(1,1,(&sysyear_prev))),'continuous');

	%put &=days_curr_yr &=days_prev_yr;
%mend;
%leapyear;

I an getting errors on the sysevalf saying it is too long....probably something simple. Any ideas? I plan to use the days to check for leap year and assign a variable accordingly.

1 ACCEPTED SOLUTION

Accepted Solutions
ed_sas_member
Meteorite | Level 14
%macro leapyear;
	%global lyear_curr lyear_prev;

	%let sysyear_curr = %sysfunc(year("&sysdate"d));
	%let sysyear_prev = %sysevalf(&sysyear_curr - 1);

	%let days_curr_yr = %sysfunc(intck(day,%sysfunc(mdy(1,1,&sysyear_curr)),%sysevalf(%sysfunc(mdy(12,31,&sysyear_curr))+1)));
	%let days_prev_yr = %sysfunc(intck(day,%sysfunc(mdy(1,1,&sysyear_prev)),%sysevalf(%sysfunc(mdy(12,31,&sysyear_prev))+1)));

	%put &=days_curr_yr &=days_prev_yr;
%mend;
%leapyear;

View solution in original post

6 REPLIES 6
Reeza
Super User
You need to wrap your functions in %SYSFUNC(). Every single function in a %LET needs to be wrapped, including your INTCK() and MDY(). A data step seems significantly easier here IMO using CALL SYMPUTX().
ed_sas_member
Meteorite | Level 14
%macro leapyear;
	%global lyear_curr lyear_prev;

	%let sysyear_curr = %sysfunc(year("&sysdate"d));
	%let sysyear_prev = %sysevalf(&sysyear_curr - 1);

	%let days_curr_yr = %sysfunc(intck(day,%sysfunc(mdy(1,1,&sysyear_curr)),%sysevalf(%sysfunc(mdy(12,31,&sysyear_curr))+1)));
	%let days_prev_yr = %sysfunc(intck(day,%sysfunc(mdy(1,1,&sysyear_prev)),%sysevalf(%sysfunc(mdy(12,31,&sysyear_prev))+1)));

	%put &=days_curr_yr &=days_prev_yr;
%mend;
%leapyear;
Tom
Super User Tom
Super User

The number of days in the current year?

%put %eval(
 %sysfunc(intnx(year,"&sysdate9"d,0,e))
-%sysfunc(intnx(year,"&sysdate9"d,0,b))
+1);
Reeza
Super User
And if you're using it often, maybe use PROC FCMP to make it a custom function?
Kurt_Bremser
Super User

No errors:

27         %macro leapyear;
28         	%global lyear_curr lyear_prev;
29         
30         	%let sysyear_curr = %sysfunc(year("&sysdate"d));
31         	%let sysyear_prev = %sysevalf(&sysyear_curr - 1);
32         
33         	%let days_curr_yr = intck('days',(mdy(1,1,&sysyear_curr)),(mdy(1,1,(&sysyear_curr))),'continuous');
34         	%let days_prev_yr = intck('days',(mdy(1,1,&sysyear_prev)),(mdy(1,1,(&sysyear_prev))),'continuous');
35         
36         	%put &=days_curr_yr &=days_prev_yr;
37         %mend;
38         %leapyear;
DAYS_CURR_YR=intck('days',(mdy(1,1,2020)),(mdy(1,1,(2020))),'continuous') 
DAYS_PREV_YR=intck('days',(mdy(1,1,2019)),(mdy(1,1,(2019))),'continuous')

But, as is to be expected, the macro variables do not contain values, but the text you stored in them. Close inspection of the first macro variable shows that it would return a zero when used in a data step.

You can greatly simplify your macro by making use of the fact that dates are counts of days:

%macro leapyear;
%global lyear_curr lyear_prev;
%let lyear_curr = %eval(%sysfunc(intnx(year,%sysfunc(today()),1,b))-%sysfunc(intnx(year,%sysfunc(today()),0,b)));
%let lyear_prev = %eval(%sysfunc(intnx(year,%sysfunc(today()),0,b))-%sysfunc(intnx(year,%sysfunc(today()),-1,b)));
%mend;
%leapyear
%put &lyear_curr;
%put &lyear_prev;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 6 replies
  • 1935 views
  • 2 likes
  • 5 in conversation