Announcement

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

  • List of Variables in Local Using Levelsof

    I have a variable with 50 categories and would like to create indicators for each category. Then I would like to add the names of each category into a local and keep only those indicators plus some other variables. So I tried this:

    create local list of all categotries:
    Code:
     levelsof cat_var, local(cat_list)
    local list of group 1 variables to keep
    Code:
     local indvars   = "ind1 ind2"
    local list of group 2 variables to keep
    Code:
     local facvars   = "fac1 fac2"
    local list of all variables to keep
    Code:
     local vars  = "`indvars' `facvars' `cat_list'"
    create indicators for each category:
    Code:
    foreach var in `cat_list' {
        gen `var' = (cat_var == "`var'")
    }
    keep variables from local list:
    Code:
     keep `vars'

    I get the error message ` invalid name. I think the source of the error is levelsof.

  • #2
    Originally posted by Trevor Andrew View Post
    I think the source of the error is levelsof.
    Probably due to the compound quotation marks that levelsof returns the list in. One way to get a clean copy is along the following lines. Begin at the "Begin here" comment; the stuff above is just to create a dataset for illustration.
    Code:
    version 19
    
    clear *
    
    // seedem
    set seed 67478420
    
    quietly set obs 50
    
    // Variables to drop
    forvalues i = 1/5 {
        generate byte dro`i' = 1
    }
    
    // Variables to keep
    generate str cat_var = char(runiformint(65, 69))
    forvalues i = 1/2 {
        generate double ind`i' = runiform()
        generate byte fac`i' = runiformint(1, 5)
    }
    
    *
    * Begin here
    *
    quietly levelsof cat_var, local(cat_list)
    local indvars ind1 ind2
    local facvars fac1 fac2
    
    local vars `indvars' `facvars'
    
    foreach var of local cat_list {
        generate byte `var' = cat_var == "`var'"
        local vars `vars' `var'
    }
    
    keep `vars'
    
    exit
    You could also use the : list clean macname macro list function, see its help file.

    Comment


    • #3
      Well, the contents of local macro cat_list will be the values of the variable cat_var. It is likely that these will not be valid variable names--which will result in an invalid name error message when you try to -gen `var' ...- where `var' is one of the elements of `cat_list'. Your code assumes that variable cat_var is a string variable (because otherwise -(catvar == "`var'")- would produce a type mismatch error.) But not all strings are valid variable names, and it appears that at least some of them, in your case are not. It would be easier to give you specific advice on how to proceed from here if you used the -dataex- command* to show example data.

      But let me also ask why you even want to do this. It is rarely necessary, or even useful, in Stata to create single variables corresponding to the levels of a categorical variable--you can use factor-variable notation for this instead. See -help fvvarlist- for more information on how that works.

      *If you are running version 16 or later, or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

      Added: Crossed with #2. The example data that is created there is specifically designed to set the value of cat_var to strings that are legal variable names. That happy result may not obtain in your real data, however.
      Last edited by Clyde Schechter; 11 May 2025, 20:39.

      Comment

      Working...
      X