"Efficiency" may depend on how many variables with similar values.
Similar calculations for multiple variables often indicates an array approach might work. So the block of the comparisons to single values could be done a couple of ways.
Here is one:
data trial;
set have;
hcp_score=0;
if hcp_screen=1 then hcp_score=hcp_score+1;
else if hcp_screen=2 then hcp_score=hcp_score+0.5;
if hcp_home=1 then hcp_score=hcp_score+1;
else if hcp_home=2 then hcp_score=hcp_score+0.5;
if hcp_edu_change in (1,9) then hcp_score=hcp_score+0.25;
/* below are the SINGLE value comparisons*/
array vars hcp_list hcp_cohort hcp_restricted hcp_singlefac
hcp_edu_covid hcp_edu_sick hcp_edu_ip ;
/* this array holds the COMPARISON values for the variables
IN ORDER*/
array vals {7} _temporary_ (1,1,1,1,1,1,1);
/* this has the score additions*/
array sc {7} _temporary_ (1,1,1,1, 0.25,0.25,0.25);
do i=1 to dim(vars);
if vars[i]=vals[i] then hcp_score=hcp_score + sc[i];
end;
drop i;
run;
The only change shown is for the Single value comparisons. The first array has the variables you need to compare, the second array, vals, contains the values that the variables are tested for equality and third has the amount to add to the score. The order of the variables, values and score additions must match in order.
You might see right off had that if I have to add 10 more variables I add them to the VARS list, then the value to compare, then score. The do loop with the number of elements in the vars list takes care of all of the conditional additions to the score total.
Note that this is really simple for single values. You could use it for multiple values by placing the variable on the list twice with the corresponding Values for comparsion and the corresponding score additions. That just is a tad harder to see right away.
If the above test code I show above works as expected then you could try
data trial;
set have;
hcp_score=0;
array vars hcp_list hcp_cohort hcp_restricted hcp_singlefac
hcp_edu_covid hcp_edu_sick hcp_edu_ip
hcp_screen hcp_screen
hcp_home hcp_home
hcp_edu_change hcp_edu_change
;
/* this array holds the COMPARISON values for the variables
IN ORDER*/
array vals {13} _temporary_ (1,1,1,1,1,1,1,1,2,1,2,1,9);
/* this has the score additions*/
array sc {13} _temporary_ (1,1,1,1, 0.25,0.25,0.25,1,0.5,1,0.5, 0.25,0.25);
do i=1 to dim(vars);
if vars[i]=vals[i] then hcp_score=hcp_score + sc[i];
end;
drop i;
run;
Note that I just added the multi-value comparison variables to end of the list with the corresponding comparison and score values and adjusted the size of the temporary arrays to match.
One of the drawbacks of this approach is having a mismatched number of values and variables will likely cause the error: Array Subscript out of range
And one or more warnings about "partial array initialization" (not enough values) or "Too many values for initialization of the array".
... View more