Announcement

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

  • Extracting Value Lables in Quotations

    Consider the dataset and code
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float id
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    end
    label values id regionname
    label def regionname 1 "Andalucia", modify
    label def regionname 2 "Aragon", modify
    label def regionname 3 "Baleares (Islas)", modify
    label def regionname 4 "Canarias", modify
    label def regionname 5 "Cantabria", modify
    label def regionname 6 "Castilla Y Leon", modify
    label def regionname 7 "Castilla-La Mancha", modify
    label def regionname 8 "Cataluna", modify
    label def regionname 9 "Comunidad Valenciana", modify
    label def regionname 10 "Extremadura", modify
    label def regionname 11 "Galicia", modify
    label def regionname 12 "Madrid (Comunidad De)", modify
    label def regionname 13 "Murcia (Region de)", modify
    label def regionname 14 "Navarra (Comunidad Foral De)", modify
    label def regionname 15 "Pais Vasco", modify
    label def regionname 16 "Principado De Asturias", modify
    label def regionname 17 "Rioja (La)", modify
    
    loc labs 8 12 16 17
            local lab : value label id
    
            foreach l of local labs {
                local controls `controls' `: label `lab' `l''
            }
    
    // which returns...
    // Cataluna Madrid (Comunidad De) Principado De Asturias Rioja (La)
    What I want, is for the macro to read as
    Code:
    "Cataluna" "Madrid (Comunidad De)" "Principado De Asturias" "Rioja (La)"
    How might I do this?

  • #2
    Code:
    foreach l of local labs {
        
       local name : label `lab' `l'   
       local controls = `"`controls'"' + char(34) + "`name'" + char(34) + char(32)
    }
    Code:
    . mac di _controls
    _controls:      "Cataluna" "Madrid (Comunidad De)" "Principado De Asturias" "Rioja (La)"

    Comment


    • #3
      Okay so this works perfect, but now I have an additional one.... Why does this work? Let me write how I understand it in pseudo-code. `name' is meant to take the value label of each iterative l in the loop. We then define another macro named controls, which at first will be empty, but we then add `name' as we've just defined it above in quotations like I asked originally.

      The part I don't get at all however is char(34) and char(32). I've never even seen or heard of these. When I do h char I get the characteristics programming option, which I've never heard of before. Is this a reference to ASCII characters, perhaps?

      Comment


      • #4
        It appears you missed this detail in my answer to your other post. My toy example picks variable labels, but labels that include spaces nevertheless.

        On your last question here, type

        Code:
        help char()
        including the parentheses.

        The solution in #2 uses the char() function but it should not be necessary. I did not explicitly test this one, but

        Code:
        ...
        local controls `"`controls' `"`name'"'"'
        ...
        should work just fine. The key idea is that compound double quotes nest. You may use simple double quotes as in

        Code:
        local controls `"`controls' "`name'""'
        if `name' does not contain double quotes.

        Obviously, you feed the final local as

        Code:
        ... `controls'
        omitting any double quotes.

        Comment


        • #5
          #2 use compound double quotes see -help quotes-, and -help char()- for the function char() returning character n (ASCII).

          Comment

          Working...
          X