Announcement

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

  • How to avoid losing variable labels when using collapse command

    I have smallholder agriculture plot level commercialization data which i want to collapse to household level data. To avoid losing variable labels i resorted to the foreach loop method below by Cox;

    Copy variable labels before collapse . foreach v of var * { . local l`v' : variable label `v' . if `"`l`v''"' == "" { . local l`v' "`v'" . } . } Attach the saved labels after collapse​​​​​​​ . foreach v of var * { . label var `v' "`l`v''" . }
    Whenever i try running this codes, i am getting the error message:

    "foreach command may not result from a macro expansion interactively or in do files"


    What could be the reason? Thank you in advance

    ​​​​​​​Takesure Tozooneyi

  • #2
    Please read the Forum FAQ, with emphasis on #12 to learn the best way to post code, using code delimiters.

    The code you have posted does not, presumably, convey the actual layout of the code you ran. In this instance, the exact layout of the code you ran is likely to be the source of your error message, but given that nobody can see that, it is impossible to be sure.

    Please repost showing the exact code you ran (i.e. copy/paste it directly from your do-file, Results window, or log file with no editing whatsoever) and put it between code delimiters. I am confident that you will get a concrete and useful response if you do that.

    Comment


    • #3
      I'm less optimistic than Clyde that seeing the actual code copied from the to-file will help. The example below shows that this code will work. Instead, you have done something else that is causing this error.

      See the example below, the run your commands and copy from Stata's results window everything starting at the first loop and ending at the error message, and paste that into your next post using code delimiters [CODE] and [/CODE].
      Code:
      cls
      sysuse auto, clear
      keep foreign price mpg
      
      foreach v of var * { 
          local l`v' : variable label `v' 
          if `"`l`v''"' == "" { 
              local l`v' "`v'" 
          } 
      } 
      
      collapse price mpg, by(foreign)
      
      foreach v of var * { 
          label var `v' "`l`v''" 
      } 
      
      list
      describe *
      Code:
      . sysuse auto, clear
      (1978 Automobile Data)
      
      . keep foreign price mpg
      
      . 
      . foreach v of var * { 
        2.         local l`v' : variable label `v' 
        3.     if `"`l`v''"' == "" { 
        4.             local l`v' "`v'" 
        5.     } 
        6. } 
      
      . 
      . collapse price mpg, by(foreign)
      
      . 
      . foreach v of var * { 
        2.     label var `v' "`l`v''" 
        3. } 
      
      . 
      . list
      
           +------------------------------+
           |  foreign     price       mpg |
           |------------------------------|
        1. | Domestic   6,072.4   19.8269 |
        2. |  Foreign   6,384.7   24.7727 |
           +------------------------------+
      
      . describe *
      
                    storage   display    value
      variable name   type    format     label      variable label
      ------------------------------------------------------------------------------------------------
      foreign         byte    %8.0g      origin     Car type
      price           float   %8.0gc                Price
      mpg             float   %8.0g                 Mileage (mpg)

      Comment


      • #4
        Thank you guys for the help. I successfully executed it this time around.

        Comment

        Working...
        X