Help using Base SAS procedures

Remove machine that was service at same time range

Accepted Solution Solved
Reply
Regular Contributor
Posts: 240
Accepted Solution

Remove machine that was service at same time range

Hi I have a 2 tables that 

 

 Machine.     Time.                      Date 

 

A1.               15:35:35         11/07/15

A2                10:59:06        11/04/15

A1.                 15:42:23.       11/07/15

 

Table2

 

 Machine.     Time.                      Date 

A1.               15:38:35         11/07/15

A2                10:55:06        11/04/15

A3               15:45:23.       11/07/15

 

I need to look into both tables an keep the entries that are in one table an not the other.if there are 2 entries that have same date ,same machine an the time falls between 15 min of each table exclude one of the 2...the output shoult look like this 

Machine.     Time.                       Date 

   A3               15:45:23.       11/07/15.                                     

A1.               15:35:35         11/07/15

A2.               10:55:06        11/04/15     


Accepted Solutions
Solution
‎12-13-2015 06:33 PM
Respected Advisor
Posts: 4,920

Re: Remove machine that was service at same time range

[ Edited ]

I suggest to use datetimes. It makes the code simpler and allows matching times just before and after midnight :

 

data t1;
input Machine $ Time :time8. Date :mmddyy8.;
datalines; 
A1               15:35:35         11/07/15
A2                10:59:06        11/04/15
A1                 15:42:23       11/07/15
;
data t2;
input Machine $ Time :time8. Date :mmddyy8.;
datalines; 
A1               15:38:35         11/07/15
A2                10:55:06        11/04/15
A3               15:45:23       11/07/15
;

/* Concatenate the tables, create datetimes */
data t;
set t1 t2;
dt = dhms(date, hour(time), minute(time), second(time));
format dt datetime19. date date9. time time8.;
run;

proc sort data=t; by machine dt; run;

/* Drop records that are less than 15 min. from previous */
data want;
do until(last.machine);
    set t; by machine;
    if dt - '00:15:00't > lastDT then do;
        output;
        lastDT = dt;
        end;
    end;
drop lastDT;
run;

proc print data=want noobs; run;
PG

View solution in original post


All Replies
Regular Contributor
Posts: 240

Re: Remove machine that was service at same time range

If it falls greater than 15 min keep both of them. .
Solution
‎12-13-2015 06:33 PM
Respected Advisor
Posts: 4,920

Re: Remove machine that was service at same time range

[ Edited ]

I suggest to use datetimes. It makes the code simpler and allows matching times just before and after midnight :

 

data t1;
input Machine $ Time :time8. Date :mmddyy8.;
datalines; 
A1               15:35:35         11/07/15
A2                10:59:06        11/04/15
A1                 15:42:23       11/07/15
;
data t2;
input Machine $ Time :time8. Date :mmddyy8.;
datalines; 
A1               15:38:35         11/07/15
A2                10:55:06        11/04/15
A3               15:45:23       11/07/15
;

/* Concatenate the tables, create datetimes */
data t;
set t1 t2;
dt = dhms(date, hour(time), minute(time), second(time));
format dt datetime19. date date9. time time8.;
run;

proc sort data=t; by machine dt; run;

/* Drop records that are less than 15 min. from previous */
data want;
do until(last.machine);
    set t; by machine;
    if dt - '00:15:00't > lastDT then do;
        output;
        lastDT = dt;
        end;
    end;
drop lastDT;
run;

proc print data=want noobs; run;
PG
Regular Contributor
Posts: 240

Re: Remove machine that was service at same time range

Hi pgstats. How would it look like if I wanted to keep everything within 15min? ... If it was greater than 15 min drop.. Table one columns would be machine time date. Table 2 machine start date end date duration time
Respected Advisor
Posts: 4,920

Re: Remove machine that was service at same time range

Post as a new question with example data and expected output.

PG
🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 337 views
  • 1 like
  • 2 in conversation