Announcement

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

  • Problem with if-command in foreach loop

    Stata returns an error when I integrate an if-command within a loop. I hope the answer to this question is not too obvious - as I don't see the mistake (I fear it is a typo...) - but it is my first time I try to produce some more generic do-files for the same task.

    I do have the following code:

    Code:
    foreach l in pro pub qual koop preis vera {
    
    *** Some more if-commands that work well
    *** The problematic part when the loop is at pub
    
    if "`l'" == "pro" | "`l'" == "qual" | "`l'" == "koop" | "`l'" == "vera" {
    if "`l'" == "pro" | "`l'" == "koop" {
    local beginn ``l'_db'_beginn
    local ende ``l'_db'_ende
    }
    else "`l'" == "qual" {
    local beginn ``l'_db'_beginn
    local ende ``l'_db'_pruefdat
    }
    else "`l'" == "vera" {
    local beginn ``l'_db'_start
    local ende ``l'_db'_ende
    }
    
    ***Some more code
    }
    With set trace on I get:
    - if "`l'" == "pro" | "`l'" == "qual" | "`l'" == "koop" | "`l'" == "vera" {
    = if "pub" == "pro" | "pub" == "qual" | "pub" == "koop" | "pub" == "vera" {
    if "`l'" == "pro" | "`l'" == "koop" {
    local beginn ``l'_db'_beginn
    local ende ``l'_db'_ende
    }
    else "`l'" == "qual" {
    local beginn ``l'_db'_beginn
    local ende ``l'_db'_pruefdat
    }
    - else "`l'" == "vera" {
    = else "pub" == "vera" {
    "pub is not a valid command name
    local beginn ``l'_db'_start
    local ende ``l'_db'_ende
    }
    r(199);

    end of do-file

    r(199);

    Your help is very much appreciated.
    Last edited by Marc Kaulisch; 30 Apr 2017, 12:28.

  • #2
    -else- followed directly by a condition is a syntax error. You can have -else if condition-, or just -else- by itself, but not -else condition-. So when you have branching logic, the first level is -if whatever-, then the next levels are -else if whatever-. The final level, if it applies to everything that was not handled by -if- or -else if- can be just -else- by itself, with no condition.

    So in your example, every place you have -else- it should be -else if-.

    In the future, when posting Stata code and output, place it between code delimiters. (See FAQ #12 for instructions if you don't know how to do this.) That makes the output much more readable.

    Comment


    • #3
      All your elses seem to need a following if as only then are you following else by a command (or block of commands). In fact Stata is telling you in the error message that it can't see a command name when it expects one.

      With some other simplifications your code could be

      Code:
      if inlist("`l'", "pro", "qual", "koop", "vera")  {
          if inlist("`l'", "pro", "koop") {
              local beginn ``l'_db'_beginn 
              local ende ``l'_db'_ende
          }
          else if "`l'" == "qual" { 
              local beginn ``l'_db'_beginn
              local ende ``l'_db'_pruefdat
          }
          else {
              local beginn ``l'_db'_start
              local ende ``l'_db'_ende
          }
      }
      Please note our longstanding request to present code as such using CODE delimiters.

      Comment


      • #4
        Clyde Schechter Nick Cox : Thanks a lot for your help. This is such an embarrassing mistake. I don't now how many times I looked at the -help ifcmd- page and haven't seen the most obvious... And i am still learning the new Forum functions - thanks for pointing me to it - I corrected it

        I looked again at -help ifcmd- and I think I got caught by this part where I git the impression that else is enough althoug example 2 clealy states it differently:

        "
        if exp { or if exp single_command
        multiple_commands
        }

        which, in either case, may be followed by

        else { or else single_command
        multiple_commands
        }
        "
        Last edited by Marc Kaulisch; 30 Apr 2017, 12:44. Reason: adding a short justification

        Comment

        Working...
        X