Announcement

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

  • Issue with loop over txt files

    Hi all,
    I have an issue with the following loop over text files, stored in the folder US companies and whose names are 1, 2, 3....up to 3290

    local files : dir "$path\US companies" files "*.txt"
    di `files'

    foreach file of local `files' {
    import delimited using "$path\US companies\`file'", varnames(1) delim("") bindquotes(loose) stripquotes(yes) clear
    keep companyname bvdidnumber nacerev2corecode4digits countryisocode postcode city latitude longitude guobvdidnumber duobvdidnumber
    foreach V of varlist _all {
    capture confirm string variable `V'
    if !_rc {
    replace `V' = ustrto(ustrnormalize(`V', "nfd"), "ascii", 2)
    replace `V' = ustrtrim(`V')
    replace `V' = stritrim(`V')
    replace `V' = strlower(`V')
    replace `V' = ustrlower(`V')
    }
    }

    save "$path\dta\US_companies\`file'.dta", replace
    }

    When I run the command the prompt returns me the following error:
    _"1.txt invalid name
    r(198);

    Do you have any idea of why the loop does not work?
    Best regards and thank you for any help

  • #2
    Give two dataexs that we can work with. I ain't at my computer cuz I'm in Puerto Rico and without my laptop, but make two dataex examples and save them as text files so others can work with them and someone can assist. It isn't immediately clear to me what the issue is




    EDIT: and please, put all code in CODE delimiters, not as regular text, it should be
    Code:
    like this
    Other edit: see this for more https://youtu.be/bXfaRCAOPbI
    Last edited by Jared Greathouse; 08 Aug 2022, 09:54.

    Comment


    • #3
      Here you go, I erased all the bits of the code that were not strictly necessary for the issue at hand, since I believe that the problem is within the creation of the local macro and the import delimited command:

      Code:
      local files : dir "$path\US companies" files "*.txt"
      
      foreach file of local `files' {
      import delimited using "$path\US companies\`file'", varnames(1) delim("") bindquotes(loose) stripquotes(yes) clear
      save "$path\dta\US_companies\`file'.dta", replace
      }
      Below a snapshot of how the files within the folder look like. Hope this helps to find a solution!

      Click image for larger version

Name:	Immagine 2022-08-08 182433.png
Views:	3
Size:	25.0 KB
ID:	1676930
      Attached Files

      Comment


      • #4
        No, this doesn't help at all. I asked for two data examples to work with, not for an attachment that literally nobody can work with. So please again, give two data examples. Using dataex, of how these files look when you import them into Stata. Export this dataex as a text file with export delimited, using the first two names of each file, and then somebody can help you with this. Francesco Tucci

        I'm in Puerto Rico now and won't be at my computer til 7pm tomorrow at earliest, but at least someone else may be able to assist in the meantime. No screenshots, no attachments, no pictures, none of that, all I'd ask of you is two data examples via dataex

        Comment


        • #5
          I don't think the contents of the text files matters for the error at hand.

          Try this
          Code:
          **should not be quotes around the name of the local macro since you specify that it's a local
          **and use forward slashes instead of backslashes in the path to avoid escaping the macro
          **(alternatively use two backslashes in front of macro)
          
          foreach file of local files {
          import delimited using "$path/US companies/`file'", varnames(1) delim("") bindquotes(loose) stripquotes(yes) clear
          save "$path/dta/US_companies/`file'.dta", replace 
           }
          More on backslashes and macros here:
          https://www.stata.com/support/faqs/p...es-and-macros/

          Comment


          • #6
            Thank you very much Sarah, that works perfectly!!

            Comment


            • #7
              No the contents don't matter at all, but this misses my bigger point. In order for us to give good technical support, we have to have a reproducible example of the error. In this case, the contents of the files don't matter they could have a billion lines of gobbledygook for all it's worth, the point is that we shouldn't have to make our own toy example, all OPs (including myself) should provide data examples or small file examples, unless the question is literally something like
              why won't
              Code:
              BREAK
              work. To me, reproducibility is the name of the game here, and in 97% of questions we get here, full examples should be given whenever possible.

              Comment


              • #8
                Jared, there is literally no way for a questioner to give us a reproducible example here because of the nature of the problem. Putting something into dataex wouldn't help because the question was specifically about pulling directly contents into a macro and using that macro.

                Someone paying close attention probably could have spotted the error in the code without having to work through it. I'll admit that despite 20 years of programming in Stata I still had to make myself a sandbox example to figure out exactly where the error was, though it was clear to me from the error message that the problem was coming from how the macro in the import line was being read. A data example in dataex would have provided some additional context of what was in the .txt files, but it's not necessary context to answer the question.

                I agree completely that clear questions with sufficient information are important. Learning to write those questions is a skill and I am deeply grateful to the folks here (and from the former statalist email list) who guided me in learning how to write clearer questions. That said, tone matters. And I would argue that in this case the fact that the contents of the .txt files don't matter means that berating the poster for not providing them is inappropriate and not a useful way to teach people to ask better questions.

                Comment

                Working...
                X