Announcement

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

  • Collapse string variable to achieve a "count" within a postcode

    I have 1 string variable with 7913 observations. These are UK postcodes of either "AB23 8BB" or "AB1 1AA" format.

    These have been generated from individual level data and I now need to get a count of people within each postcode. I have tried to use the collapse command but keep receiving a "type mismatch" error. I am using Stata 14.2.

    Many thanks.

  • #2
    No precise code to comment on here. FAQ Advice #12 applies. But by default collapse takes means, which may be your problem

    At worst something like

    Code:
    gen one = 1 
    collapse (count) one, by(postcode)
    should work if you want a reduced dataset. Or see contract instead. Or consider

    Code:
    bysort postcode : gen freq = _N
    for a variable alongside your existing dataset.

    Comment


    • #3
      Apologies for the lack of code.

      Many thanks for that Nick - the best fit for my needs was:
      gen one = 1 collapse (count) one, by(postcode)
      Last edited by Kath Machray; 13 Jun 2017, 09:36. Reason: To close thread properly

      Comment


      • #4
        Hi nick,

        It seems strange to me that count does not count string variable. I don't really understand the rational behind it. I think this should be fixed.

        Best

        Comment

        Working...
        X