BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
monday89
Fluorite | Level 6

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

1 ACCEPTED SOLUTION

Accepted Solutions
mklangley
Lapis Lazuli | Level 10

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;

View solution in original post

2 REPLIES 2
ballardw
Super User

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.

mklangley
Lapis Lazuli | Level 10

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: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 6447 views
  • 2 likes
  • 3 in conversation