Announcement

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

  • Wrong manipulation of a string variable with cleanchars

    Dear Statalisters,

    I am trying to convert an age variable into a variable for age groups as follows 1-5 ; 6-10 ; 11-15 ; ... I am using Stata 13.1 and using the user-written code cleanchars after the command egen cut as follows:
    #
    Code:
    * Creating dummies for age groups
    clear
    set obs 500
    set more off
    gen age=int(100*runiform())+1
    sum age
    local mn = r(min)
    local mx = r(max)
    forvalues bot=`mn'(5)`mx' {
    local top = `bot'+4
    gen age`bot'to`top' = age >= `bot' & age <= `top'
    }
    
    * converting age variable into age groups
    egen agegroup = cut(age), at(1 (5) 101) label
    
    forvalues i = 1(5)96 {
    local j = `i'+ 4
    cleanchars , in("`i'-") out("`i'-`j'") vlab vval values
    }
    
    #
    I obtained the following output:

    #
    Code:
    agegroup:
    0 1-5
    1 6-10
    2 11-155
    3 16-2010
    4 21-255
    5 26-3010
    6 31-355
    7 36-4010
    8 41-455
    9 46-5010
    10 51-555
    11 56-6010
    12 61-655
    13 66-7010
    14 71-755
    15 76-8010
    16 81-855
    17 86-9010
    18 91-955
    19 96-10
    20 101-5
    name or _all required
    
    #
    I am probably missing something very basic I could not figure out. Any help will be highly appreciated.

    Thanks.
Working...
X