It is hard to identify a general problem within your example that requires a program.
Also, when you ask about a Stata program, a Stata programmer tends to imagine that you expect to write program
and that way to define a new command, but it's not at all obvious that you really need to write a new command here.
The example I take to be that regress
and test
could be run repeatedly for different variables.
But some details are obscure. What is Y
? Or is it just a way of indicating "Yes"? If you were to run this interactively, the use of local macros would be futile, as you would just be putting a name into a local macro and taking it out again.
If you do write a program, then you need to document it with a help file to explain what it does, or minimally otherwise. For what purpose? So, that a user (you or someone else) can repeat two commands with different details? The gain there is not clear.
As far I understand your problem, the main need is not a program at all, but
To put repeated commands in a do-file and allow that do-file to take arguments. That's a much underestimated possibility. See the [U] manual, 16.4
To learn about looping over variables with a
foreach
loop.
Note: "local variables" is not standard Stata terminology; they would always be called "local macros". In Stata a variable is a column in the dataset.