- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
Thank you in advance!
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
INTNX increments dates.
end_date = intnx('day', existing_date, 60);
end_date = existing_date + 60;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hello @Sas_noob25,
Replace intck with intnx and it should work.
Or simply write
end_date = existing_date+60;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
INTNX increments dates.
end_date = intnx('day', existing_date, 60);
end_date = existing_date + 60;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thank you! Both of these options worked.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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');
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thank you, both of these worked out for me. in the intnx function, what is the 's' doing at the end?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@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 |
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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');