Announcement

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

  • Grouping observations

    Hello community this my first post.

    I'm a SAS user, new to STATA, require it for my graduate program. I may not use the terminology, but here goes my question:

    Example dataset

    ID employees
    1 205
    2 5
    3 14
    4 62
    5 0

    I would like to group employees count into categorical
    if a firm has less than 5 employees =1
    employee count from 6-100 firm_size = 2
    employee count above 100=3

    Thank you in advance


  • #2
    The grouping you specified in #1 fails to provide any classification for exactly 5 employees. In the code below, I assume that you meant "if a firm has less than 6 employees = 1."

    Code:
    gen emp_count_group = 1 if employees < 6
    replace emp_count_group = 2 if inrange(employees, 6, 100)
    replace emp_count_group = 3 if employees > 100 & !missing(employees)
    In the future, when showing data examples, please use the -dataex- command to do so. If you are running version 15.1 or a fully updated version 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    When asking for help with code, always show example data. When showing example data, always use -dataex-.

    Comment


    • #3
      gen size = 1 if !missing(employees)
      replace size = 2 if employees>5 & employees<101
      replace size = 3 if employees>100 & !missing(employees)

      Comment


      • #4
        Thank you Clyde and Joro - problem solved

        Comment


        • #5
          Make it 1 line.
          Code:
          gen wanted = (employees>0)+(employees>5)+(employees>100) if employees <.

          Comment

          Working...
          X