## days in month between two dates

hi i am looking to create macro with days in month like( 30,31,30) from two dates. which i will use later in array as macro like(array days_mnt{6} 8 _temporary_( &daycount.);) any suggestions please. thanks

1 ACCEPTED SOLUTION

Accepted Solutions

## Re: days in month between two dates

April, May, June, July, August, September

That is 6 months, why do you have only 5 entries?

As others have mentioned this isn't a great approach. There are better ways to include that calculation into the loop below itself, rather than separate it into separate steps. But there's an answer for you at least.

and I am looking to create macro which will provide me daycount(30,31,30,31,30) so I can use this later in array as below. thanks for guiding

``````data demo;
length days_list \$40.;
*starting dates;
start_date='01apr2020'd;
end_date='30sep2020'd;
*calculate number of months between intervals;
nMonths=intck('month', start_date, end_date, 'C');
*assign to new variable that can be incremented;
month_start=start_date;
*loop over Number of months;

do i=1 to nMonths+1;
*get the month end;
month_end=intnx('month', month_start, 1, 'b');
*calcuate the number of days;
nDays=month_end - month_start;
*add value to the days list;
days_list=catx(', ', days_list, nDays);
*output to data set - optional;
output;
*set start to new month;
month_start=month_end;
end;

*create macro variable for later usage or put interest calculation into loop above;
call symputx('ndays_list', days_list, 'g');
format month: start_date end_date date9.;
run;

%put &ndays_list;``````
8 REPLIES 8

## Re: days in month between two dates

How are you providing the end dates, i.e. start and end dates?

Also how will expect to use that value? You can always get the number of days in a given month for a SAS date value with

days = day(intnx('month',date,0,'e'));

day function returns the day of month, the intnx function shown advances any given date to the end of the month.

## Re: days in month between two dates

hi I have data like this

start_date=01apr2020

end_date=30sep2020

and I am looking to create macro which will provide me daycount(30,31,30,31,30) so I can use this later in array as below. thanks for guiding

array days_mnt{6} 8 _temporary_( &daycount.);

## Re: days in month between two dates

@SASUser0001 wrote:

hi I have data like this

start_date=01apr2020

end_date=30sep2020

and I am looking to create macro which will provide me daycount(30,31,30,31,30) so I can use this later in array as below. thanks for guiding

array days_mnt{6} 8 _temporary_( &daycount.);

And just how are you going to use that array? It may not be needed. There are a number of SAS functions for dealing with date values and almost anything I can imagine wanting a not very well defined array of days in month is generally covered in one of the SAS functions.

INTNX will increment dates, times or datetime values by intervals such as week, month, quarter and year plus multiples and shifts of those (or with time values hours, minutes, and seconds)

The INTCK function returns the number of intervals between two values.

Functions like Year, Month, Day, Hour, and such extract those values from a date or time value.

## Re: days in month between two dates

so basically I am looking to calculate interest for 6 months , each record start date and end date is different I have created array to calculate interest and now I am looking to create macro for days in each month for 6 months . so for each record (1 to 6 months ) interest will be calculated based on month.

ints_1=

((balance *Interest_rate )/100)/365)*31;

bal = balance + ints_1;

so trying to replace days in month (31) with actual days in month by using data _null_step or macro

thanks

## Re: days in month between two dates

It does not sound like you want either a macro variable or a macro program.  The actual calculation of days in a month and interest should be done in actual SAS code, not in macro code.

Once you have it working you can think about whether any of it would benefit from being generated by a macro variable reference or some macro logic.

## Re: days in month between two dates

April, May, June, July, August, September

That is 6 months, why do you have only 5 entries?

As others have mentioned this isn't a great approach. There are better ways to include that calculation into the loop below itself, rather than separate it into separate steps. But there's an answer for you at least.

and I am looking to create macro which will provide me daycount(30,31,30,31,30) so I can use this later in array as below. thanks for guiding

``````data demo;
length days_list \$40.;
*starting dates;
start_date='01apr2020'd;
end_date='30sep2020'd;
*calculate number of months between intervals;
nMonths=intck('month', start_date, end_date, 'C');
*assign to new variable that can be incremented;
month_start=start_date;
*loop over Number of months;

do i=1 to nMonths+1;
*get the month end;
month_end=intnx('month', month_start, 1, 'b');
*calcuate the number of days;
nDays=month_end - month_start;
*add value to the days list;
days_list=catx(', ', days_list, nDays);
*output to data set - optional;
output;
*set start to new month;
month_start=month_end;
end;

*create macro variable for later usage or put interest calculation into loop above;
call symputx('ndays_list', days_list, 'g');
format month: start_date end_date date9.;
run;

%put &ndays_list;``````

## Re: days in month between two dates

You may also want to check out the SAS financial functions.

I suspect that any approach that was going to use days in the manner implied may have been completely writing the interest calculations.

One function actually name FINANCE takes as the first parameter what type of calculation is involved.

Others like CUMIPMT, cumulative interest paid on a loan, IPMT interest payment for a given period and others.

Plus if you have  SAS/ETS there is Proc Loan and other goodies

Discussion stats
• 8 replies
• 1267 views
• 5 likes
• 4 in conversation