0

In my .do file, I am asking the user some questions about what regressions they want to run, using display _request(macroname). Then I interpret their answer (which is either yes or no in most cases) and define another macro that appears in the regression code. I'm doing this as there are lots of different options of which regression to run and this makes it easy to select which one to run.

Now I want to automate a process, which loops through all the different options and I thought it would be nice to add in an extra question at the start. This will be asking whether the user also doesn't want to bother with answering questions, but to run the 64 combinations (the output of which will save to various appropriately named different Word documents).

So I have the code for the regressions that needs to be run, but it needs to either be run only once after asking five questions, or looped.

Can I save the code to a macro and then stipulate when the code should be run?

Edit: My best solution is to run a separate .do file using the do command. If I need to do this more often, can this be done without running a separate .do file?

********************** Regressions ***********************

* If you haven't yet installed estout on your machine, you need to do that.
 ssc install estout

pause Note that the regressions will be put in $DataOUT


capture window stopbox rusure "DO YOU WANT TO RUN EVERY POSSIBLE REGRESSION?" "This might take about 10 minutes."
if _rc == 0 {
    global cheat_yn = "yes"
}
if _rc == 1 {
    global cheat_yn = "no"
}


if "$cheat_yn" != "yes" {

/* Great! You're now at the end of the .do file! I'm going to ask you to type
in answers to some questions, to determine what variables you want to work
with. Just type in your answer in the command field, and press enter. */

* 1. Please enter a dependent variable out of searches, encouraged, employed or participates
display _request(dep)
* 2. Grant X female recipient interaction? Enter yes or no.
display _request(gXf_yn)
* 3. Grant X matric interaction? Enter yes or no.
display _request(gXmat)
* 4. Define grant independent variable to include 3 or 5 grant types? Enter 3 or 5.
display _request(n_3_5)
* 5. Dummy for grant or income? Enter dummy or income. (Note that the sample with dummy includes households with no grant income whereas the sample with the income variable only includes households with a positive grant income.)
display _request(grantd)
* 6. Separate out grant into different types or not? Enter yes or no.
display _request(separate)

do "$DataIN\running_regressions.do"

}


if "$cheat_yn" == "yes" {
pause off
foreach dependent in employed participates encouraged searches {
global dep = "`dependent'"
foreach f in no yes {
global gXf_yn = "`f'"
foreach d in income dummy {
global grantd = "`d'"
foreach s in no yes {
global separate = "`s'"
foreach n in 3 5 {
global n_3_5 = "`n'"
foreach m in yes no {
global gXmat = "`m'"
if ( "`dependent'" != "searches" & "`dependent'" != "encouraged" ) | "`f'" != "yes" | "`d'" != "dummy" | "`s'" != "yes" {
* The previous line removes a few regressions that don't run properly

do "$DataIN\running_regressions.do"

}
}
}
}
}
}
}

}
ahorn
  • 217
  • 2
  • 12
  • 5
    You could create a program for that and have this run once or looped? It is not very clear what you are asking. –  May 17 '18 at 20:10
  • @PearlySpencer I don't know how to create a program for it. – ahorn May 18 '18 at 08:15
  • Cross-posted at https://www.statalist.org/forums/forum/general-stata-discussion/general/1444721-can-the-user-decide-whether-to-loop-code-or-only-run-it-once – Nick Cox May 18 '18 at 08:28
  • 4
    My suggestion to write a program still stands. See `help program` on how to get started. That said, it is not reasonable to expect anyone to sit down and do this for you as it requires a fair bit of time. Stack Overflow is more geared towards giving targeted solutions to more specific problems. –  May 19 '18 at 19:04
  • @PearlySpencer thank you - I didn't realize you were referring to a command that I could look up in the help documentation. – ahorn May 19 '18 at 19:17

1 Answers1

0

There is a trick using a macro to comment out script lines: (Not as elegant as writing a program ...)

if answer = "yes" local star = ""
else local star = "*"

and then put 'star' first on a script line: If 'star' is empty, the line is run, but if 'star' contains the asterisk, the line is regarded as a comment:

`star' display "hi, there"

would run only if the "answer" was "yes".

If you can build your script so it is possible to comment out the loop start- and end-lines and still have the commands inside the loop functioning, this might help you.

  • 1
    `if answer = "yes"` is not yet legal code Note that you only need to define the local macro when the value is to be `"*"` as if the local macro is undefined that is equivalent to being an empty string and then the command will be executed, which is what you want. – Nick Cox Sep 20 '18 at 13:28