I think you misunderstand how keyboard-macros work. What @Trey provided is a Emacs-Lisp command. You could have accomplished this for yourself without understanding Emacs-Lisp.
First figure out the sequence of keys that does what you want and then record that sequence as a macro.
You proposed this: C-a M-; (M-; is comment-dwim). Does it do what you had in mind? If not then it's not going to magically work when you play it back as a keyboard macro.