I need to compute the mode for different variables for the same id using SAS. The original dataset is data df_a;
input id obs e1 e2 e3 b0 b1 b2 b3 b4 b5;
datalines;
2 2 1 2 3 600 0 200 0 530 550
2 1 1 2 3 600 0 200 0 530 550
3 3 2 2 3 150 500 . 120 3 58
3 2 2 2 3 150 500 . 120 3 58
3 1 2 2 1 150 500 . 120 3 58
4 3 1 2 3 200 300 550 270 0 50
4 2 1 2 3 200 300 550 270 0 50
4 1 1 2 3 200 300 550 270 0 50
5 1 1 2 . 20 20 200 100 500 57
5 2 1 2 . 20 20 200 100 500 57
5 3 1 2 . 20 20 200 100 500 57
run; The desired output is id obs e1 e2 e3 mode_e b0 b1 b2 b3 b4 b5 mode_b
2 2 1 2 3 random choice 600 0 200 0 530 550 0
2 1 1 2 3 random choice 600 0 200 0 530 550 0
3 3 2 2 3 2 150 500 . 120 3 58 random choice
3 2 2 2 3 2 150 500 . 120 3 58 random choice
3 1 2 2 1 2 150 500 . 120 3 58 random choice
4 3 1 2 3 random choice 200 300 550 270 0 50 random choice
4 2 1 2 3 random choice 200 300 550 270 0 50 random choice
4 1 1 2 3 random choice 200 300 550 270 0 50 random choice
5 1 1 2 . random choice 20 20 200 100 500 57 20
5 2 1 2 . random choice 20 20 200 100 500 57 20
5 3 1 2 . random choice 20 20 200 100 500 57 20 Points to consider i) I need the mode for variables e1-e3 and b0-b5 in a new column (mode_e, mode_b). iii) If multimode, select one randomly. iv) keep all the variables in the final output. v) The code must be able to compute this for large database I have tried to use the code proposed here https://communities.sas.com/t5/Base-SAS-Programming/Calculating-Mode-across-variables/td-p/150375 but it does not give the desired output.
... View more