Announcement

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

  • Variable labels in new frame

    Hello all,

    I was trying learn frames and put myself to setting up collection of several chi-square test p-values into a new frame. I am running into issues with exporting the variable labels. Can you point me to where the error is occuring? Spaces in variable labels seem to make the code break. Even a pointer to the appropriate help section would be great. See -dataex- and my code.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte(age70 anymds mvaf25 hit) str3 ptxrls1
    1 1 1 0 ""   
    1 0 1 0 ""   
    1 0 0 1 ""   
    1 0 1 0 ""   
    0 0 1 0 ""   
    0 0 1 0 ""   
    1 1 1 0 ""   
    1 0 1 0 ""   
    0 0 1 0 ""   
    0 0 1 1 ""   
    1 0 1 0 ""   
    0 0 1 0 ""   
    0 0 0 0 ""   
    1 0 1 0 ""   
    1 0 0 0 ""   
    0 0 1 0 ""   
    0 0 1 0 ""   
    1 0 0 0 ""   
    0 0 0 0 "Yes"
    0 0 1 1 ""   
    0 0 0 0 "Yes"
    0 0 1 0 ""   
    1 0 1 0 "Yes"
    1 0 1 0 "No" 
    0 0 1 1 "Yes"
    0 0 1 0 "Yes"
    1 0 1 0 ""   
    0 0 1 0 "No" 
    0 0 1 0 ""   
    0 0 0 0 ""   
    0 1 0 0 "Yes"
    1 0 1 1 ""   
    0 0 1 0 ""   
    1 0 1 0 ""   
    0 0 0 0 ""   
    0 1 1 0 "Yes"
    1 1 1 1 ""   
    0 0 0 0 "Yes"
    1 0 1 1 ""   
    0 0 1 0 ""   
    0 0 1 0 ""   
    0 0 0 0 "Yes"
    1 0 1 0 ""   
    0 1 1 0 ""   
    1 0 1 0 ""   
    0 0 0 0 "Yes"
    0 0 1 1 ""   
    0 0 1 0 "Yes"
    0 1 1 0 ""   
    1 0 1 0 ""   
    1 0 1 0 ""   
    0 0 1 0 ""   
    0 0 1 0 "Yes"
    0 0 0 0 ""   
    1 0 0 1 ""   
    0 1 1 0 "Yes"
    0 0 1 1 ""   
    1 0 1 0 ""   
    1 0 1 1 ""   
    1 1 1 0 ""   
    0 0 1 0 ""   
    1 0 1 0 ""   
    1 0 1 0 ""   
    0 1 1 0 ""   
    0 0 1 0 ""   
    1 0 1 0 ""   
    1 0 1 0 ""   
    1 0 1 0 ""   
    0 0 1 0 "Yes"
    0 0 1 0 ""   
    1 0 1 0 ""   
    0 0 1 0 ""   
    0 1 1 1 ""   
    0 0 0 0 ""   
    1 0 0 0 ""   
    1 0 0 0 ""   
    1 1 0 0 ""   
    1 0 0 0 ""   
    1 0 0 0 ""   
    0 0 0 0 ""   
    0 0 0 0 ""   
    1 0 0 0 ""   
    0 0 1 1 ""   
    0 0 1 1 ""   
    1 1 1 0 ""   
    0 0 0 0 ""   
    0 0 0 0 ""   
    1 0 0 0 ""   
    0 0 1 1 ""   
    0 0 1 0 ""   
    0 0 1 1 ""   
    1 0 0 1 ""   
    0 0 1 0 ""   
    0 0 1 0 "No" 
    0 0 0 1 "No" 
    0 0 1 0 ""   
    0 0 1 1 ""   
    0 0 1 0 ""   
    1 1 1 0 ""   
    1 0 1 0 ""   
    end
    label values age70 age70lab
    label def age70lab 0 "≤ 70 yrs.", modify
    label def age70lab 1 "> 70 yrs", modify
    label values anymds anymdslab
    label def anymdslab 0 "No", modify
    label def anymdslab 1 "Yes", modify
    label values mvaf25 mvaf25
    label def mvaf25 0 "≤ 25%", modify
    label def mvaf25 1 "> 25%", modify
    label values hit hit
    label def hit 0 "1 Mutn.", modify
    label def hit 1 "2+ Mutn.", modify
    What I tried:
    Code:
    frame create chi var chi2
    
    local x age70 anymds mvaf25 hit
    
    foreach v of local x{
        tab `v' ptxrls1, chi2
        return list
        local `v'lab: variable label `v'
        frame post chi (``v'lab') (r(p)) 
    }
    frame change chi
    frame change default
    frame drop chi

  • #2
    The problem is that the variable var that you are trying to create in frame chi is a string variable, but you have not done what you need to do to make that work.

    Code:
    frame create chi str244 var chi2
    
    local x age70 anymds mvaf25 hit
    
    foreach v of local x{
        tab `v' ptxrls1, chi2
        return list
        local `v'lab: variable label `v'
        frame post chi (`"``v'lab'"') (r(p))
    }
    frame change chi
    compress
    Note: variable labels can be very long. I made a guess that 244 characters will be enough to accommodate the longest of your labels, but you may need to do something larger. It doesn't hurt to overestimate, although once you are done, using -compress- to shrink the amount of storage to the minimum needed is a good idea.

    Comment


    • #3
      Thanks much, Clyde Schechter I guess the double quotes is needed to handle string variable label with spaces?

      Comment


      • #4
        Yes, In fact, given the very wide variation possible in variable labels, double quotes (" ") are not really sufficient. You should use compound double quotes (`" "'), as shown in #2, to be robust to the possibility that a label may itself contain double quotes.
        Last edited by Clyde Schechter; 05 Aug 2023, 11:27.

        Comment


        • #5
          Finally I understood the utility of compound double quotes. Thanks again, Clyde Schechter

          Comment


          • #6
            Two refinements:

            1. Variable labels cannot be longer than 80 characters (not bytes!). If your labels are ASCII, str80 will do; str320 is needed to hold a variable label that consists of 80 4-byte Unicode characters.
            2. Compound quotes handle double quotes in labels but will choke on left single quotes as in don`t know. There are ways around that but left single quotes are best avoided because they will likely cause problems at some point.

            Comment

            Working...
            X