I have the following dataset. What I would like to do is that if day is blank then add 7 days to start_dtm
ptid | days | start_dtm |
1 | 03DEC19:04:35:00 |
Something like this:
ptid | days | start_dtm | stop_dtm |
1 | 03DEC19:04:35:00 | 10DEC19:04:35:00 |
I tried both codes and doesn't work:
if missing(days) then stop_dtm = intnx('dtday', start_dtm, 7);
ALSO
if missing(days) then stop_dtm = start_dtm + 7*24*60*60
but i am getting 01Jan20:23:59 for stop date
Try running the code I included. It looks like both those approaches will add seven days. (The second one will ensure the timestamp is correct.)
How are you getting 01Jan20:23:59 for the stop date? Could you include the exact code you ran for that?
data have;
input ptid days start_dtm :datetime16.;
format start_dtm datetime16.;
datalines;
1 . 03DEC19:04:35:00
;
run;
data want;
set have;
format stop_dtm stop_dtm2 datetime16.;
if missing(days) then stop_dtm = intnx('dtday', start_dtm, 7);
if missing(days) then stop_dtm2 = start_dtm + 7*24*60*60;
run;
I suspect that you want:
if missing(days) then stop_dtm = intnx('dtday', start_dtm, 7,'S');
the S, alignment, in the last position means "same", as in the same hour and minute. Default is 'B' or begin for the Intnx function would would advance to the start of the day or 00:00:00
(you didn't show the result you were getting).
Hint: you may want to make sure that your dates display with 4 digit years.
And without your actual values and the complete code of a data step it really isn't possible to tell why something goes "wrong".
Doesn't work is awful vague.
Are there errors in the log?: Post the code and log in a code box opened with the <> to maintain formatting of error messages.
No output? Post any log in a code box.
Unexpected output? Provide input data in the form of data step code pasted into a code box, the actual results and the expected results. Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the <> icon or attached as text to show exactly what you have and that we can test code against.
Try running the code I included. It looks like both those approaches will add seven days. (The second one will ensure the timestamp is correct.)
How are you getting 01Jan20:23:59 for the stop date? Could you include the exact code you ran for that?
data have;
input ptid days start_dtm :datetime16.;
format start_dtm datetime16.;
datalines;
1 . 03DEC19:04:35:00
;
run;
data want;
set have;
format stop_dtm stop_dtm2 datetime16.;
if missing(days) then stop_dtm = intnx('dtday', start_dtm, 7);
if missing(days) then stop_dtm2 = start_dtm + 7*24*60*60;
run;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.