Announcement

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

  • cap file commands

    Hi all

    I have a problem with the following code (I got it by a colleague...):

    use "${logpath}\countryfile_atk_webstats_all", clear

    * write header to the output file
    cap file close webstatsfile
    file open webstatsfile using ${outputfile2}, write text replace ----> the error is in this line
    file write webstatsfile "Web Statistics - Atkinson Index"
    file close webstatsfile

    noi di in b "Generating tables for Baseline report in ${outputfile}"
    save "${logpath}\temp_atk1.dta", replace

    I runned it, studied the various commands, but I can find the bug...

    Thank you a lot!

  • #2
    Your code references global variables $logpath and $outputfile2 but you do not define them using the global command.

    Comment


    • #3
      Ok, doing the adjustments you suggested it still not works

      Comment


      • #4
        Then something else must be wrong. In what little you told us of your code the undefined global variables were obvious.

        The more you help others understand your problem, the more likely others are to be able to help you solve your problem.

        Section 12.1 of the Statalist FAQ linked to from the top of the page is particularly pertinent

        12.1 What to say about your commands and your problem

        Say exactly what you typed and exactly what Stata typed (or did) in response. N.B. exactly!
        ...
        Never say just that something "doesn't work" or "didn't work", but explain precisely in what sense you didn't get what you wanted.

        Comment


        • #5
          Hi William

          Ok, I will try to be more precise.
          I am checking an old code (1500 lines). The code should produce some Atkinson table and statistics that have to be reported on our website (i.e. webstatfiles in the code).



          global projectfolder = "C:\Users\utente\Desktop\09 - Webstatistics\2016-2019"
          global datapath = "${projectfolder}\input"
          global dofilepath = "${projectfolder}\do-files"
          global logpath = "${projectfolder}\log"

          global outputfile2 = "${savepath}\base_line_statistics_atk_2.csv"

          use "${logpath}\countryfile_atk_webstats_all", clear

          cap file close webstatsfile
          file open webstatsfile using ${outputfile2}, write text replace
          file write webstatsfile "Web Statistics - Atkinson Index"
          file close webstatsfile

          noi di in b "Generating tables in ${outputfile}"
          save "${logpath}\temp_atk1.dta", replace


          When I run the code, Stata produces the error message:

          file open webstatsfile using ${outputfile2}, write text replace
          invalid '-'
          r(198);

          I hope to be sufficiently precise. If you need more information, I will try to add all of necessary.

          Comment


          • #6
            You do not show us the definition of the global variable $savepath, but we can guess it includes $projectfolder, which has spaces in it. So your file path has spaces, and you must enclose your file path in quotation marks
            Code:
            file open webstatsfile using "${outputfile2}", write text replace
            as you did in
            Code:
            global outputfile2 = "${savepath}\base_line_statistics_atk_2.csv"
            and
            Code:
            save "${logpath}\temp_atk1.dta", replace
            Last edited by William Lisowski; 11 Sep 2020, 05:29.

            Comment

            Working...
            X