Announcement

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

  • Escaping double quotes leads to issue with semicolon

    Hello,

    I'm very new to stata and i'm trying to define a label but i'm running into an issue with double quotes. I can get around the double quotes by wrapping that specific row with ` and ' at the end.
    The issue then becomes the semicolon it will cut the string off if there is a semicolon in the string.

    example
    label define lblTemp
    0 "This is a test"
    1 `"This person is a ""Test"" only"'
    2 `"This person is a ""Test"" only; Unless there are more people then he will be ""TEST2"""'
    ;

    label values VarTest lblTemp

    What will happen is 1 and 2 will have the same value because it is cut off at the semicolon. How can I get around this? I want to show it exactly like it is between the first and last double quote.

    Thank you


  • #2
    Maybe not the solution you want but I would simply drop the use of #delimit ;, which you seem to be using but don't comment on. Instead, use /// to cut long lines. An example:

    Code:
    label drop _all
    
    label define lblTemp ///
    0 "This is a test" 1 `"This person is a "Test" only"' ///
    2 `"This person is a "Test" only; Unless there are more people then he will be "TEST2" "'
    
    label list
    See help #delimit for details and more options.
    You should:

    1. Read the FAQ carefully.

    2. "Say exactly what you typed and exactly what Stata typed (or did) in response. N.B. exactly!"

    3. Describe your dataset. Use list to list data when you are doing so. Use input to type in your own dataset fragment that others can experiment with.

    4. Use the advanced editing options to appropriately format quotes, data, code and Stata output. The advanced options can be toggled on/off using the A button in the top right corner of the text editor.

    Comment


    • #3
      Hi Roberto,

      I can't change the delimiter. The program i'm generating is part of a bigger program that uses Semi-colon. Can I change the delimiter to /// then reset it back to semicolon at the end of my code?

      Thank you

      Comment


      • #4
        yes, try looking at the help file (h delimit)

        Comment


        • #5
          Originally posted by jerry8989 View Post
          The program i'm generating is part of a bigger program that uses Semi-colon. Can I change the delimiter to /// then reset it back to semicolon at the end of my code?
          The phrase "part of a bigger program" suggests that you should probably be breaking the code up into multiple do-files, for readability and maintainability. The preprocessor command #delimit is specific to the do-file in which it is called, meaning that you can set it at the beginning of a do-file and Stata will automatically revert back to the previous setting when the do-file terminates. Thus, my suggestion would be to put the value label definitions into a separate do-file; note that if you do this, you don't even have to use #delimit because Stata by default will use a CR.

          Switching back and forth between different #delimit settings within the same do-file increases the chance of syntax errors, and will make your code difficult to read.

          Comment


          • #6
            Phil,

            I do have my value labels in a separate do file. I did remove the #delimit line and now have the following working. I had instances were I had a double quote and a single quote in the same label. It doesn't look right in the editor but it works so far.

            Code:
            label define lblTemp ///
            0 "This is a test" 1 `"This person is a "Test's" only"' ///
            2 `"This person is a "Test's" only; Unless there are more people then he will be "TEST2" "'
            Can I use a range when setting the label define? for example if I have values 1-5 that I want set to 'LOW' without having to have a row for each?

            Thank you

            Comment


            • #7
              The -label define- command does not accept groups or ranges of values. That doesn't prevent you from writing a loop over those values, though:

              Code:
              forvalues n = 1/5 {
                   label define label_name `n' "LOW", modify
              }

              Comment


              • #8
                Thank you Clyde for your response.

                What if my code values range from 1-20.
                1-5 is low
                6-10 is med
                11-20 is high

                They are all for the same variable. Would I have to create a forvalues for each? How can I have them be the same label_name so I can set this formatting to my variable?

                Thank you

                Comment


                • #9
                  Probably 3 loops is simplest:

                  Code:
                  forvalues n = 1/5 {
                       label define label_name `n' "LOW", modify
                  }
                  forvalues n = 6/10 {
                       label define label_name `n' "MED", modify
                  }
                  forvalues n = 11/20 {
                       label define label_name `n' "HIGH", modify
                  }
                  label values variable_name label_name

                  Comment


                  • #10
                    Thank you so much I will try that and report my results.

                    Comment


                    • #11
                      Originally posted by jerry8989 View Post
                      Thank you Clyde for your response.

                      What if my code values range from 1-20.
                      1-5 is low
                      6-10 is med
                      11-20 is high

                      They are all for the same variable. Would I have to create a forvalues for each? How can I have them be the same label_name so I can set this formatting to my variable?

                      Thank you

                      Some packages allow placing a label on a range, for example CSPro. SPSS allows tagging a certain range as missings. Stata does not allow this.

                      Avoid assigning individual labels to values in such a case. What happens is in fact you are talking about 3 categories, low, med and high, each covering a certain range of values. So create this categories explicitly, and assign the labels:

                      Code:
                      recode x (1/5=1 "low") (6/10=2 "med") (11/20=3 "high"), generate(cat)
                      tabulate cat
                      Best, Sergiy Radyakin

                      Comment


                      • #12
                        I agree that Sergiy's approach is better than my proposal.

                        Comment

                        Working...
                        X