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;
Thank you in advance for your kind reply.
With Regards,
Deepak
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;
This is the same as this question, essentially.
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;
Hi Ksharp,
Great solution as I have been looking for without generating 2 by 2 table.
Thanks.
With Kind regards,
Deepak
Hi Ksharp,
Is it possible to extend this code to calculate PPV and NPV.
Thank you in advance for your kind reply.
Regards,
Deepak
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.
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 |
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.
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;
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
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.