Announcement

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

  • Make an exception within a loop

    I'm using version 15 of stata, and I'm wondering if it is possible to make an exception within a loop. I have four variables which I currently have running through a loop. However, I've just realized that I need to pull in data from a different excel sheet for just one of the variables. Is it possible to, within the existing loop, create a command that will allow me to merge the data from this other excel sheet and use it just in the calculation of the one variable?

  • #2
    A merge operation, in the Stata sense of -merge-, would not likely be something you would do in the midst of a loop, so my best guess here is that there is a difficulty of communication. Per the FAQ, I'd suggest you use -dataex- to show an example of your data so as to clarify your situation.

    Comment


    • #3
      I would just have the loop skip that value, and do the merge for that variable outside the loop. So, if I were creating 15 variables, but needed to do something different when creating variable 8, I would do:

      Code:
      foreach i of numlist 1/7 9/15 {
      [create your variables here]
      }
      
      //  Do the merge for variable 8

      Comment


      • #4
        An approach I use occasionally is the programmer's if (see help ifcmd). Here is a silly illustration where I exclude a particular variable, which is why that line uses quotation marks, but you could also use it to exclude numeric values (like the previous post) or other characteristics.
        Code:
        sysuse auto, clear
        foreach v of varlist * {
            if "`v'" != "headroom" summarize `v'
            }
        David Radwin
        Senior Researcher, California Competes
        californiacompetes.org
        Pronouns: He/Him

        Comment


        • #5
          Hello, Interesting subject and I have a question.
          I’m having a problem with a loop
          I want to do a loop in a folder, I have 10 .dat but 9 of them has problems with numeric variable storage like string, and the last it's ok. So I can't figure out what missing in my code to fix it, using command destring for 9 datasets and avoid the one that is good, please somebody could help me?
          Thank you


          Code:
          local i : dir "D:\buenos_collapse" files "!=gruposalimentarios2013.dta"
          foreach file in `i'  {
          use `file', clear
          destring folio, replace
          save `file', replace
          }

          Comment


          • #6
            You can add -capture- to suppress any errors, see

            Code:
            help capture
            Code:
            local i : dir "D:\buenos_collapse" files "!=gruposalimentarios2013.dta"
            foreach file in `i'  {
            use `file', clear
            cap destring folio, replace
            save `file', replace
            }

            Added in Edit: However, it appears that destringing an already numeric variable does not terminate with an error code. Therefore, you need to explain what

            has problems with numeric variable storage like string,
            exactly means. If your variables have non-numeric characters, destring will not proceed. You need to find out why the non-numeric characters exist in the first place. Otherwise, you can specify the -force- option, but only after you are certain that the non-numeric characters in the number strings can safely be ignored (i.e., converted to missing values).

            Code:
            destring folio, replace force
            Last edited by Andrew Musau; 11 Nov 2021, 16:06.

            Comment


            • #7
              Thank you Andrew Musau but still doesn't work.
              folio is stored type str11 format %11s (I don't know why) and looks like 10040010021 and so on, no have abc characters only numbers.
              Wen, I apply "destring folio, replace" in Command window work perfectly, but in my loop doesn't work.
              Maybe another option is included in the loop an exception if folio is number the loop ignores destring, otherwise make the change.
              But I don't know how to do it.

              Comment


              • #8
                If you are not getting an error that directly relates to destring (#7 implies no error), the issue may be that your local is empty. Try:

                Code:
                local i : dir "D:\buenos_collapse" files "*.dta"
                foreach file in `i'{
                    if "`file'"!= "gruposalimentarios2013.dta"{
                        use `file', clear
                        cap destring folio, replace
                        save `file', replace
                     }
                }

                Comment


                • #9
                  Dear Andrew Musau Thank you, now run perfect!

                  Comment

                  Working...
                  X