Announcement

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

  • Help on labeling categorical variables

    Hello

    I'm working with my data file but find difficulty on labelling derived categorical variables.
    In my data set, there are more than 100 crude variables and they range from 1 to 28.
    And crudecat variables are derived from crude variables.
    Then, according to crude variables, I want to label my variables as following rules:
    crude variables crudecat variables
    if crude variables=1 1
    if crude variables=2 2
    if crude variables=3 3
    if crude variables>3 4+
    It's easy to do that at first, I type:
    gen crudecat=.
    replace crudecat=1 if crude==1
    replace crudecat=2 if crude==2
    ……

    But when I entered "replace crrudecat=4+ if crude>3 & crude<3", it didn't work. And I also noticed that "gen" command only create numerical variables rather than categorical variables that I want.

    So I tried to use "label" command, I typed:
    label define crudecat 1 "1" 2 "2" 3 "3" >3 "4+"

    or

    label define crudecat 1 "1" 2 "2" 3 "3" 3/99 "4+" (Because the range from crude variables are 1 to 28 and I guessed "/" can use to denote "range to" , but it failed. After that command, my crude variables which equal to 4 change to "/" and others stay same. I don't know why.)

    or
    label define crudecat 1 "1" 2 "2" 3 "3" 3/max "4+"

    All methods show errors and don't work. Most of time we only label variables with fixed number like:
    label define crudecat 1 "1" 2 "2 "

    It tortures me for days.
    Thanks for your time and help
    Last edited by Geralt Ji; 21 May 2020, 22:56.

  • #2
    It seems that values 1 to 3 are really just copied. So the following should suffice to create the variable(s) that you want.

    Code:
    generate crudecat = crude
    replace  crudecat = 4 if crude >= 4
    Likewise, there is no point in assigning label "1" to 1 and so forth; simply

    Code:
    label define crudecat 4 "4+"
    label values crudecat crudecat
    should do.

    Before you ask your next question, please take some time to review the FAQs. Data examples help; use CODE delimters (as I have above) to make code (and output) better readably; statements, such as "did not work" or "showed error" are imprecise and do not help diagnosing the problem, better include a carbon copy of what you typed and what Stata did (or did not) in response.

    Comment


    • #3
      Thanks Daniel Klein!

      Comment

      Working...
      X