Here’s a couple of my public macros. You might get an idea or two from them. Also, here’s a link to some gcode definitions that address variables.
Gcode Definitions