Hi My current dataset looks like this:
ID | start_date_time | stop_date_time | discharge |
1234 | 4/4/2020 12:42 | 4/9/2020 12:11 | 5 |
4567 | 8/1/2019 19:12 | . | |
8910 | 8/2/2019 18:12 | 8/11/2019 18:12 | 9 |
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
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;
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;
Start by reading the log and fixing the syntax errors. Your code misses a semicolon.
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!
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.