Announcement

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

  • Generating dummy variables WITH the value of the variable as name AND keeping the label of the new variable the same as the value label

    Hello stata community,

    I have a problem while creating dummy variables and having the correct name and label. So I have an expenditure dataset with multiple households, consuming multiple goods. Something like that (I put after the : the label of the value given):

    Family ID | Expenditure ID (expcode)

    1 | 2 : Energy expenditure
    1 | 5 : Utilities expenditure
    1 | 7 : Food expenditure
    2 | 1 : Housing expenditure
    2 | 11 : Leisure expenditure
    3 | 3 : Clothing expenditure
    3 | 8 : Fruits expenditures
    3 | 11 : Education expenditure

    As showed above, very expenditure ID has a label that corresponds, for example 1 is Housing expenditure, 2 Energy expenditure, 3 Clothing expenditure, etc.

    What I would like to do is to create dummy variables for each of the expenditure category that will have:
    -As name, the value of the expenditure category with a prefix (like exp_).
    -As label, the label of each expenditure category.

    Which will translate into something like this for the first dummy variable: exp_1 : Housing expenditures
    second dummy var: exp_2 : Energy expenditure

    I tried with many different solutions and so far I have managed to generate the variable name correctly with either of these 2 codes:

    numlabel, add
    dummieslab exp, word(1) template (exp_@)

    or

    levelsof expcode, local(exp)
    foreach i of numlist `exp' {
    gen exp_`i'=expcode==`i'
    }


    but nothing on the label! I tried labelmask, label def and so far no results! If anybody could help me, it would be great, I have more than 500 variables so impossible to do it one by one thank you!

  • #2
    Why not use label var inside the foreach loop since you already have the label levels and successfully created the named variable?

    Comment


    • #3
      No additional ado files from SSC, nor loops, needed; this is built-in functionality with Stata's -tabulate-:
      Code:
      tabulate expcode , generate(exp_)
      In case you want to change the variables' labels afterwards, of course, you will have to use a loop anyways.

      Regards
      Bela

      PS: To ease giving an answer to possible future questions, please have a (second?) look at the FAQ (also linked at the top of each page) on how to post, especially the parts about data examples and using CODE-delimiters. Thanks in advance.
      Last edited by Daniel Bela; 03 Nov 2017, 07:03. Reason: added PS

      Comment

      Working...
      X