Announcement

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

  • saving temp file in working directory

    Dear All,

    I have created a temporary file based on certain condition and want to save it to the current directory by using the following command.

    Although there is no error in the code but I am not able to locate the saved file.

    Looking forward to your suggestion

    I have attached the do file and data file for your reference.

    With sincere regards,
    Upananda Pani
    Attached Files

  • #2
    Better to post code than attachments. Some people, including me, are reluctant to open attachments sent by strangers via the internet. All this is to say, I did not look at the code.

    A temporary file in Stata, as the name suggests, is temporary in that it is automatically deleted once the session/program/do-file/namespace that created it concludes. Perhaps this is why you cannot locate it? Or, perhaps you are looking for files with a .dta extension; temporary files in Stata have extension .tmp.

    Comment


    • #3
      Stata -tempfile-s are not saved in the current working directory. They are saved in a directory for temporary files used by all applications and provided by your operating system. To access a Stata tempfile, you need only refer to it by the name you gave it: you do not need to know what directory it is stored in.

      Nevertheless, if you are curious to know where it is, you can do something like this:
      Code:
      tempfile my_temporary_file
      display `"`my_temporary_file'"'
      Please note that attachments are discouraged on this forum and should be used only when there is no viable alternative. Example data is best given by using the -dataex- command. And code should be pasted directly into the Forum editor, wrapped in code-delimiters. Please see Forum FAQ #12 for additional details.

      If you are running version 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

      Added: Crossed with #2. I, too, do not open attachments from people I do not know. I assumed that your question is about working with a temporary file within the scope of the program that creates it. Once that program has completed execution, as Daniel Klein points out, it goes out of existence and you cannot access it at all. Indeed, that is the entire point of -tempfile-s is that they do not persist when they are no longer needed, so you do not have to remember to clean them up when you are done.
      Last edited by Clyde Schechter; 21 Jul 2022, 10:29.

      Comment


      • #4
        Hi,

        Many Many Thanks to Daniel and Clyde for your reply. I am appending the code below.

        Code:
        *Dealing with multiple events:
        *1: Finding out how many events there are for each firm
        use acq_dates.dta, clear
        sort iperm date_announce
        bysort iperm: gen eventcount=_N
        /*2: Cutting the dataset down to just one observation for each firm.
        Each firm observation is associated with the count of event dates
        for that firm. Then, saving this as a new dataset */
        keep iperm eventcount
        tempfile eventcount
        quietly save `eventcount', replace
        clear
        Here I am saving the temporary file. I will look it again in the temporary folder. Appended the code incase you want to have a look at it.

        Regards,
        UP

        Comment


        • #5
          So this is how you save the file. We cannot see the environment in which you do so. Is this code in a do-file? Is this code inside a program? And, most importantly, your problem appears to be locating the file. You do not show us the code of how you tried that.
          Last edited by daniel klein; 21 Jul 2022, 20:46.

          Comment


          • #6
            this code is in a do file. Actually I am trying to merge two data files based on the eventcount criteria.

            Comment


            • #7
              OK. Actually, as Clyde has pointed out, there is no need to know the location of the temporary files as you can simply refer to the local macro; that is: merge will be able to handle the temporary file in the same way as save, where you do not care about its location either. As you are still not showing us the relevant code, I am bailing out here.

              Comment


              • #8
                Thanks Daniel. I do agree with you.

                Comment

                Working...
                X