SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Sas_noob25
Obsidian | Level 7

I am attempting to create a new date field in a dataset based on an existing date field. 

 

I'm looking to create a new field 'end_date' that is 60 days after various dates in a dataset in column 'existing_date'.

 

data want;

set have;

format end_date date9.;

end_date = intck('days',existing_date,60);

 

Below are my results.

Sas_noob25_0-1663689397170.png

Thank you in advance!

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
INTCK calculates intervals between two dates.
INTNX increments dates.

end_date = intnx('day', existing_date, 60);
end_date = existing_date + 60;

View solution in original post

7 REPLIES 7
FreelanceReinh
Jade | Level 19

Hello @Sas_noob25,

 

Replace intck with intnx and it should work.

Or simply write

end_date = existing_date+60;

 

Reeza
Super User
INTCK calculates intervals between two dates.
INTNX increments dates.

end_date = intnx('day', existing_date, 60);
end_date = existing_date + 60;

Sas_noob25
Obsidian | Level 7

Thank you! Both of these options worked.

Tom
Super User Tom
Super User

Your code is counting how many day intervals there are between the start date and day number 60 (which is the first day of March in the year 1960) .  That will be a very large negative number.  Since day zero is January first of 1960 you can see why you ended up with dates that are in the 19th century.

 

If you want to add 60 days to the number of days that are stored in EXISTING_DATE then addition is the simplest way.

end_date = existing_date+60;
format end_date date9.;

If you want to add a multiple of some other interval that is NOT the unit that the value is already stored in then you could use the INTNX() function instead of the INTCK() function.   For example if you wanted to add two months you could use the MONTH interval.

end_date = intnx('month',existing_date,2,'s');
Sas_noob25
Obsidian | Level 7

Thank you, both of these worked out for me. in the intnx function, what is the 's' doing at the end?

Tom
Super User Tom
Super User

@Sas_noob25 wrote:

Thank you, both of these worked out for me. in the intnx function, what is the 's' doing at the end?


Read the documentation

 

'alignment'

controls the position of SAS dates within the interval. You must enclose alignment in quotation marks. Alignment can be one of these values: BEGINNING, MIDDLE, END, and SAME. SAME specifies that the date that is returned has the same alignment as the input date. For more information, see SAS DS2 Language Reference.

controls the position of SAS dates within the interval. You must enclose alignment in quotation marks. Alignment can be one of these values:

BEGINNING

specifies that the returned date or datetime value is aligned to the beginning of the interval.

Alias B

MIDDLE

specifies that the returned date or datetime value is aligned to the midpoint of the interval, which is the average of the beginning and ending alignment values.

Alias M

END

specifies that the returned date or datetime value is aligned to the end of the interval.

Alias E

SAME

specifies that the date that is returned has the same alignment as the input date.

Aliases S
SAMEDAY
See SAME Alignment
Default BEGINNING
Data type CHAR, NCHAR, NVARCHAR, VARCHAR
See Aligning SAS Date Output within Its Intervals
ballardw
Super User

Possibly a silly question. Is that 60 days used as a substitute for "two months"? As in what is the expected result of 60 days after 25Mar2022?

 

If you want 25May2022, the same day of the two months later, then you would use Intnx('month',date,2,'s');

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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
  • 7 replies
  • 1144 views
  • 4 likes
  • 5 in conversation