Announcement

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

  • Need help in generating a categorical variable

    Hello,

    I am having trouble generating a categorical variable by the name of Age which is supposed to display categories in this format:
    Age
    5-9 years
    10-14 years
    15-19 years
    20-24 years
    All ages (5-24 years)
    I am able to generate the first 4 categories using a loop however I am unable to understand as to how I can generate the final category "All ages (5-24 years)"

    I used the following command to generate the first 4 categories and was wondering if you can help out with generating the last one within the categorical variable:

    Code:
    local x = 1
    gen Age = 1
    label variable Age "Age in Years"
    forvalue j=5(5)24{
    replace Age =`x' if s1aq05>=`j'
    replace Age = . if (s1aq05<5 | s1aq05>24)
    local ++x
    }
    similarly, how may I add value labels to these categories within the loop?

    Looking forward.
    Last edited by Fahad Mirza; 19 Apr 2019, 01:29.

  • #2
    No need for a loop.

    Code:
    gen Age = 5 * floor(slaq05 / 5) 
    label def Age 5 "5-9" 10 "10-14" 15 "15-19" 20 "20-24" 
    label val Age Age
    See also https://journals.sagepub.com/doi/10....867X1801800311

    Comment


    • #3
      As Nick's code implies, your categorical variable can have only 4 categories. A variable takes exactly one value. The variable you describe would have to take two values. For example, if slaq05 is 16, it would have to take both the value for 15-19 years and for 5-24 years.

      Perhaps if you tell us how you intend to use the variable you describe, we can explain why it is not necessary to include a category for all ages.

      Comment

      Working...
      X