Announcement

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

  • Why If Else does not work?

    I have some codes as follows (as you may know this is the result after many previous helps from forum):

    Code:
    clear
    import excel "path_letter.xls", sheet("..") firstrow
    levelsof Letter, local(name_list)
    foreach name of local(name_list) {
    clear
    import excel "path_`name'.xls", sheet("..") cellrange(A5) firstrow
    foreach var of varlist _all {
    local vars "`vars' `var'"
    }
    local first: word 1 of `vars'
    if "`first'" == "ABC" {
    do "path.do"
    }
    else if "`first'" == "DEF" {
    do "path2.do"
    }
    save "path_`name'_2.xls"
    }
    It seems only the first "if" code works. The second part "else" does not work, because when the loop bumps into the condition that "`firsrt'" == "DEF", it still runs as if it is "`firsrt'" == "ABC", then notified error.

    Why does this code not work?

    I am targeting transform data from many excel sheets at the same position in many excel files. They are in 2 forms: some have beginning variable as "ABC" and the remaining as "DEF", and the structures of theirs are different following these different forms too. That's why I must run 2 different do-file for them.

    Many thanks for your help.

  • #2
    "`first'" == "ABC" will be true if and only if the name inside first is in fact ABC. Is that what you want? Note that we can't see your spreadsheet.

    Comment


    • #3
      I think it's correct in that point. that's why the loop can run without problems for a few spreadsheets before stop and notify error (variable "ABC" not found) because it does not take into account the part "else" of the command.

      It is the same when the part "else" is as follows:

      else {
      do "path2.do"
      }
      Last edited by Giang Dao; 31 May 2018, 04:20.

      Comment


      • #4
        I already see my problem. There are some missing codes in the do-file 2 in "path2.do". Many thanks for your time!

        Comment

        Working...
        X