Announcement

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

  • Labelling dummy variables with underlying variable names

    Hey there,

    I'm very new to Stata and fairly certain this has a simple solution but i don't seem to find it.

    I have a Dataset with 200 different regions and want to create Dummies for each of the regions. I want to label the dummies according to the name of the region.


    When doing as below I only get another round of R_1 all the way to R_200 as Dummies. What goes into region in order to not use the numeric labelling but the actual names of the region as a label for the dummies?

    levelsof region, local(region)
    foreach i of numlist `region' {
    gen R_`i'=region==`i'
    }


    Thanks in advance guys!

    Cheers

  • #2
    Code:
    help tabulate
    -- but it's unlikely that you really need all those variables any way. Use factor variable notation.

    Comment


    • #3
      Being new to the matter I don't see where this is supposed to lead me. Could you be more specific?

      Comment


      • #4
        Everyone here, new or old or ancient, is asked to try to read the documentation first as explained at http://www.statalist.org/forums/help#before That underlines that help and search are your friends here.

        Did you type

        Code:
        help tabulate 
        
        search factor variable
        ?

        Clicking through will tell you that tabulate has a generate() option for generating indicator variables and that (e.g.) help fvvarlist tells you about factor variable notation as a way of avoiding indicator variables. (You say dummy variables.)


        Comment


        • #5
          Just in case you need to transform the labels somehow, here is the direct code:
          Code:
          // your data goes here
          sysuse nlsw88, clear
          
          local vname "occupation"
          local root "o_"
          keep `vname'
          
          // my code is here
          levelsof `vname', local(levs)
          local dctname `:value label `vname''
          foreach i of numlist `levs' {
            generate byte `root'`i'=`vname'==`i'
            label variable `root'`i' "`:label `dctname' `i''"
          }
          
          // show the result
          describe
          Produces:
          Code:
            obs:         2,246                          NLSW, 1988 extract
           vars:            14                          1 May 2011 22:52
           size:        31,444                          (_dta has notes)
          ----------------------------------------------------------------------------------------------------------------------------------------------------------
                        storage   display    value
          variable name   type    format     label      variable label
          ----------------------------------------------------------------------------------------------------------------------------------------------------------
          occupation      byte    %22.0g     occlbl     occupation
          o_1             byte    %8.0g                 Professional/technical
          o_2             byte    %8.0g                 Managers/admin
          o_3             byte    %8.0g                 Sales
          o_4             byte    %8.0g                 Clerical/unskilled
          o_5             byte    %8.0g                 Craftsmen
          o_6             byte    %8.0g                 Operatives
          o_7             byte    %8.0g                 Transport
          o_8             byte    %8.0g                 Laborers
          o_9             byte    %8.0g                 Farmers
          o_10            byte    %8.0g                 Farm laborers
          o_11            byte    %8.0g                 Service
          o_12            byte    %8.0g                 Household workers
          o_13            byte    %8.0g                 Other
          ----------------------------------------------------------------------------------------------------------------------------------------------------------
          Sorted by:
          Use Nick's advice if you are not transforming the value labels anyhow.

          Best, Sergiy Radyakin

          Comment


          • #6
            Thanks Sergiy!

            Comment

            Working...
            X