![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj7NjekhHYaTKnzEAyVxboLyE6zj5OGqUtr4o-swwi7hwCPWtvm2p1vtUWDvsgv41MkG7aFL8173CVVxUHq42b6sQQSxP15vtzK408MjLsDRZjvowIMTBxsLbFUbugrxltnLWroJUPgrPI/s200/SAS_Blog.png)
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;
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjxCEGQvqoYZepiM3n368XXdpY3LTSAT9isjOqTBe35imyVNV56wcUYtFitca_5it9eBh0JfEVvw1uQg92KES-KmHAThB3qcNQVYyM3fsfHvarpxzr8wAy4hkGb2Uxx71R7aLORPQwJBJs/s1600/puzzle+of+sum.jpg)
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 ;
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 )
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjZ0kjsOHYlTgMCHBT6RbamVqS9LV45L8QB9iwuGHfP055B5Gj2hS_ConjnKpDpkEzhF80XjPIABD9ArMLgwkKdyL0CJJcZlqmdQ1_FhNzVB_oD-ZTscU9yki6ivzbWh2NfKcpx5w7HA4c/s320/puzzle+of+sum2.jpg)
Data result_new;
set sum_data ;
sum_of_3 = n(Val1, Val2, Val3) * mean(Val1, Val2, Val3);
Run;
Approach 3, Also fine
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjZ0kjsOHYlTgMCHBT6RbamVqS9LV45L8QB9iwuGHfP055B5Gj2hS_ConjnKpDpkEzhF80XjPIABD9ArMLgwkKdyL0CJJcZlqmdQ1_FhNzVB_oD-ZTscU9yki6ivzbWh2NfKcpx5w7HA4c/s320/puzzle+of+sum2.jpg)
Data result_new;
set sum_data ;
sum_of_3 = max( Val1,0) + max( Val2,0) + max( Val3,0) ;
Run;
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.