Announcement

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

  • Did Stata 15 Change Loops?

    I just updated to Stata 15 and now a code I consistently use is broken. Have I overlooked something here, or did Stata 15 change loops?

    Here is the code:

    global xlist e1-e8
    foreach var in $xlist {
    recode `var' (1/2=1) (3=2) (4/5=3), gen(`var'_net)

    However, I get this error message:

    variable e1 already defined
    r(110)
    Previously, it would just create new variables for e1-e8 with "_net" at the end of each variable. Now it won't. Any advice?
    Last edited by Emily Ekins; 28 Jan 2019, 07:07. Reason: Fixing formatting

  • #2
    Try replacing the first line of the loop by

    Code:
    foreach var of varlist $xlist {

    Comment


    • #3
      I think I've resolved this issue. Changing the code to this worked:

      PREVIOUS: global xlist e1-e8
      NEW: global xlist2 e1 e2 e3 e4 e5 e6 e7 e8

      Comment


      • #4
        It seems there is a space between e1 and -e8. It should be easier to spot with - dataex - under code delimiters.
        Best regards,

        Marcos

        Comment


        • #5
          Originally posted by Emily Ekins View Post
          I think I've resolved this issue. Changing the code to this worked:

          PREVIOUS: global xlist e1-e8
          NEW: global xlist2 e1 e2 e3 e4 e5 e6 e7 e8
          What you have done should resolve the problem, and what I showed in #2 should resolve the problem too.

          I think the problem is that one cannot use wildcards on variables in the list part of the loop statement.

          Code:
          foreach var in list {
          ...
          }

          Comment


          • #6
            Originally posted by Joro Kolev View Post
            I think the problem is that one cannot use wildcards on variables in the list part of the loop statement.

            Code:
            foreach var in list {
            ...
            }
            That is correct. And, just for the record, nothing in this regard has changed in Stata 15 compared (down)to (at least) Stata 10 (and probably since foreach was introduced in Stata 8 or so).

            Best
            Daniel

            Comment


            • #7
              daniel klein , I know that what I said has to be correct as you confirmed, because here on Statalist somebody was trying to do something for all variables in the dataset, and my first attempt to solve his/her problem by -foreach var in * {}- did not work. Then I replaced this with -foreach var of varlist * {}- and this worked... So far so good.

              The problem is that now I have been toying around, and I have been unable to replicate the problem of the Original Poster in this thread, and the problem which I clearly remember how I encountered in another thread and how I resolved. Here:

              Code:
              . clear
              
              . set obs 10
              number of observations (_N) was 0, now 10
              
              . forvalues i =1/3 {
                2. gen var`i' = runiform()
                3. }
              
              . foreach x in var1-var3 {
                2. summ `var'
                3. }
              
                  Variable |        Obs        Mean    Std. Dev.       Min        Max
              -------------+---------------------------------------------------------
                      var1 |         10    .4091037    .2741222    .109727   .9731625
                      var2 |         10    .4416201    .2608051   .1269047    .870596
                      var3 |         10    .3579504    .3263697    .008862   .9540287
              
              . foreach x in * {
                2. summ `var'
                3. }
              
                  Variable |        Obs        Mean    Std. Dev.       Min        Max
              -------------+---------------------------------------------------------
                      var1 |         10    .4091037    .2741222    .109727   .9731625
                      var2 |         10    .4416201    .2608051   .1269047    .870596
                      var3 |         10    .3579504    .3263697    .008862   .9540287
              
              .
              I do not know what is going on here. For some reason in my toy example Stata accepted the wildcards....

              Comment


              • #8
                Joro, let me try and explain.

                When you write

                Code:
                foreach x in var1-var3 {
                    summ `var'
                }
                foreach interprets var1-var3 as one token (defined by white space, optionally bound together by quotes) and the only element in the list. Now, to foreach, var1-var3 is just a string and `var' inside the loop just evaluates to this string [that is not so; see Edit, below]. However, to summarize, var1-var3 is a valid varlist and interpreted as such.

                The explanation for the second example is exactly the same. To foreach, * is just one character (and the entire list). To summarize, * means all variables in the dataset and it acts accordingly.

                Edit:

                Your code is wrong but my explanation still (partly) holds. My explanation holds exactly had you typed

                Code:
                foreach var in ...
                However, look carefully and you will notice that you did type

                Code:
                foreach x ...
                yet, you never refer to `x' inside the loop. How do we explain the result? Simple. To foreach it does not matter whether you ever refer to the defined local; it just iterates through the supplied list. The summarize command is perfectly happy to see `var' to evaluate to "", that is, missing string. If you do not feed summarize a variable list, it defaults to all variables in the dataset, which in your case are only var1 var2, and var3. Hence, both loops produce the exact same result for the exact same reason. The results will still match if you fix your error, but the reasons will be slightly different.

                Best
                Daniel
                Last edited by daniel klein; 28 Jan 2019, 09:58.

                Comment


                • #9
                  Thank you daniel klein for dissecting this teachable moment and demystifying it. What just happened here are the most tricky situations, when one does a stupidity, and the stupidity goes through with flying colours. It is a lot easier when one tries something stupid, Stata rejects it, so one has to come back and think things through.

                  The stupidity of mine in loops 2 and 3, that I was looping through values of local `x', but I was referring inside the loop to the non-existent local `var', led to something very positive. I learnt something new today, that referring to non-existent macros does not lead Stata to terminate the process and return an error (as it would happen with referring to non-existent variables, scalars or matrices). Referring to non-existent macros simply leads to outcome of the macro evaluation being "missing"...

                  The other error of mine was to misinterpret the output as generated by a loop through 3 elements, when in fact the loop was through 1 element and -summ- was parsing singlehandedly the 3 elements.



                  Comment

                  Working...
                  X