I have the following dataset.
What I would like to do is
| SubjectID | final_stop_date_time | start_date_time_1 | stop_date_time_1 | group |
| 11 | 12FEB2020:10:59:00 | 11FEB2020:15:30:00 | 11FEB2020:16:08:00 | 2 |
| 11 | 12FEB2020:10:59:00 | 11FEB2020:16:08:00 | 12FEB2020:10:59:00 | 2 |
| 11 | 12FEB2020:10:59:00 | 12FEB2020:21:40:00 | . | 2 |
Want:
| SubjectID | final_stop_date_time | start_date_time_1 | stop_date_time_1 | group | keep |
| 11 | 12FEB2020:10:59:00 | 11FEB2020:15:30:00 | 11FEB2020:16:08:00 | 2 | 1 |
| 11 | 12FEB2020:10:59:00 | 11FEB2020:16:08:00 | 12FEB2020:10:59:00 | 2 | 1 |
| 11 | 12FEB2020:10:59:00 | 12FEB2020:21:40:00 | . | 2 | 0 |
I tried the following and doesn't work
data want; set have;
if group = '2' and
(stop_date_time_1 <= final_stop_date_time) then keep = 1 ;
else if
(group = '2' and ((start_date_time_1 > final_stop_date_time ) and missing(stop_date_time_1)) OR
(group = '2' and (stop_date_time_2 > final_stop_date_time))
then keep = 0;
run;
are your fields numeric or character. If the fields are numeric you can use missing(<fieldname>) instead of <fieldname>='.'.
it is datetime23. format
@smantha wrote:
are your fields numeric or character. If the fields are numeric you can use missing(<fieldname>) instead of <fieldname>='.'.
The MISSING function will work with character variables as well. So a better suggestion than you first thought.
Still doesn't work
data want; set have;
if group = '2' and
(stop_date_time_1 <= final_stop_date_time) then keep = 1 ;
else if
(group = '2' and ((start_date_time_1 > final_stop_date_time ) and missing(stop_date_time_1)) OR
(group = '2' and (stop_date_time_2 > final_stop_date_time))
then keep = 0;
run;
Show the log for your data step. Show the code and any messages. Copy from the LOG and paste into a code box opened on the forum with the</> icon. The code box is important because the message windows will reformat text including SAS generated diagnostic text.
(group = '2' and ((start_date_time_1 > final_stop_date_time ) and missing(stop_date_time_1)) OR (group = '2' and (stop_date_time_2 > final_stop_date_time))
count the ( and ) you have mismatched parentheses.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.