Announcement

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

  • How to drop last 0

    I have a dummy dataset that looks like the following:


    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str7 varlist
    "A111110"
    "A11110"
    "A1110"  
    "A110"   
    "A10"    
    "A10110"
    "A1010"  
    "A111000"
    "A1100"  
    end
    And I am trying to use the below code in order to drop the last 0 from each observation. I took this line of code from here: Stata | FAQ: Removing leading or trailing zeros from string variables

    replace varlist = usubstr(varlist, 1, ustrlen(varlist) − 1) if usubstr(varlist,−1,1) == "0"

    However when I run this I get the following error message:


    unknown function ()
    r(133);

    Any advice would be much appreciated.

    BW,
    Jenna



  • #2
    It works if you retype the expression. I think you got some exotic character that looks like a space but isn't a plain space tangled up in your code.

    Comment


    • #3
      Great, thank you Nick. That was an easier fix than expected.

      Comment


      • #4
        Has that code been anywhere near a word processor such as MS Word?

        Comment


        • #5
          No, I copied it straight from the website into Stata.

          Comment


          • #6
            NIck's hunch was in the right direction.

            The corrected code is below. The issue is that the minus signs were not the traditional ASCII character 45 [=char(45)], but a Unicode minus sign (\u2212). Stata will not interpret these characters synonymously, and thus gives an error.

            Code:
            replace varlist = usubstr(varlist, 1, ustrlen(varlist) - 1) if usubstr(varlist,-1,1) == "0"

            Comment


            • #7
              Leonardo Guizzetti thank you but I had it sorted already.

              Comment


              • #8
                So, when I look at the link in #1 it has my name on it!

                But what you're citing is a StataCorp edit. I didn't suggest or change the function names to what they are now (supporting Unicode too).

                StataCorp are generous about attributing work to people outside the company, but then company changes are not attributed even if your work is completely rewritten. I just wrote some text a while ago, with Jamie Griffin who I think asked a question on Statalist, and StataCorp did all the rest in terms of HTML mark-up and so forth.

                In short, any non-spaces were almost certainly introduced or produced somehow by a browser. The original authors did not intend to bite you, as you will appreciate.

                EDIT: Broadly consistent with #6 from Leonardo Guizzetti even if the details were quite wrong!
                Last edited by Nick Cox; 13 Dec 2022, 07:20.

                Comment


                • #9
                  Originally posted by Jenna Grabey View Post
                  Leonardo Guizzetti thank you but I had it sorted already.
                  Of course. My post was intended to help those future readers who may encounter a similar problem and come to the forum in search of a solution.

                  Comment

                  Working...
                  X