Announcement

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

  • Unbalancing parentheses

    Dear all,

    I am wondering where the unbalanced parentheses are in my line of commands below. The error is "parentheses do not balance". I have really tried redoing the codes but I am not still sure what's going on.

    Code:
    use "Output/Reshaped1.dta", clear
    
    local vars v106 v190 v151 v3a13 v249 v217
    local labels HighestEducLevel Wealth Sex_HouseholdHead UsedEmergContraPast12Months AgeMenarche knowledgeOvulCycle
    local titles "Teenage Births by Highest Education Level" ///
                 "Teenage Births by Wealth quintiles" ///
                 "Teenage Births by Sex of Household Head" ///
                 "Teenage Births by Use of Contraceptives" ///
                 "Teenage Births by Age at Menarche" ///
                 "Teenage Births by Knowledge of Ovulatory Cycle"
    local outputs "Bar_BirthsbyEduc.pdf" "Bar_BirthsbyWealth.pdf" "Bar_BirthsbySexHHhead.pdf" ///
                  "Bar_BirthsbyContraceptiveUsage.pdf" "Bar_BirthsbyAgeMenarche.pdf" "Bar_BirthsbyKnowOvulCycle.pdf"
    local ylabels "0(100)1000" "0(100)900" "0(100)1500" "0(100)1100" "0(25)275" "0(50)400"
    
    local i = 1
    
    foreach var of local vars {
        use "Output/Reshaped1.dta", clear
    
        * Collapse the dataset by summing up the count of teenage births by outcome_year and the variable of interest
        collapse (sum) year_, by(outcome_year `var')
        
        rename year_ TeenageBirths
        local label : word `i' of `labels'
        rename `var' `label'
    
        * Apply any special conditions
        if "`var'" == "v249" {
            keep if `label' >= 9 & `label' <= 19
        }
    
        * Generate the graph
        local title : word `i' of `titles'
        local output : word `i' of `outputs'
        local ylabel : word `i' of `ylabels'
    
        graph bar (sum) TeenageBirths, over(`label') ///
            plotregion(style(none)) ///
            ysize(15) ///
            title("`title'", size(medium)) ///
            ytitle("Teenage births") ///
            ylabel(`ylabel') ///
            note("Note: Data restriction: 2017 to 2021")
    
        graph export "Output/`output'", replace
    
       local i = `i' + 1
    }

  • #2
    I guess the problem lurks otherwise given improper use of quotation marks.

    The quotation marks in the definition of locals outputs and ylabels should just be deleted. They aren't needed and don't help given the problem I now explain.

    Consider the local macro

    Code:
     local titles "Teenage Births by Highest Education Level" ///              "Teenage Births by Wealth quintiles" ///              "Teenage Births by Sex of Household Head" ///              "Teenage Births by Use of Contraceptives" ///              "Teenage Births by Age at Menarche" ///              "Teenage Births by Knowledge of Ovulatory Cycle"
    Stata's rule is that the outermost quotation marks should be stripped as delimiters, So you are left with this definition

    Code:
    Teenage Births by Highest Education Level" "Teenage Births by Wealth quintiles" "Teenage Births by Sex of Household Head" "Teenage Births by Use of Contraceptives" "Teenage Births by Age at Menarche" "Teenage Births by Knowledge of Ovulatory Cycle
    which isn't what you want. You need compound double quotes around the entire beast, so that when they get stripped, you have what you want.

    Code:
     local titles `" "Teenage Births by Highest Education Level" ///              "Teenage Births by Wealth quintiles" ///              "Teenage Births by Sex of Household Head" ///              "Teenage Births by Use of Contraceptives" ///              "Teenage Births by Age at Menarche" ///              "Teenage Births by Knowledge of Ovulatory Cycle" "'

    Comment


    • #3
      Those were the days, my friend ....
      Last edited by Nick Cox; 17 Oct 2024, 07:06.

      Comment


      • #4
        Do you know the old joke about the physicist, the chemist, and the economist? Of course you do.

        (Just filler; accidentally posted three times.)

        Comment


        • #5
          Thank you Nick Cox but it actually gives an "invalid syntax" error just at the beginning of the local titles:
          Code:
          local titles `" "Teenage Births by Highest Education Level" ///
                       "Teenage Births by Wealth quintiles" ///
                       "Teenage Births by Sex of Household Head" ///
                       "Teenage Births by Use of Contraceptives" ///
                       "Teenage Births by Age at Menarche" ///
                       "Teenage Births by Knowledge of Ovulatory Cycle" "'
          local outputs Bar_BirthsbyEduc.pdf Bar_BirthsbyWealth.pdf Bar_BirthsbySexHHhead.pdf ///
                        Bar_BirthsbyContraceptiveUsage.pdf Bar_BirthsbyAgeMenarche.pdf Bar_BirthsbyKnowOvulCycle.pdf
          local ylabels 0(100)1000 0(100)900 0(100)1500 0(100)1100 0(25)275 0(50)400

          Comment


          • #6
            I think the principle is right. Sometimes exotic mark-up characters get carried from place to place within this forum that Stata can't digest if you copy and paste. At worst you need to retype it until it works.

            Consider this (irrelevant material edited out)

            Code:
            . local beasts `" "this frog" "that toad" "your dragon" "'
            
            . mac li
            
            _beasts:         "this frog" "that toad" "your dragon"

            Comment


            • #7
              Actually, I think there is a problem with the material within -`" "'- being broken up acoss several lines, separated by ///. (And if I am right, then this is, I believe, a bug.)

              While Nick's example works just fine, if you attempt to separate the items onto different lines:
              Code:
              local beasts `" "this frog" ///
                  "that toad" ///
                  "your dragon" "'
              produces
              Code:
              local beasts `" "this frog" "that toad" "your dragon" "'
              
              .
              end of do-file
              
              . do "C:\Users\clyde\AppData\Local\Temp\STDc3c_000004.tmp"
              
              . local beasts `" "this frog" ///
              invalid syntax
              r(198);
              So you need to either put it all on one very long line (which I don't recommend, for readability reasons), or use a different way of separating lines. I tried using the trick of commenting out the line breaks with /* */, but that produces the same problem:

              Code:
              local beasts `" "this frog" /*
              */ "that toad" /*
              */ "your dragon" "'"
              which gives
              Code:
              . local beasts `" "this frog" /*
              invalid syntax
              r(198);



              Here's a workaround for your original that does the trick:

              Code:
              . #delimit ;
              delimiter now ;
              . local titles `" "Teenage Births by Highest Education Level"
              >              "Teenage Births by Wealth quintiles"
              >              "Teenage Births by Sex of Household Head"
              >              "Teenage Births by Use of Contraceptives"
              >              "Teenage Births by Age at Menarche"
              >              "Teenage Births by Knowledge of Ovulatory Cycle" "';
              
              . #delimit cr
              delimiter now cr
              . macro list _titles
              _titles:         "Teenage Births by Highest Education Level" "Teenage Births by Wealth quintiles" "Teenage Births by Sex of
                              Household Head" "Teenage Births by Use of Contraceptives" "Teenage Births by Age at Menarche" "Teenage Births by
                              Knowledge of Ovulatory Cycle"




              Comment

              Working...
              X