Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • How not to do the post command in each loop

    I have a loop in which I run a few t-tests. I would like to post the output of these t-tests using the post command. However, not in every loop is the t-test performed. In some loops there is no data to do a t-test. The problem is that when post hits inputs such as "meanbuy" where there is no local variable defined, the code stops. I have tried adding the command capture to the post command but it didn't work. I also tried to the If {exp} command but it seems it kept going to it even when the if did not hold. Below is the code.

    HTML Code:
    levelsof index, local(levels)
    foreach l of local levels{
    di "`l'"
    sum ret if index == "`l'", meanonly
    gen mean_ret = r(mean) capture ttest ret = mean_ret if hold_`v' == 1 & check != 0 & index == "`l'"
    local meanbuy_t = r(t) 
    capture ttest ret = mean_ret if hold_`v' == 0 & check != 0 & index == "`l'"
    local meansell_t = r(t)
    capture ttest ret if check != 0 & index == "`l'", by(hold_`v')
    local meanbuy = r(mu_2)
    local meansell = r(mu_1)
    local buysell = meanbuy - meansell
    local buysell_t = r(t)
    local sd_b = r(sd_2)
    local sd_s = r(sd_1)
    local n_b = r(N_2)
    local n_s = r(N_1)
    
    post `table' ("`l'") ("`v'") (`meanbuy') (`meanbuy_t') (`meansell') (`meansell_t') (`buysell') (`buysell_t') (`sd_b') (`sd_s') (`n_b') (`sd_s')
    In essence, I would like to run the code even if the t-test has not been run. I do not need though to record the times where a combination of "l" and "v" did not have a t-test output so I don't mind skipping the post line when there is no t-test output. Appreciate your help and hope I was clear in describing my problem.

  • #2
    You do not have any if commands here, only the if qualifier. these are not the same. you can read more here:
    http://www.stata.com/support/faqs/pr...-if-qualifier/
    http://www.statalist.org/forums/foru...ming-languages

    Also if you could post a code that works on one of the example datasets or dataex (see FAQ) and what you get NOW vs. what you WANT to get would be very helpful, because I didn't really understand what it is your'e trying to achieve.

    Comment


    • #3
      In a loop you need to add "capture noisily:" to get an output. "capture" will suppress the output. However, this is prone to errors and this would only be my second choice.

      As suggested by Ariel you should try the programming "if" condition. I cannot test it because I don't have your data but something like this should work (assuming that if `meanbuy' takes a value all other necessary locals do as well; if this is not the case you need to add additional conditions with "&"):

      Code:
      if "`meanbuy'"!="" {
      post `table' ("`l'") ("`v'") (`meanbuy') (`meanbuy_t') (`meansell') (`meansell_t') (`buysell') (`buysell_t') (`sd_b') (`sd_s') (`n_b') (`sd_s)
      }
      Note that you need to drop the locals beforehand to make sure that no values are taken from a previous round (e.g. add cap: local drop meanbuy before creating the new local).


      Comment

      Working...
      X