Announcement

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

  • Quoted variable label in frame header

    I have an issue passing a variable label into one of the column headers for a frame I want to display. I have tried variations of quotes, double quotes and compound quotes but it is not helping. Guessing some illegal characters and spaces need some sanitizing? Not sure if this a very reproducible example but all of this is part of a bigger wrapper to conduct multiple Chi-square tests and post to a frame for display for exploratory analysis.

    Code:
    clear
    
    webuse auto
    local collabel : variable label make
    
    di "`collabel'"
    
    cap frame drop test
    frame create test ///
        str100 "`collabel'"
    
    frame change test
    list, noobs table
    Output I get

    Code:
    . clear
    
    . 
    . webuse auto
    (1978 automobile data)
    
    . local collabel : variable label make
    
    . 
    . di "`collabel'"
    Make and model
    
    . 
    . cap frame drop test
    
    . frame create test ///
    >         str100 "`collabel'"
    "Make and model invalid name
    r(198);
    
    end of do-file
    
    r(198);

  • #2
    You can't do that. "Make and model" is not a legal variable name in Stata.

    This is perhaps an example of why it is best not to use spreadsheet terminology like "colulmns" and "rows" when talking about Stata. Stata is not a spreadsheet, even though in the Viewer it may look like one. What in a spreadsheet would be called a column is, in Stata, called a variable, and what in a spreadsheet could be called a column header is, in Stata, the variable's name. Spreadsheet column headers have few or no restrictions on what they can be. Stata variable names must consist exclusively of letters, digits, and the underscore character, the first character may not be a digit, and the length is capped at 32 characters. Because "Make and model" contains blanks, it is not a legal variable name. Using spreadsheet terminology when discussing Stata data sets perhaps subtly encourages you to think about Stata as if it were a spreadsheet, which, in turn, leads you to do things that would be fine in a spreadsheet but don't work in Stata. (And perhaps also to do things in complicated spreadsheet-friendly ways that can be more simply accomplished some other way in Stata.)

    Stata does have a -strtoname()- function which will convert strings into highly similar legal variable names. So you might want to do:
    Code:
    clear*
    
    webuse auto
    local collabel : variable label make
    local variable_name = strtoname(`"`collabel'"')
    
    di "`collabel'"
    di `"`variable_name'"'
    
    cap frame drop test
    frame create test ///
        str100 `variable_name'
    
    frame change test
    des
    I have eliminated your -list, noobs tabl- command because, even though it is legal, it is pointless: at this stage you have not put any data into frame test, so the -list- command produces no output. I have instead suggested the -des- command which will at least show you the names of the variables in the frame, enabling you to confirm that you got the variable you asked for.

    Comment


    • #3
      I see. Thanks for the detailed explanation Clyde Schechter This worked. Sorry for using that spreadsheet terminology. I was trying to pass the variable label as a column header. All my variable names are thankfully legal ones in Stata. The variable labels do have their spaces, superscripts and other characters. I was perplexed how -di- faithfully displays the content of the variable label macro but passing the same macro to the new frame as a column header glitches. Maybe I will just pass the variable names instead of their labels since I did not want a pretty header anyway for this exploratory phase. Will put that -strtoname- function to use for when I think variable label may be more useful.

      Comment

      Working...
      X