Add numbers (including missing) without "Sum function" and "If statements"


A contemporary SAS interview question


Recently, a friend shared a SAS interview question that she faced in one of the Interviews she appeared in.
The question might sound very basic initially but when I heard about the constraints, I found it too be quite tricky one. It required to make a little more use of my grey matter.

Sharing, how I approached to the problem .

Let's say we have the data like below :

Data sum_data;
input val1 : 8. val2: 8. val3 : 8.;
cards;
8 . .
9 . 2
10 12 3
13 14 .
;
proc Print;
Run;


Using Data step, a new variable needs to be created which would be the sum of the Val1, Val2 and Val3 and the solution needs to be designed in such a way that there's no use of any SUM function or any IF statement.

So where the complication lies ?


You can not simply used

sum_of_3 =  Val1+ Val2 + Val3, due to presence of missing values and also these missing values can't be dealt without IF statements.

Approach 1, I thought  ( Failed though L )


First, I thought of using global option MISSING =0 . I used it, but failed to see the Output.

Options missing = 0 ;
Data result ;
set sum_data;
sum_of_3 =  Val1+ Val2 + Val3;
Run;

Result was not what was required, but I learned one thing with this. The missing = 0 option actually masks the missing values with 0 value, but keeps the value missing intact.

Run the following code and check yourself :

Options missing = 0 ;
Data result ;
set sum_data;
sum_of_3 =  Val1+ Val2 + Val3;
num_missing = nmiss(Val1, Val2, Val3, sum_of_3);
Run;

Approach 2, Bingo ! It worked well  ( J )




Data result_new;
set sum_data ;
sum_of_3 = n(Val1, Val2, Val3) * mean(Val1, Val2, Val3);
Run;




Approach 3,  Also fine



Data result_new;
set sum_data ;
sum_of_3 = max( Val1,0) + max( Val2,0) + max( Val3,0) ;
Run;



Enjoy reading our other articles and stay tuned with us.

Kindly do provide your feedback in the 'Comments' Section and share as much as possible.





No comments:

Post a Comment

Do provide us your feedback, it would help us serve your better.