Announcement

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

  • Invalid syntax error while assigning labels to multiple variables using loop

    Hi,
    I wish to assign labels to multiple variables. I am aware of the elabel command in SSC, however I wanted to see how I could do it using loop. This is the code I used:

    Code:
    local varlist t_hhexp school_fees pvt_tuition t_childrensedu_exp
    
    local labels "Household expenditure" ///
                 "School fees" ///
                 "Private tuition fees" ///
                 "Children's education expenditure"
    
    local i = 1
    foreach var of local varlist {
        label variable `var' : word(`labels', `i')
        local ++i
    }
    However, I get the error 'invalid syntax r(198)' when I run this code. Could someone explain to me why I am getting an error?

    Thank you!

  • #2
    The invalid syntax is -: word(`labels', `i')- The colon ( operator for introducing macro functions has to be used within local macro quotes (` '), and then you must use the macro word function, not the -gen- word() function. Finally, the -label var- command also requires that the variable label be enclosed in quotes and compound double quotes are safer if the label might itself contain quotes. So you need this:
    Code:
    local i = 1
    foreach var of local varlist {
        label variable `var' `"`:word `i' of `labels''"'
        local ++i
    }

    Comment


    • #3
      In addition, I suspect that you need to use compound double quotes in defining local macro labels.

      Comment


      • #4
        Code:
        clear
        set obs 1
        local varlist t_hhexp school_fees pvt_tuition t_childrensedu_exp
        input `varlist'
        end 
        
        local labels  `" "Household expenditure" "School fees" "Private tuition fees" "Children's education expenditure" "'  
        mac li _labels // inspect local macro
        
        local i = 1
        
        foreach var of local varlist {
            
             local label : word `i' of `labels'     
             label variable `var' "`label'"
            
            local ++i
        }
        Code:
        Contains data
         Observations:             1                  
            Variables:             4                  
        --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
        Variable      Storage   Display    Value
            name         type    format    label      Variable label
        --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
        t_hhexp         float   %9.0g                 Household expenditure
        school_fees     float   %9.0g                 School fees
        pvt_tuition     float   %9.0g                 Private tuition fees
        t_childrensed~p float   %9.0g                 Children's education expenditure
        --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
        Sorted by: 
             Note: Dataset has changed since last saved.

        Comment


        • #5
          FWIW, elabel (SSC, SJ or GitHub) would do this as
          Code:
          elabel variable (*)                     ///
              (                                   ///
              "Household expenditure"             ///
              "School fees"                       ///
              "Private tuition fees"              ///
              "Children's education expenditure"  ///
              )
          Last edited by daniel klein; 04 Jan 2025, 13:32.

          Comment


          • #6
            Thank you all so much for answering! This should really help me with future data cleaning exercises

            Comment

            Working...
            X