BookmarkSubscribeRSS Feed
bel2806
Fluorite | Level 6

I am interested in creating a flag for whether or not an alarm was a true positive.  We consider the alarm a true positive if the 'predictiveAlarm' occurs (=1) within 1 hour prior to the first time an alarm goes off each night.  I.e., row #9 would be a true positive because the predictive alarm went off 20 minutes prior (row #7).  

 

What the data looks like:

ObsPtIDdeviceDtdeviceTmalarmpredictiveAlarm
111119-Sep-170:1800
211119-Sep-170:2800
311119-Sep-171:5800
411119-Sep-172:0801
511119-Sep-172:1801
611119-Sep-172:2801
711119-Sep-172:3801
811119-Sep-172:4800
911119-Sep-172:5810
1011119-Sep-173:0810
1111119-Sep-173:1810
2 REPLIES 2
ballardw
Super User

Your example data show 4 potential candidates for comparing the time when PredictiveAlarm=1. In the shown cases all of them would be within an hour but what if that were not the case? Would earliest, latest or some thing in between be used for the time comparison?

 

I would suggest creating a single SAS datetime valued variable instead of date and time since that will considerably simplify comparing times across midnight. It is not clear to me whether your date or time values are actually SAS date and Time values.

 

You should provide example data in the form of a data step as that resolves such questions. 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.

 

You didn't state it explicitly but I would assume that this has to be within each value for Ptid, correct?

 

And for a last question, what is the desired output appearance for the comparison?

novinosrin
Tourmaline | Level 20

HI @bel2806  Assuming I understand your requirement


data have;
input Obs	PtID	deviceDt :date9.	deviceTm :time5.	alarm	predictiveAlarm;
format deviceDt date9.	deviceTm time5.;
cards;
1	1111	9-Sep-17	0:18	0	0
2	1111	9-Sep-17	0:28	0	0
3	1111	9-Sep-17	1:58	0	0
4	1111	9-Sep-17	2:08	0	1
5	1111	9-Sep-17	2:18	0	1
6	1111	9-Sep-17	2:28	0	1
7	1111	9-Sep-17	2:38	0	1
8	1111	9-Sep-17	2:48	0	0
9	1111	9-Sep-17	2:58	1	0
10	1111	9-Sep-17	3:08	1	0
11	1111	9-Sep-17	3:18	1	0
;

data want;
 do _n_=-999 by 0 until(last.deviceDt);
  set have;
  by ptid deviceDt;
  if predictiveAlarm then _t=deviceTm;
  if not _a and alarm then do;
   _a=deviceTm;
   if 0<=intck('min',_t,_a)<=60 then _n_=_t;
  end;
 end;
 do until(last.deviceDt);
  set have;
  by ptid deviceDt;
  flag=deviceTm=_n_;
  output;
 end;
 drop _:;
run;

proc print noobs;run;

Obs

PtID

deviceDt

deviceTm

alarm

predictiveAlarm

flag

1

1111

09SEP2017

0:18

0

0

0

2

1111

09SEP2017

0:28

0

0

0

3

1111

09SEP2017

1:58

0

0

0

4

1111

09SEP2017

2:08

0

1

0

5

1111

09SEP2017

2:18

0

1

0

6

1111

09SEP2017

2:28

0

1

0

7

1111

09SEP2017

2:38

0

1

1

8

1111

09SEP2017

2:48

0

0

0

9

1111

09SEP2017

2:58

1

0

0

10

1111

09SEP2017

3:08

1

0

0

11

1111

09SEP2017

3:18

1

0

0

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 555 views
  • 0 likes
  • 3 in conversation