Macros don’t do looping or branching. Macros are just a list of commands that run from the start of the list to the end of the list.
For more complex programming (including looping, branching and conditionals) you need to use a programming language such as Nyquist (built into Audacity) or and external language such as Python.
See: Nyquist - Audacity Manual
and: Scripting - Audacity Manual