Announcement

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

  • Creating age-range variable

    Hello!
    I would like to create a new age-range variable, which will be a breakdown of already existing age-range variable: at the moment I have age ranges of 0 to 24, 25 to 39, 40 to 49, and 50 to 59 for every country in the world. I would like to keep this one and also create a new variable which will have following age-ranges: <1, 1 to 4, 5 to 9, 10 to 14, 15 to 19... etc (with 4 year intervals) for every country. I understand there will need to be duplicates for already existing age ranges. I am having a hard time with coming up with a code, and would appreciate some help! Thank you!

  • #2
    I'm going to assume that you have an age variable in the data set (the actual age, not the 0-24, 25-39, etc. age group) to work from.

    Code:
    gen byte age_range = 0 if age < 1
    replace age_range = 1 + floor(age/5) if age > 1
    summ age_range, meanonly
    local max = r(max)
    label define age_range 0    "< 1"  1   "1 to4"
    forvalues i = 2/`max' {
        label define age_range `i' "`=5*`i'-5' to `=5*`i'-1'", add
    }
    label values age_range age_range
    I understand there will need to be duplicates for already existing age ranges.
    I don't understand this comment.

    In the future, when showing data examples, please use the -dataex- command to do so. If you are running version 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.

    When asking for help with code, always show example data. When showing example data, always use -dataex-.
    Last edited by Clyde Schechter; 02 Feb 2023, 18:14.

    Comment


    • #3
      Originally posted by Clyde Schechter View Post
      I'm going to assume that you have an age variable in the data set (the actual age, not the 0-24, 25-39, etc. age group) to work from.

      Code:
      gen byte age_range = 0 if age < 1
      replace age_range = 1 + floor(age/5) if age > 1
      summ age_range, meanonly
      local max = r(max)
      label define age_range 0 "< 1" 1 "1 to4"
      forvalues i = 2/`max' {
      label define age_range `i' "`=5*`i'-5' to `=5*`i'-1'", add
      }
      label values age_range age_range

      I don't understand this comment.

      In the future, when showing data examples, please use the -dataex- command to do so. If you are running version 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.

      When asking for help with code, always show example data. When showing example data, always use -dataex-.
      Noted! Thank you for the code!

      Comment

      Working...
      X