Announcement

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

  • How to preserve extra spaces in a local?

    Hi everyone,

    My question is about how to preserve extra spaces in a local. I have a local as follows:

    Code:
    local Lab ""Abc tests done on patients" "Abc  Pregnacy tests done""
    (Note that the 2nd item in the local has extra spaces - I want to preserve these.)

    However, the local seems to be saving this item without the extra spaces:
    Code:
    . macro list _Lab
    _Lab:           "Abc tests done on patients" "Abc Pregnacy tests done"
    This is an issue, as I later want to replace an observation if it's value is "Abc Pregnacy tests done" (the observation has a double space).

    Does anyone know how I can preserve the double space in the local? Many thanks in advance!



  • #2
    This is very odd. On the one hand I can reproduce your result. But, if I -display- the macro, or if I check the string length of the second word of `Lab', Stata does exhibit the presence of the extra blank space. This may be some peculiarity (bug?) in -macro list-.

    Code:
    . clear*
    
    .
    . local Lab ""Abc tests done on patients" "Abc  Pregnacy tests done""
    
    .
    . macro list _Lab // EXTRA BLANK IS MISSING
    _Lab:           "Abc tests done on patients" "Abc Pregnacy tests done"
    
    .
    . display `"`Lab'"' // EXTRA BLANK IS NOT MISSING
    "Abc tests done on patients" "Abc  Pregnacy tests done"
    
    .
    . display strlen("Abc  Pregnacy tests done")
    24
    
    .
    . display strlen(`"`:word 2 of `Lab''"')
    24
    I would consider reporting this to Tech Support at StataCorp. I don't see anything in the help file or documentation that explains this behavior.

    Comment


    • #3
      You should probably be using compound quotes, as in

      Code:
      local Lab `""Abc tests done on patients" "Abc  Pregnacy tests done""'
      However even with compound quotes, I get

      Code:
      . macro list
      [...]
      _Lab:           "Abc tests done on patients" "Abc Pregnacy tests done"
      This seems to be a glitch in macro list as I also get

      Code:
      . display `"`Lab'"'
      "Abc tests done on patients" "Abc  Pregnacy tests done"
      
      . mata : st_local("Lab")
        "Abc tests done on patients" "Abc  Pregnacy tests done"
      I am using Stata 16.1, fully updated, on a Win 10 system.


      EDIT: Clyde was quicker along the same path.

      Comment


      • #4
        Thanks so much, Clyde and Daniel!

        I ultimately need to run a loop which goes through the strings in the local and replaces a variable if the variable matches the string in the local (simplified version of code below -

        Code:
         local Lab ""Abc tests done on patients" "Abc  Pregnacy tests done"" local Other ""X" "Y"" local services Lab Other
        
            foreach ser of local services {
                forvalues n = 1/`number' {
                    loc s: word `n' of ``ser''
                    replace A="`ser'`n'" if A=="`s'"
                    }    
                }

        However, because the local wasn't preserving the extra space, the replacement wasn't happening when my variable A was = "Abc Pregnacy tests done" (with the extra space -- Edit: funnily enough even Statalist is removing the extra space!). I instead used subinstr to remove extra spaces from variable A.
        Last edited by Harshita Joshi; 17 Dec 2021, 01:09.

        Comment


        • #5
          You can make use of the functions -strtrim- and -stritrim- and avoid including spaces in the local.

          Code:
          di strtrim(stritrim("   Abc      Pregnacy   tests done   " ))
          Res.:

          Code:
          . di strtrim(stritrim("   Abc      Pregnacy   tests done   " ))
          Abc Pregnacy tests done
          See

          Code:
          help strtrim()
          help stritrim()

          Comment


          • #6
            Re #4. I think you misunderstood the thrust of my response in #2 (and, at the risk of putting words in his mouth, Dan Klein's at #3). What we showed is that, in fact, the local macro was intact and does include the extra spaces. It was just being improperly shown by -macro list-. If your code was not working, then there is some other reason for it--not the mismatch of blank spaces between the variable and the macro.

            That said, I do agree that it makes more sense to clean up the string variable and eliminate superfluous blanks, as suggested by Andrew Musau in #5.

            Comment

            Working...
            X