Hi @Kimani and welcome to the SAS Support Communities!
You could also use PROC SQL:
proc sql;
create table want as
select a.patientid, sum(sign(a.ascore-b.ascore)) as total
from have a, have b
where a.patientid ne b.patientid
group by a.patientid;
quit;
But I think a more efficient solution would use PROC RANK:
proc rank data=have out=rks(drop=a:);
var ascore;
ranks r;
run;
data want(drop=r);
set rks nobs=n;
total=2*r-n-1;
run;
I have checked these suggestions with simulated data (not containing missing values, though).
Edit: Now I have also completed a mathematical proof of the formula total=2*r-n-1.
... View more