Calculate sensitivity and specificity without generating 2 by 2 table

Solved
Frequent Contributor
Posts: 120

Calculate sensitivity and specificity without generating 2 by 2 table

Hi there,

I am trying to calculate sensitivity and specificity of a test (e.g. test2) with reference to a standard test (e.g. test1). I am interested to know a way where I can calculate sensitivity and specificity without multiple proc freq steps.  A sample of my data is gievn below:

``````data sensitivity_study;
input test1 test2;
datalines;
1 1
1 0
0 1
0 0
1 1
1 0
0 0
0 0
0 0
0 0
;
run;``````

With Regards,

Deepak

Swain

Accepted Solutions
Solution
‎06-06-2016 09:42 AM
Super User
Posts: 10,784

Re: Calculate sensitivity and specificity without generating 2 by 2 table

```This could give you a start .

data sensitivity_study;
input test1 test2;
datalines;
1 1
1 0
0 1
0 0
1 1
1 0
0 0
0 0
0 0
0 0
;
run;
proc sql;
select test1,sum(test1=test2)/count(*) as sensitivity,
case when test1=0 then 'specificity' else 'sensitivity'  end as name
from sensitivity_study
group by test1;
quit;

```

All Replies
Super User
Posts: 23,747

Re: Calculate sensitivity and specificity without generating 2 by 2 table

Solution
‎06-06-2016 09:42 AM
Super User
Posts: 10,784

Re: Calculate sensitivity and specificity without generating 2 by 2 table

```This could give you a start .

data sensitivity_study;
input test1 test2;
datalines;
1 1
1 0
0 1
0 0
1 1
1 0
0 0
0 0
0 0
0 0
;
run;
proc sql;
select test1,sum(test1=test2)/count(*) as sensitivity,
case when test1=0 then 'specificity' else 'sensitivity'  end as name
from sensitivity_study
group by test1;
quit;

```
Frequent Contributor
Posts: 120

Re: Calculate sensitivity and specificity without generating 2 by 2 table

Hi Ksharp,

Great solution as I have been looking for without generating 2 by 2 table.

Thanks.

With Kind regards,

Deepak

Swain
Frequent Contributor
Posts: 120

Re: Calculate sensitivity and specificity without generating 2 by 2 table

Hi Ksharp,

Is it possible to extend this code to calculate PPV and NPV.

Regards,

Deepak

Swain
Super User
Posts: 10,784

Re: Calculate sensitivity and specificity without generating 2 by 2 table

Technically , it could be . Give us formula about them .

Or you could change

`sum(test1=test2)/count(*)`

into

`sum(test1 ne test2)/count(*)`

and see whether it is what you want.

Frequent Contributor
Posts: 120

Re: Calculate sensitivity and specificity without generating 2 by 2 table

Hi Ksharp,

I tried the following sas code but could not generate the desired result.

``````data sensitivity_study;
input test1 test2;
datalines;
1 1
1 0
0 1
0 0
1 1
1 0
0 0
0 0
0 0
0 0
;
run;

proc sql;
select test1,sum(test1 ne test2)/count(*) as ppv,
case when test1=0 then 'NPV' else 'PPV'  end as name
from sensitivity_study
group by test1;
quit;``````

Can you kindly help me further.

 Positive Predictive Value =True Positive/(True Positive + False Positive) Negative Predictive Value=True Negative/(True Negative + False Negative)Thank you in advance for your kind reply.Regards,Deepak
Swain
Super User
Posts: 23,747

Re: Calculate sensitivity and specificity without generating 2 by 2 table

The logic is to write a sum statement with the conditions inside the sum statement that meet your requirements, ie true positive is test1=1 and test2=1.

The first link I posted demonstrates this a bit. You should be able to extend the logic already provided to your new scenario. Please try and post what you've tried and isn't working if you need further help.

Super User
Posts: 10,784

Re: Calculate sensitivity and specificity without generating 2 by 2 table

```OK. Just change GROUP variable into TEST2.

data sensitivity_study;
input test1 test2;
datalines;
1 1
1 0
0 1
0 0
1 1
1 0
0 0
0 0
0 0
0 0
;
run;

proc sql;
select test2,sum(test1=test2)/count(*) as ppv,
case when test2=0 then 'NPV' else 'PPV'  end as name
from sensitivity_study
group by test2;
quit;

```
Frequent Contributor
Posts: 120

Re: Calculate sensitivity and specificity without generating 2 by 2 table

Hi Ksharp,

Kindly accept my apology for the delayed reply. Your tips has worked nicely and has provided me a very simple way to calulate Sensitivity, Specificity, PPV, NPV without creating 2 by 2 table.

Thanks a lot for your honest effort to help me.

Appreciated.

Regards,

Deepak

Swain
🔒 This topic is solved and locked.