Announcement

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

  • Labelsof & Macros

    I have a dataset similar to census.dta. I'd like to save the variable labels (for, in this case, region) to a local and then use them in a loop. I know with this example, it's quicker to do something else, like bys region: summarize pop or tabstat pop, by(region)but I'm just using a simple example to try to figure this problem out.

    I can't get the code below to work - I keep getting this error: invalid '"N Cntrl'.

    Code:
    sysuse census
    labelsof region
    local regionlab = `r(labels)'
    
    foreach r of local regionlab {
     keep if region == `r'
     summarize pop
    }
    Any ideas or advice would be so helpful.

    Thanks,
    Erika

  • #2
    I think you need double quote marks around `r' as in:
    Code:
     
     keep if region == "`r'"

    Comment


    • #3
      Thanks for the response. I should have noted that the error occurs before keep; Stata stops at the line local regionlab = `r(labels)'.

      Comment


      • #4
        Try this code, Erika. Is its output what you want?
        Code:
        sysuse census, clear
        levelsof region
        local regionlab  `r(levels)'
        foreach r of local regionlab {
         summarize pop if region==`r'
        }

        Comment


        • #5
          I did some experimenting and I think it's a problem in the way the contents of r(labels) is quoted, as shown by return list. Perhaps someone else will take your code and add return list following the labelsof command and explain to us what syntax is needed to make the results usable in a loop such as yours.

          Beyond that, though, your sample code is probably not doing what you want it to. First, your code compares the values of your variable to the labels for that variable's values. Those are not the same and nothing will compare. Even if the comparison did what you want, the first time through the loop, Stata will drop all the observations which are not in the first region; the second time through, the observations not in the second region will be dropped, and since the only observations remaining from the first time through are those in the first region, nothing will remain.

          This will do what your code intended.
          Code:
          sysuse census, clear
          levelsof region, local(regions)
          foreach r of local regions {
           summarize pop if region==`r'
          }
          If your goal is something more complicated that actually requires dropping observations, something like the following might work.
          Code:
          sysuse census, clear
          levelsof region, local(regions)
          foreach r of local regions {
           preserve
           keep if region==`r'
           summarize pop
           restore
          }

          Comment


          • #6
            Thanks so much for this information. I was trying to avoid using levelsof because it strips out the variable labels. I was hoping to find a way to keep the labels so I could write them to a file along with other output.

            sysuse census labelsof region local regionlab = `r(labels)' foreach r of local regionlab { keep if region == `r' summarize pop file write myfile `r' } Any ideas about how to do that? Is there some way of referring to a numeric variable using its label in an "if" statement?

            Erika

            Comment


            • #7
              The following code avoids the problem with labelsof by using a combination of levelsof and macro extended functions. You should be able to use it as a starting point to accomplish what you want.
              Code:
              sysuse census, clear
              levelsof region, local(regions)
              local lblname : value label region
              foreach r of local regions {
               preserve
               keep if region==`r'
               local lbl : label `lblname' `r' 
               summarize pop
               display `"the macro r has the value "`r'" "'
               display `"the macro lbl has the value "`lbl'" "'
               restore
              }

              Comment


              • #8
                Originally posted by Erika Kociolek View Post
                Is there some way of referring to a numeric variable using its label in an "if" statement?
                Did not read the hole thing here, but yes, there is. Here is an example with the auto dataset

                Code:
                sysuse auto , clear
                summarize price if foreign == "Foreign":origin
                The relevant reference is Higbee (2004).

                [Edit]
                By the way, labelsof (Jann, SSC) saves in r(values) the numeric values, so these are available, too.
                [/Edit]

                Best
                Daniel

                Higbee, Kenneth (2004). Stata tip 14: Using value labels in expressions. [i]The Stata Journal[i], 4(4): 486–487.
                Last edited by daniel klein; 17 Sep 2015, 23:36.

                Comment

                Working...
                X