Announcement

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

  • How to make age groups ?

    Hello, i need help in creating age groups. My variable name is Age and i have almost 10000 cases. I want to categorize them into these age groups, 0-9, 10-19, 20-29, 30-39, 40-49, 50-59, and 60+

    Another question is, i have a variable RStatus and it has 6 categories i.e. pink, red, blue, yellow, black. I want RStatus for 60+ but only black. pink, red, blue, yellow i dont need for 60+. But i do need those for rest of the age categories. In excel i can easily do this by filtering and deleting those. In stata i dont know how to do this.

    Click image for larger version

Name:	Untitled.jpg
Views:	1
Size:	46.1 KB
ID:	1713488

  • #2
    Code:
    forvalues i = 0/5 {
        label define age_group    `i' `"`=10*`i'' - `=10*`i' + 9'"', add
    }
    label define age_group 6 "60 +", add
    
    gen int age_group:age_group = min(floor(Age/10), 6)
    will get you the age groups. I don't really understand your other question. At a guess, perhaps you want to do this:
    Code:
    drop if age_group == "60 +":age_group & RStatus != "black"
    Note: both of these solutions rely on assumptions about your data that cannot be verified from a screenshot. In particular, I assume that Age is a numeric variable and RStatus is string. But neither of those is necessarily the case. If I have guessed wrong, then I have wasted my time writing this code, and you will have wasted yours reading this response. To avoid guesswork in the future and make the most of your Statalist experience, please read the Forum FAQ for helpful guidance about how to effectively provide information in your posts. In particular, screenshots are strongly discouraged--they are very close to useless. The helpful way to show example data is by using the -dataex- command. If you are running version 18, 17, 16 or a fully updated version 15.1 or 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.

    Comment


    • #3
      See also https://journals.sagepub.com/doi/pdf...867X1801800311 for a basic tutorial review of rounding and binning, which (among other things) flags how useful floor() and ceil() functions can be.
      Last edited by Nick Cox; 13 May 2023, 03:51.

      Comment


      • #4
        @ thank you Cylde, your age group code worked. Thank you. Your second code works too. But I need to add more conditions to make it work.

        @ nick, thank you for your prompt response. I have used your answers to other questions to find solutions. I will go through your given document as well. Much appreciated both of you.

        Comment

        Working...
        X