Announcement

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

  • Creating dummy variables (i.e., indicator variables) for tempvar.

    Hi Everyone,

    I am writing a program that will need to produce indicator variables for several analyses. I'd like for the variables to disappear once the program is through running. Is there a way to produce dummy-variables that are tempvars? I can produce real dummy variables from tempvar, but not tempvars dummy variables from tempvar.

    Cheers,

    David.

  • #2
    It depends on what you want to do with them. Many Stata commands support factor-variable notation now, and if you will be using them only for those purposes, then there is no need to create dummy variables at all. Just use factor-variable notation and the variables disappear after use. In fact, they never actually appear in your data set. -help fvvarlist-.

    If you need to use them in commands that will not support factor-variables, you can just create tempvars equal to the corresponding factor variables. The following code illustrates how it is done, demonstrates that the variables work as expected, and shows that they disappear once the program ends:
    Code:
    sysuse auto, clear
    
    //    CREATE TEMPORARY INDICATOR VARIABLES FOR rep78
    levelsof rep78, local(values)
    foreach v of local values {
        tempvar rep78_`v'
        gen byte `rep78_`v'' = `v'.rep78
        label var `rep78_`v'' "Indicator: rep78 == `v'"
    }
    
    des
    
    foreach v of local values {
        tab `rep78_`v'' rep78, miss
    }

    Comment


    • #3
      There is a command dedicated to creating temporary variables for factor variables; see help fvrevar.
      Here is an example based on Clyde's above, but lacks his nice variable labels.
      Code:
      sysuse auto
      
      fvrevar bn.rep78
      local rep78_levels = r(varlist)
      
      describe
      char list
      The output from describe shows the temp vars created by fvrevar, sorry no nice variable labels.
      char list shows the meta data that Stata uses to link these temp vars back to their source.

      Comment


      • #4
        Here is a dirty trick, relying on the fact that temporary names are (usually, and perhaps always) 8 characters long, while variable names can be longer. Hence you can add suffixes of modest length. That may or may not be helpful but it does not seem widely known or used. You would need to check on whether such "temporary" variables really are temporary.


        Code:
        . sysuse auto, clear
        (1978 automobile data)
        
        . tempname rep78
        
        . tab rep78, gen(`rep78')
        
             Repair |
        record 1978 |      Freq.     Percent        Cum.
        ------------+-----------------------------------
                  1 |          2        2.90        2.90
                  2 |          8       11.59       14.49
                  3 |         30       43.48       57.97
                  4 |         18       26.09       84.06
                  5 |         11       15.94      100.00
        ------------+-----------------------------------
              Total |         69      100.00
        
        . d `rep78'*
        
        Variable      Storage   Display    Value
            name         type    format    label      Variable label
        ----------------------------------------------------------------------------------------------------------------
        __0000001       byte    %8.0g                 rep78== 1.0000
        __0000002       byte    %8.0g                 rep78== 2.0000
        __0000003       byte    %8.0g                 rep78== 3.0000
        __0000004       byte    %8.0g                 rep78== 4.0000
        __0000005       byte    %8.0g                 rep78== 5.0000
        
        .

        Comment


        • #5
          Clyde Schechter It's for a program that needs to calculate a variety of statistical details per level of categorical variable. I'm using the dummy variables to produce percentages per level of category. Many of the datasets I'm using have a lot of categorical variables and I didn't want to produce a huge amount of dummies. Your solution is magnificent though, I didn't realize dummies could be produced that way. This solves the issue.

          Jeff Pitblado (StataCorp) I had never seen the fvrevar command before, this is so helpful because I'm still using Stata 15. Thanks so much!

          Nick Cox That's really neat - I think I can apply that to a related problem. Thanks!

          Comment

          Working...
          X