Announcement

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

  • Convert composite string variable delimited by commas into separate dummy variables

    Hi, I have observations of building tenants which has a string variable recording the floors they occupy delimited by commas (e.g. ‘GRND,1st,MEZZ,4-6’), and for each of these observations I would like to convert this into separate dummy variables for each floor occupied (i.e. in the case above, create dummy variables coded ‘1’ for GRND, 1, MEZZ, 4, 5, and 6). Does anyone know how I might code this?

    Cheers


  • #2
    Gerard:
    do you mean something along the following lines?:
    Code:
    . set obs 1
    number of observations (_N) was 0, now 1
    
    . g A="‘GRND,1st,MEZZ,4-6’"
    
    . split A, parse(",") gen(nA)
    variables created as string: 
    nA1  nA2  nA3  nA4
    
    . list
    
         +-------------------------------------------------+
         |                   A     nA1   nA2    nA3    nA4 |
         |-------------------------------------------------|
      1. | ‘GRND,1st,MEZZ,4-6’   ‘GRND   1st   MEZZ   4-6’ |
         +-------------------------------------------------+
    
    .
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3

      Hi Carlo,

      Almost. I would like to also create dummies for the floors implicitly contained in-between two numbers separated by hyphens. So instead of a '4-6' dummy, separate dummies for 4, 5, and 6.

      Cheers!

      Comment

      Working...
      X