Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- Home
- /
- Programming
- /
- Programming
- /
- Calculate months between two dates in SAS

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

☑ This topic is **solved**.
Need further help from the community? Please
sign in and ask a **new** question.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Posted 06-20-2023 02:49 PM
(193 views)

Hi all,

somehow I can't calculate the months between 20211231 and 20230228.

Code:Copy to clipboard

%let End = %sysfunc(intnx(month,%sysfunc(today()),-4,e),YYMMDDN8.);

%let Begin = %sysfunc(intnx(year, %sysfunc(date()), -2, e),YYMMDDN8.);

%put &Begin;

%put &End;

%let count_months =%sysfunc(intck(month,&Begin.,&End.));

%put &count_months;

I get the following log statement:

WARNING: An argument to the function INTCK referenced by the %SYSFUNC or %QSYSFUNC macro function is out of range.

NOTE: Mathematical operations could not be performed during %SYSFUNC function execution.

The result of the operations have been set to a missing value.

Can someone help me with synthax?

Many thanks in advance.

Kind regards,

Ben

somehow I can't calculate the months between 20211231 and 20230228.

Code:Copy to clipboard

%let End = %sysfunc(intnx(month,%sysfunc(today()),-4,e),YYMMDDN8.);

%let Begin = %sysfunc(intnx(year, %sysfunc(date()), -2, e),YYMMDDN8.);

%put &Begin;

%put &End;

%let count_months =%sysfunc(intck(month,&Begin.,&End.));

%put &count_months;

I get the following log statement:

WARNING: An argument to the function INTCK referenced by the %SYSFUNC or %QSYSFUNC macro function is out of range.

NOTE: Mathematical operations could not be performed during %SYSFUNC function execution.

The result of the operations have been set to a missing value.

Can someone help me with synthax?

Many thanks in advance.

Kind regards,

Ben

1 ACCEPTED SOLUTION

Accepted Solutions

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Don't apply the format when creating the value, that changes the values that INTCK expects to see.

When you formatted the value you changed a numeric value on the order of 23181 to 20230620. INTCK and other date functions in SAS expect to see a value that is the number of days since 01JAN1960 as an argument. So the formatted value such as 20230620 would be roughly in year 55,388. The SAS date functions won't work with any dates past the year 20,000 .

If you have a need for a formatted version of Begin and End then create separate non-formatted variables.

5 REPLIES 5

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Remove the format (YYMMDD) from the macro variables.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Thanks for your help.

Now it works.

Kind regards,

Ben

Now it works.

Kind regards,

Ben

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

A SAS date value is an integer *count of days, starting at 1960-01-01*.

So the number 20211231 would be *very far* in the future, beyond the range of available formats.

Maxim 28: Macro Variables Need no Formats.

```
%let End = %sysfunc(intnx(month,%sysfunc(today()),-4,e));
%let Begin = %sysfunc(intnx(year,%sysfunc(today()),-2,e));
```

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

DO NOT format macro variables. This prevents mathematical operations from working correctly.

Use unformatted macro variables

```
%let End = %sysfunc(intnx(month,%sysfunc(today()),-4,e));
%let Begin = %sysfunc(intnx(year, %sysfunc(date()), -2, e));
%put &Begin;
%put &End;
%let count_months =%sysfunc(intck(month,&Begin.,&End.));
%put &count_months;
```

--

Paige Miller

Paige Miller

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Don't apply the format when creating the value, that changes the values that INTCK expects to see.

When you formatted the value you changed a numeric value on the order of 23181 to 20230620. INTCK and other date functions in SAS expect to see a value that is the number of days since 01JAN1960 as an argument. So the formatted value such as 20230620 would be roughly in year 55,388. The SAS date functions won't work with any dates past the year 20,000 .

If you have a need for a formatted version of Begin and End then create separate non-formatted variables.

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

**If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. **

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.