Announcement

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

  • Keep parts of a variable label with the caveat that the variable labels have weird characters

    Hello. I have a question about automating the variable labeling process. Below is my fictional dataset with my variables and the variable labels. The dataset originally came from a very messy csv file. When you try to run the code below, the variable labels has this weird character â€" and will produce an error code when you try to reproduce my data. You will have to go to the GUI and manually paste it in but I provided code to illustrate how my dataset looks like.

    We are deciding how to create variable labels. There are two options to relabeling the variable labels:

    1)How would I keep everything before â€" symbols in the variable label? For example for the variable bl_instruct_sal,, the variable label would be "Object Code 1000"
    2) How would I keep everything between â€" and the *: symbol? For example, for the variable bl_instruct_sal, the variable label would be "Instructional Salaries"


    I tried this approach to delete out the word "Object Code" but I don't know how to delete out this †symbol.

    Code:
    foreach v of varlist bl_* {
        local lbl: var label `v'
        local lbl: subinstr local lbl "Object Code" "", all
        label var `v' `"`lbl'"'
    }


    Thank you so much for your feedback and help.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input long(bl_instruct_sal bl_non_instruct_sal bl_employ_bene bl_supply_materials bl_o_oper_expenses bl_capital_outlay bl_o_outgoing)
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    end
    
    label var bl_instruct_sal "Object Code 1000 â€" Instructional Salaries*:How were Promise Grant funds alloca"
    label var bl_non_instruct_sal "Object Code 2000 â€" Non-Instructional Salaries*:How were Promise Grant funds al"
    label var bl_employ_bene "Object Code 3000 â€" Employee Benefits*:How were Promise Grant funds allocated b"
    label var bl_supply_materials "Object Code 4000 â€" Supplies and Materials*:How were Promise Grant funds alloca"
    label var bl_o_oper_expenses "Object Code 5000 â€" Other Operating Expenses and Services*:How were Promise Gra"
    label var bl_capital_outlay "Object Code 6000 â€" Capital Outlay*:How were Promise Grant funds allocated by b"
    label var bl_o_outgoing "Object Code 7000 â€" Other Outgoing*:How were Promise Grant funds allocated by b"
    Last edited by joseph wells; 12 Sep 2024, 16:57.

  • #2
    Do not insert the variable labels manually into your dataex example. Simply run

    Code:
    dataex, varlabel
    and then repost.

    Comment


    • #3
      Hi Joseph,

      Notes on that:
      1) I only inputted 1 observation as all (probably 100) of yours seemed identical to me
      2) I had to put your variable label strings into compound double quotes to label the variables correctly
      3) I assumed that you want to get rid of the â€", so these two symbols followed by a "

      Code:
      clear
      input long(bl_instruct_sal bl_non_instruct_sal bl_employ_bene bl_supply_materials bl_o_oper_expenses bl_capital_outlay bl_o_outgoing)
      0 0 0 0 0 0 0
      end
      
      label var bl_instruct_sal `"Object Code 1000 â€" Instructional Salaries*:How were Promise Grant funds alloca"'
      label var bl_non_instruct_sal `"Object Code 2000 â€" Non-Instructional Salaries*:How were Promise Grant funds al"'
      label var bl_employ_bene `"Object Code 3000 â€" Employee Benefits*:How were Promise Grant funds allocated b"'
      label var bl_supply_materials `"Object Code 4000 â€" Supplies and Materials*:How were Promise Grant funds alloca"'
      label var bl_o_oper_expenses `"Object Code 5000 â€" Other Operating Expenses and Services*:How were Promise Gra"'
      label var bl_capital_outlay `"Object Code 6000 â€" Capital Outlay*:How were Promise Grant funds allocated by b"'
      label var bl_o_outgoing `"Object Code 7000 â€" Other Outgoing*:How were Promise Grant funds allocated by b"'
      
      
      foreach v of varlist bl_* {
          local lbl: var label `v'
          // directly remove string â€"
          local lbl1: subinstr local lbl `"â€""' "", all
          // alternative 1: keep everything before that string
          local lbl2 = ustrregexra(`"`lbl'"', "(.*)â€(.*):(.*)", "$1")    
          // alternative 2: keep everything after that string
          local lbl3 = ustrregexra(`"`lbl'"', "(.*)â€(.*):(.*)", "$2")
          local lbl3 = trim(subinstr(`"`lbl3'"',char(34),"",.))
          // clone variables in order to show different labels
          clonevar `v'_2 = `v'
          clonevar `v'_3 = `v'
          label var `v' `"`lbl1'"'
          label var `v'_2 `"`lbl2'"'
          label var `v'_3 `"`lbl3'"'
      }
      Hope that helps

      [edited typo]

      Comment


      • #4
        @Benno Schoenberger This is exactly what I was looking for. Adding the compound double quotes fixed the issue with the data. Your loop also worked perfectly. Thank you so much.

        Comment

        Working...
        X