Announcement

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

  • Replacing "&" with just an ampersand "&"

    Hi,

    I am looping across several files to clean some datasets imported from Excel. I want to match each of my firm names with a mapping table so I can get their ID. However, some firm names have this expression ""&" instead just a normal ampersand, so the match won't perform correctly.

    I tried this code:

    Code:
    foreach si of local site {
    foreach file in `files_`si'' {
    
    foreach var of varlist name* {
    local amper = strpos(`var[1]', "&")
    
    set tr on
    if `amper' > 0 {
    replace `var' = subinstr(`var', "&", "&", .)
    set tr off
    }
    
    tempfile `file'_CLEAN
    save ``file'_CLEAN'
    }
    Now it seems to work for *some* files because the tracer gave me this result when the code was running :

    - else if `amper' > 0 {
    = else if 232 > 0 {
    replace `var' = subinstr(`var', "&", "&", .)
    }

    However when I try to open the first of my many files (just out of curiosity, because I knew there was a problem like this in the first file)

    Code:
    use `file1_CLEAN', clear
    dataex name16, count(1)
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str36 name16
    "xxxxxxx & xxxxxx"
    end
    What did I do wrong here?
    Last edited by Adam Sadi; 24 Jan 2023, 03:20.

  • #2
    `var[1]' isn't going to be interpreted as `var'[1]

    But the intent of that isn't clear. You are trying just to look at the first observation. Is that going to be enough scrutiny?

    The code

    Code:
    replace `var' = subinstr(`var', "&", "&", .)
    won't do any harm if no occurrences are found, so you don't need to check beforehand whether you need it.

    Comment


    • #3
      Dear Nick:

      Indeed, I am trying to look at the first observation because I want to name my variables from the firms' IDs and in order to do so, I must replace the *correctly written* firm names with their ID first. And it's a wide format hence the many name?? variables.

      Even after I corrected the typo you mentioned in the first line of #1, the code still didn't work until I removed the -if- programming command as you advised. By any chance, do you know why is that? Thank you for solving my problem!

      Comment


      • #4
        Sorry, but I can't add helpfully to my previous comment.

        On the face of it

        Code:
        `var[1]'
        shouldn't work but the implication of #1 is that it does. On the other hand your code blocks don't include any else statements but the implication of #1 is that there is at least one.

        Perhaps you are using different versions of the code and/or not showing us all the code, and that's likely to confuse readers in practice, although not on purpose.

        Comment

        Working...
        X