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

Hi My current dataset looks like this:

 

IDstart_date_timestop_date_timedischarge
12344/4/2020 12:424/9/2020 12:115
45678/1/2019 19:12 .
89108/2/2019 18:128/11/2019 18:129

 

WHat I would like to do

if discharge < 7 then new_stop_date_time = stop_date_time

else if discharge >= 7 then new_stop_date_time  = stop_date_time

else if discharge = . then new_stop_date_time  = start_date_time + 7 days

 

I tried the following

 


data o.time_to_first_event2; set o.time_to_first_event;
if discharge < 7 then new_stop_date_time= stop_date_time;
else if discharge >= 7 then new_stop_date_time= stop_date_time;
else discharge=. then new_stop_date_time= intnx('dtday', start_date_time, 7);
format new_stop_date_time DATETIME16.;
run;

 

Not working! 

discharge is in BEST12. format

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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.

 

One common problem we all learn with SAS is that missing is "less than everything". So you may need something like:

data o.time_to_first_event2; set o.time_to_first_event;

if  0 <= discharge < 7 then new_stop_date_time= stop_date_time;
else if discharge >= 7 then new_stop_date_time= stop_date_time;
else discharge=. then new_stop_date_time= intnx('dtday', start_date_time, 7);
format new_stop_date_time DATETIME16.;
run;

Pick a reasonable value for the "0 <= discharge" I show above.

Or you could preface the If/then with something like:

data o.time_to_first_event2; set o.time_to_first_event;
if not missing(discharge) then do;
   if discharge < 7 then new_stop_date_time= stop_date_time;
   else if discharge >= 7 then new_stop_date_time= stop_date_time;
end;
else new_stop_date_time= intnx('dtday', start_date_time, 7);
format new_stop_date_time DATETIME16.;
run;

View solution in original post

2 REPLIES 2
ballardw
Super User

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.

 

One common problem we all learn with SAS is that missing is "less than everything". So you may need something like:

data o.time_to_first_event2; set o.time_to_first_event;

if  0 <= discharge < 7 then new_stop_date_time= stop_date_time;
else if discharge >= 7 then new_stop_date_time= stop_date_time;
else discharge=. then new_stop_date_time= intnx('dtday', start_date_time, 7);
format new_stop_date_time DATETIME16.;
run;

Pick a reasonable value for the "0 <= discharge" I show above.

Or you could preface the If/then with something like:

data o.time_to_first_event2; set o.time_to_first_event;
if not missing(discharge) then do;
   if discharge < 7 then new_stop_date_time= stop_date_time;
   else if discharge >= 7 then new_stop_date_time= stop_date_time;
end;
else new_stop_date_time= intnx('dtday', start_date_time, 7);
format new_stop_date_time DATETIME16.;
run;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

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
  • 400 views
  • 0 likes
  • 3 in conversation