BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
DeepakSwain
Pyrite | Level 9

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

Swain
1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
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;

View solution in original post

9 REPLIES 9
Ksharp
Super User
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;

DeepakSwain
Pyrite | Level 9

Hi Ksharp,

 

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

Thanks.

 

With Kind regards,

Deepak

Swain
DeepakSwain
Pyrite | Level 9

Hi Ksharp,

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

 

Thank you in advance for your kind reply. 

 

Regards,

Deepak

Swain
Ksharp
Super User

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.

DeepakSwain
Pyrite | Level 9

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
Reeza
Super User

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. 

Ksharp
Super User
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;

DeepakSwain
Pyrite | Level 9

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

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 9 replies
  • 1879 views
  • 2 likes
  • 3 in conversation