BookmarkSubscribeRSS Feed
monday89
Fluorite | Level 6

I have the following dataset.

What I would like to do is

  • if group = 2 and (stop_date_time_1 is NOT MISSING and occurs on or before final_stop_date_time) then keep = 1
  • else if group = 2 and (stop_date_time_1 is missing) and start_date_time_1 occurs AFTER final_stop_date_time) then keep = 0
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;

5 REPLIES 5
smantha
Lapis Lazuli | Level 10

are your fields numeric or character. If the fields are numeric you can use missing(<fieldname>) instead of <fieldname>='.'.

monday89
Fluorite | Level 6

it is datetime23. format

ballardw
Super User

@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.

monday89
Fluorite | Level 6

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;

ballardw
Super User

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.

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
  • 5 replies
  • 650 views
  • 0 likes
  • 3 in conversation