Announcement

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

  • Error in for-loop string editing

    I have a dataset which consists of one column of variable names (varname) and one column of their labels (vlabel). I am trying to reshape this such that it consists of as many columns as there are variable names, with each variable name having its label below it. Then, I will iterate over the list of varnames and apply the vlabel to each. However, before I apply the label, I want to cut out any occurrences of double quotes in the vlabel to ensure that no errors arise, and I want to reduce the size of the label by eliminating occurrences of "Estimate"; and "Margin of Error", and replacing them with nothing and "MoE" respectively.

    However, I am running into an error r(198). The code, error output, and data example are listed below:

    Code:
    clear all
    cap log close
    cap program drop _all
    set maxvar 5000
    
    ssc install sxpose
    
    import excel ${local_publicschoolchildren_data}ACS-ED_2015-2019_RecordLayouts.xlsx, sheet("CDP_ChildPop") firstrow allstring clear
    
    replace varname = lower(varname)
    drop denominator
    
    sxpose, clear firstnames
    
    foreach var of varlist _all {
        local tempvar1 = subinstr(`var'[1], `"""',  `""', .)
        local tempvar2 = subinstr("`tempvar1'","Estimate;", .)
        local tempvar3 = subinstr("`tempvar2'","Margin of Error;", "MoE;")
        label variable `var' "`tempvar3'"
    }
    With set trace on enabled, I hit the loop and obtain this output/error:

    . foreach var of varlist _all {
    2. local tempvar1 = subinstr(`var'[1], `"""', `""', .)
    3. local tempvar2 = subinstr("`tempvar1'","Estimate;", .)
    4. local tempvar3 = subinstr("`tempvar2'","Margin of Error;", "MoE;")
    5. label variable `var' "`tempvar3'"
    6. }

    - foreach var of varlist _all {
    - local tempvar1 = subinstr(`var'[1], `"""', `""', .)
    = local tempvar1 = subinstr(cdp02_1est[1], `"""', `""', .)
    - local tempvar2 = subinstr("`tempvar1'","Estimate;", .)
    = local tempvar2 = subinstr("Number; Estimate; HOUSEHOLDS BY TYPE; Total households","Estimate;", .)
    invalid syntax
    local tempvar3 = subinstr("`tempvar2'","Margin of Error;", "MoE;")
    label variable `var' "`tempvar3'"
    }

    Dataex included below:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str54 cdp02_1est str61 cdp02_1moe str77 cdp02_2est
    "Number; Estimate; HOUSEHOLDS BY TYPE; Total households" "Number; Margin of Error; HOUSEHOLDS BY TYPE; Total households" "Number; Estimate; HOUSEHOLDS BY TYPE; Total households; Married-couple family"
    end
    Thank you for your help.
    Last edited by Jay Philbrick; 06 Apr 2022, 23:56.

  • #2
    one column of variable names (varname) and one column of their labels (vlabel)
    Please give a data example that matches this description.

    Comment


    • #3
      Sorry -- that was the finished data snippet. Here is the original input, after which I run the code, which makes the varnames lowercase and drops the denominator, and then sxposes and runs the for loop.

      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input str16 varname str236 vlabel str13 denominator
      "CDP02_1est"     "Number; Estimate; HOUSEHOLDS BY TYPE; Total households"                                                                                                         ""          
      "CDP02_1moe"     "Number; Margin of Error; HOUSEHOLDS BY TYPE; Total households"                                                                                                  ""          
      "CDP02_2est"     "Number; Estimate; HOUSEHOLDS BY TYPE; Total households; Married-couple family"                                                                                  ""          
      "CDP02_2moe"     "Number; Margin of Error; HOUSEHOLDS BY TYPE; Total households; Married-couple family"                                                                           ""          
      "CDP02_2pct"     "Percent; Estimate; HOUSEHOLDS BY TYPE; Total households; Married-couple family"                                                                                 "CDP02_1est"
      "CDP02_2pctmoe"  "Percent; Margin of Error; HOUSEHOLDS BY TYPE; Total households; Married-couple family"                                                                          ""          
      "CDP02_3est"     "Number; Estimate; HOUSEHOLDS BY TYPE; Total households; Cohabiting couple household"                                                                            ""          
      "CDP02_3moe"     "Number; Margin of Error; HOUSEHOLDS BY TYPE; Total households; Cohabiting couple household"                                                                     ""          
      "CDP02_3pct"     "Percent; Estimate; HOUSEHOLDS BY TYPE; Total households; Cohabiting couple household"                                                                           "CDP02_1est"
      "CDP02_3pctmoe"  "Percent; Margin of Error; HOUSEHOLDS BY TYPE; Total households; Cohabiting couple household"                                                                    ""          
      "CDP02_4est"     "Number; Estimate; HOUSEHOLDS BY TYPE; Total households; Male householder, no spouse/partner present"                                                            ""          
      "CDP02_4moe"     "Number; Margin of Error; HOUSEHOLDS BY TYPE; Total households; Male householder, no spouse/partner present"                                                     ""          
      "CDP02_4pct"     "Percent; Estimate; HOUSEHOLDS BY TYPE; Total households; Male householder, no spouse/partner present"                                                           "CDP02_1est"
      "CDP02_4pctmoe"  "Percent; Margin of Error; HOUSEHOLDS BY TYPE; Total households; Male householder, no spouse/partner present"                                                    ""          
      "CDP02_5est"     "Number; Estimate; HOUSEHOLDS BY TYPE; Total households; Female householder, no spouse/partner present"                                                          ""          
      "CDP02_5moe"     "Number; Margin of Error; HOUSEHOLDS BY TYPE; Total households; Female householder, no spouse/partner present"                                                   ""          
      "CDP02_5pct"     "Percent; Estimate; HOUSEHOLDS BY TYPE; Total households; Female householder, no spouse/partner present"                                                         "CDP02_1est"
      "CDP02_5pctmoe"  "Percent; Margin of Error; HOUSEHOLDS BY TYPE; Total households; Female householder, no spouse/partner present"                                                  ""          
      "CDP02_6est"     "Number; Estimate; HOUSEHOLDS BY TYPE; Total households; Female householder, no spouse/partner present; Householder living alone"                                ""          
      "CDP02_6moe"     "Number; Margin of Error; HOUSEHOLDS BY TYPE; Total households; Female householder, no spouse/partner present; Householder living alone"                         ""          
      "CDP02_6pct"     "Percent; Estimate; HOUSEHOLDS BY TYPE; Total households; Female householder, no spouse/partner present; Householder living alone"                               "CDP02_1est"
      "CDP02_6pctmoe"  "Percent; Margin of Error; HOUSEHOLDS BY TYPE; Total households; Female householder, no spouse/partner present; Householder living alone"                        ""          
      "CDP02_7est"     "Number; Estimate; HOUSEHOLDS BY TYPE; Total households; Households with one or more people 65 years and over"                                                   ""          
      "CDP02_7moe"     "Number; Margin of Error; HOUSEHOLDS BY TYPE; Total households; Households with one or more people 65 years and over"                                            ""          
      "CDP02_7pct"     "Percent; Estimate; HOUSEHOLDS BY TYPE; Total households; Households with one or more people 65 years and over"                                                  "CDP02_1est"
      "CDP02_7pctmoe"  "Percent; Margin of Error; HOUSEHOLDS BY TYPE; Total households; Households with one or more people 65 years and over"                                           ""          
      "CDP02_8est"     "Number; Estimate; HOUSEHOLDS BY TYPE; Total households; Average number of children in households with children"                                                 ""          
      "CDP02_8moe"     "Number; Margin of Error; HOUSEHOLDS BY TYPE; Total households; Average number of children in households with children"                                          ""          
      "CDP02_9est"     "Number; Estimate; HOUSEHOLDS BY TYPE; Total households; Average number of children in families with children"                                                   ""          
      "CDP02_9moe"     "Number; Margin of Error; HOUSEHOLDS BY TYPE; Total households; Average number of children in families with children"                                            ""          
      "CDP02_10est"    "Number; Estimate; SCHOOL ENROLLMENT; Population 3 years and over enrolled in school"                                                                            ""          
      "CDP02_10moe"    "Number; Margin of Error; SCHOOL ENROLLMENT; Population 3 years and over enrolled in school"                                                                     ""          
      "CDP02_11est"    "Number; Estimate; SCHOOL ENROLLMENT; Population 3 years and over enrolled in school; Nursery school, preschool"                                                 ""          
      "CDP02_11moe"    "Number; Margin of Error; SCHOOL ENROLLMENT; Population 3 years and over enrolled in school; Nursery school, preschool"                                          ""          
      "CDP02_11pct"    "Percent; Estimate; SCHOOL ENROLLMENT; Population 3 years and over enrolled in school; Nursery school, preschool"                                                "CDP02_10est"
      "CDP02_11pctmoe" "Percent; Margin of Error; SCHOOL ENROLLMENT; Population 3 years and over enrolled in school; Nursery school, preschool"                                         ""          
      "CDP02_12est"    "Number; Estimate; SCHOOL ENROLLMENT; Population 3 years and over enrolled in school; Kindergarten"                                                              ""          
      "CDP02_12moe"    "Number; Margin of Error; SCHOOL ENROLLMENT; Population 3 years and over enrolled in school; Kindergarten"                                                       ""          
      "CDP02_12pct"    "Percent; Estimate; SCHOOL ENROLLMENT; Population 3 years and over enrolled in school; Kindergarten"                                                             "CDP02_10est"
      "CDP02_12pctmoe" "Percent; Margin of Error; SCHOOL ENROLLMENT; Population 3 years and over enrolled in school; Kindergarten"                                                      ""          
      "CDP02_13est"    "Number; Estimate; SCHOOL ENROLLMENT; Population 3 years and over enrolled in school; Elementary school (grades 1-8)"                                            ""          
      "CDP02_13moe"    "Number; Margin of Error; SCHOOL ENROLLMENT; Population 3 years and over enrolled in school; Elementary school (grades 1-8)"                                     ""          
      "CDP02_13pct"    "Percent; Estimate; SCHOOL ENROLLMENT; Population 3 years and over enrolled in school; Elementary school (grades 1-8)"                                           "CDP02_10est"
      "CDP02_13pctmoe" "Percent; Margin of Error; SCHOOL ENROLLMENT; Population 3 years and over enrolled in school; Elementary school (grades 1-8)"                                    ""          
      "CDP02_14est"    "Number; Estimate; SCHOOL ENROLLMENT; Population 3 years and over enrolled in school; High school (grades 9-12)"                                                 ""          
      "CDP02_14moe"    "Number; Margin of Error; SCHOOL ENROLLMENT; Population 3 years and over enrolled in school; High school (grades 9-12)"                                          ""          
      "CDP02_14pct"    "Percent; Estimate; SCHOOL ENROLLMENT; Population 3 years and over enrolled in school; High school (grades 9-12)"                                                "CDP02_10est"
      "CDP02_14pctmoe" "Percent; Margin of Error; SCHOOL ENROLLMENT; Population 3 years and over enrolled in school; High school (grades 9-12)"                                         ""          
      "CDP02_15est"    "Number; Estimate; SCHOOL ENROLLMENT; Population 3 years and over enrolled in school; College or graduate school"                                                ""          
      "CDP02_15moe"    "Number; Margin of Error; SCHOOL ENROLLMENT; Population 3 years and over enrolled in school; College or graduate school"                                         ""          
      "CDP02_15pct"    "Percent; Estimate; SCHOOL ENROLLMENT; Population 3 years and over enrolled in school; College or graduate school"                                               "CDP02_10est"
      "CDP02_15pctmoe" "Percent; Margin of Error; SCHOOL ENROLLMENT; Population 3 years and over enrolled in school; College or graduate school"                                        ""          
      "CDP02_16est"    "Number; Estimate; DISABILITY STATUS OF THE CIVILIAN NONINSTITUTIONALIZED POPULATION; Total Civilian Noninstitutionalized Population"                            ""          
      "CDP02_16moe"    "Number; Margin of Error; DISABILITY STATUS OF THE CIVILIAN NONINSTITUTIONALIZED POPULATION; Total Civilian Noninstitutionalized Population"                     ""          
      "CDP02_17est"    "Number; Estimate; DISABILITY STATUS OF THE CIVILIAN NONINSTITUTIONALIZED POPULATION; Total Civilian Noninstitutionalized Population; With a disability"         ""          
      "CDP02_17moe"    "Number; Margin of Error; DISABILITY STATUS OF THE CIVILIAN NONINSTITUTIONALIZED POPULATION; Total Civilian Noninstitutionalized Population; With a disability"  ""          
      "CDP02_17pct"    "Percent; Estimate; DISABILITY STATUS OF THE CIVILIAN NONINSTITUTIONALIZED POPULATION; Total Civilian Noninstitutionalized Population; With a disability"        "CDP02_16est"
      "CDP02_17pctmoe" "Percent; Margin of Error; DISABILITY STATUS OF THE CIVILIAN NONINSTITUTIONALIZED POPULATION; Total Civilian Noninstitutionalized Population; With a disability" ""          
      "CDP02_18est"    "Number; Estimate; DISABILITY STATUS OF THE CIVILIAN NONINSTITUTIONALIZED POPULATION; Under 18 years"                                                            ""          
      "CDP02_18moe"    "Number; Margin of Error; DISABILITY STATUS OF THE CIVILIAN NONINSTITUTIONALIZED POPULATION; Under 18 years"                                                     ""          
      "CDP02_19est"    "Number; Estimate; DISABILITY STATUS OF THE CIVILIAN NONINSTITUTIONALIZED POPULATION; Under 18 years; With a disability"                                         ""          
      "CDP02_19moe"    "Number; Margin of Error; DISABILITY STATUS OF THE CIVILIAN NONINSTITUTIONALIZED POPULATION; Under 18 years; With a disability"                                  ""          
      "CDP02_19pct"    "Percent; Estimate; DISABILITY STATUS OF THE CIVILIAN NONINSTITUTIONALIZED POPULATION; Under 18 years; With a disability"                                        "CDP02_18est"
      "CDP02_19pctmoe" "Percent; Margin of Error; DISABILITY STATUS OF THE CIVILIAN NONINSTITUTIONALIZED POPULATION; Under 18 years; With a disability"                                 ""          
      "CDP02_20est"    "Number; Estimate; RESIDENCE 1 YEAR AGO; Population 1 year and over"                                                                                             ""          
      "CDP02_20moe"    "Number; Margin of Error; RESIDENCE 1 YEAR AGO; Population 1 year and over"                                                                                      ""          
      "CDP02_21est"    "Number; Estimate; RESIDENCE 1 YEAR AGO; Population 1 year and over; Same house"                                                                                 ""          
      "CDP02_21moe"    "Number; Margin of Error; RESIDENCE 1 YEAR AGO; Population 1 year and over; Same house"                                                                          ""          
      "CDP02_21pct"    "Percent; Estimate; RESIDENCE 1 YEAR AGO; Population 1 year and over; Same house"                                                                                "CDP02_20est"
      "CDP02_21pctmoe" "Percent; Margin of Error; RESIDENCE 1 YEAR AGO; Population 1 year and over; Same house"                                                                         ""          
      "CDP02_22est"    "Number; Estimate; RESIDENCE 1 YEAR AGO; Population 1 year and over; Different house in the U.S."                                                                ""          
      "CDP02_22moe"    "Number; Margin of Error; RESIDENCE 1 YEAR AGO; Population 1 year and over; Different house in the U.S."                                                         ""          
      "CDP02_22pct"    "Percent; Estimate; RESIDENCE 1 YEAR AGO; Population 1 year and over; Different house in the U.S."                                                               "CDP02_20est"
      "CDP02_22pctmoe" "Percent; Margin of Error; RESIDENCE 1 YEAR AGO; Population 1 year and over; Different house in the U.S."                                                        ""          
      "CDP02_23est"    "Number; Estimate; RESIDENCE 1 YEAR AGO; Population 1 year and over; Different house in the U.S.; Same county"                                                   ""          
      "CDP02_23moe"    "Number; Margin of Error; RESIDENCE 1 YEAR AGO; Population 1 year and over; Different house in the U.S.; Same county"                                            ""          
      "CDP02_23pct"    "Percent; Estimate; RESIDENCE 1 YEAR AGO; Population 1 year and over; Different house in the U.S.; Same county"                                                  "CDP02_20est"
      "CDP02_23pctmoe" "Percent; Margin of Error; RESIDENCE 1 YEAR AGO; Population 1 year and over; Different house in the U.S.; Same county"                                           ""          
      "CDP02_24est"    "Number; Estimate; RESIDENCE 1 YEAR AGO; Population 1 year and over; Different house in the U.S.; Different county"                                              ""          
      "CDP02_24moe"    "Number; Margin of Error; RESIDENCE 1 YEAR AGO; Population 1 year and over; Different house in the U.S.; Different county"                                       ""          
      "CDP02_24pct"    "Percent; Estimate; RESIDENCE 1 YEAR AGO; Population 1 year and over; Different house in the U.S.; Different county"                                             "CDP02_20est"
      "CDP02_24pctmoe" "Percent; Margin of Error; RESIDENCE 1 YEAR AGO; Population 1 year and over; Different house in the U.S.; Different county"                                      ""          
      "CDP02_25est"    "Number; Estimate; RESIDENCE 1 YEAR AGO; Population 1 year and over; Different house in the U.S.; Different county; Same state"                                  ""          
      "CDP02_25moe"    "Number; Margin of Error; RESIDENCE 1 YEAR AGO; Population 1 year and over; Different house in the U.S.; Different county; Same state"                           ""          
      "CDP02_25pct"    "Percent; Estimate; RESIDENCE 1 YEAR AGO; Population 1 year and over; Different house in the U.S.; Different county; Same state"                                 "CDP02_20est"
      "CDP02_25pctmoe" "Percent; Margin of Error; RESIDENCE 1 YEAR AGO; Population 1 year and over; Different house in the U.S.; Different county; Same state"                          ""          
      "CDP02_26est"    "Number; Estimate; RESIDENCE 1 YEAR AGO; Population 1 year and over; Different house in the U.S.; Different county; Different state"                             ""          
      "CDP02_26moe"    "Number; Margin of Error; RESIDENCE 1 YEAR AGO; Population 1 year and over; Different house in the U.S.; Different county; Different state"                      ""          
      "CDP02_26pct"    "Percent; Estimate; RESIDENCE 1 YEAR AGO; Population 1 year and over; Different house in the U.S.; Different county; Different state"                            "CDP02_20est"
      "CDP02_26pctmoe" "Percent; Margin of Error; RESIDENCE 1 YEAR AGO; Population 1 year and over; Different house in the U.S.; Different county; Different state"                     ""          
      "CDP02_27est"    "Number; Estimate; RESIDENCE 1 YEAR AGO; Population 1 year and over; Abroad"                                                                                     ""          
      "CDP02_27moe"    "Number; Margin of Error; RESIDENCE 1 YEAR AGO; Population 1 year and over; Abroad"                                                                              ""          
      "CDP02_27pct"    "Percent; Estimate; RESIDENCE 1 YEAR AGO; Population 1 year and over; Abroad"                                                                                    "CDP02_20est"
      "CDP02_27pctmoe" "Percent; Margin of Error; RESIDENCE 1 YEAR AGO; Population 1 year and over; Abroad"                                                                             ""          
      "CDP02_28est"    "Number; Estimate; PLACE OF BIRTH; Total population"                                                                                                             ""          
      "CDP02_28moe"    "Number; Margin of Error; PLACE OF BIRTH; Total population"                                                                                                      ""          
      "CDP02_29est"    "Number; Estimate; PLACE OF BIRTH; Total population; Native"                                                                                                     ""          
      "CDP02_29moe"    "Number; Margin of Error; PLACE OF BIRTH; Total population; Native"                                                                                              ""          
      "CDP02_29pct"    "Percent; Estimate; PLACE OF BIRTH; Total population; Native"                                                                                                    "CDP02_28est"
      "CDP02_29pctmoe" "Percent; Margin of Error; PLACE OF BIRTH; Total population; Native"                                                                                             ""          
      end
      Last edited by Jay Philbrick; 07 Apr 2022, 09:48.

      Comment


      • #4
        Thanks for the extra data example.

        I presume there is no point in working with variable names and labels unless there is a data file somewhere with the data strict sense. So I would just

        Code:
         merge 1:1 _n
        with the data and then loop over the observations with variable labels and assign new variable labels.

        But as you know what you show in #3 is problematic: what is offered as a variable label is often far too long to be assigned as such. That is what you are addressing in #1 but

        1. The syntax for subinstr() requires 4 arguments not 3. That was the immediate bug.

        2. More generally, there would need to be editing steps such as

        Code:
        replace vlabel = subinstr(vlabel, "Estimate; ", "", .)
        3. I can't see any issue with quotation marks.

        4. Transposing this makes the problem more difficult. I am touched that an old command of mine -- sxpose from SSC -- is disinterred here but it's not needed or helpful.

        5. Accordingly I can't see any need for local macros here.

        Comment

        Working...
        X