Announcement

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

  • Foreach loop

    Hi,

    I'm new to Stata, so I'm still struggling with even the easier concepts of Stata. At this point I'm really just learning commands but I know that my original method is probably unnecessarily long.

    I've imported data from a .csv document and somehow it puts nearly all my data in as string variables. Besides that, this particular survey is (partly) coded reversed. So in my orignal code, the five items that should form a scale have to be generated into a numeric variable, which in turn has to be reverse coded into a new variable so that I can run the reliability tests etc. etc. (7 point likert)

    My current method:
    gen wrsmu_1 = real(q9_1)
    gen wrsmu_2 = real(q9_2)
    gen wrsmu_3 = real(q9_3)
    gen wrsmu_4 = real(q9_4)
    gen wrsmu_5 = real(q9_5)
    gen wrsmu_1n=8-wrsmu_1
    gen wrsmu_2n=8-wrsmu_2
    gen wrsmu_3n=8-wrsmu_3
    gen wrsmu_4n=8-wrsmu_4
    gen wrsmu_5n=8-wrsmu_5


    This is my first attempt at foreach loops, so perhaps I'm doing something very obviously wrong, but when looking at help files on stata.com this is what I came up with (and generates code r(198) invalid name). My guess is that it can't generate a new name since 'var'b isn't a known command, but how do I solve this?

    foreach var in varlist q9* {
    gen 'var'b=8-real('var')
    }

    Sander

  • #2
    It looks as if you may be using the wrong quotation marks. But more obviously it should be of varlist

    Code:
    foreach var of varlist q9* {
         gen `var'b = 8 - real(`var') 
    }
    in varlist q9* is legal, but first time around the loop, Stata will take the first element varlist and attempt

    Code:
     
    gen varlistb = 8 - real(varlist)
    which is not what you want and will fall over if you have no variable or scalar literally called varlist. In other words, with in what follows is taken literally.

    Comment


    • #3
      Thanks a lot for the explanation, it works great now! But is there a rule when to use ' and when to use `? I didnt really pay attention to the difference since I thought there was no difference between them.

      Also, is it possible to generate your own variable name in that code? So lets say I want to call the created variables 'happy' instead of the original name + b? Is that possible, and if so: how?
      Last edited by Sander Spoelstra; 10 Feb 2015, 13:26.

      Comment


      • #4
        The quotation marks must differ, as macro references may be nested and we need to know where macro references begin and end. This is documented in many places, e.g.

        Code:
        help quotes
        and the manual sections it references.

        Clearly variable naming is at choice within Stata's rules. Thus no two variables may have the same name, and so forth. I am not clear on what variation on happy you have in mind. You need to change the new variable name just after generate.

        Comment


        • #5
          Again thanks! I'll get into the documentation some more to figure out how to make some variations on this etc. But this help already diminished the amount of lines I have in my .do file.

          Comment

          Working...
          X