Announcement

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

  • Create a variable that contains the name of the datafile

    Dear Stata community,

    I am struggling with a probably simple operation. I have data that contains the date and number of users holding a stock. The stock ticker however is not a variable, but part of the filename (e.g. "A.dta").

    Now I want to create a ticker variable that contains the filename without the ending (e.g. "A").

    Has anyone a hint on how I can achieve this?

    Best regards!



    PS: Ideally I want this step to be a part of a script that runs through a whole folder, as I have thousands of separate stock files.


  • #2


    See the results of creturn list -- which point to one solution, namely

    Code:
    gen filename = subinstr("`c(filename)'", ".dta", "", 1)

    Comment


    • #3
      Thank you Nick!

      May it be the case, that this only works, if your code is put directly after loading the file?

      Because when I edit the dataset after loading, and only then try to generate the new variable, I get an empty column

      Comment


      • #4
        You can answer your own question. The result depends on a filename being present in c(filename) which you can check with a display. When I looked at your question my Stata has some sandbox data in that didn't come from a .dta file, but then I read in one such, and c(filename) is now populated.

        Code:
        . di "`c(filename)'"
        
        
        . sysuse auto, clear
        (1978 automobile data)
        
        . di "`c(filename)'"
        C:\Program Files\Stata18\ado\base/a/auto.dta
        That said, editing the data shouldn't make a difference. (I just checked and it doesn't.)

        Comment


        • #5
          If you are reading in data from a large number of files in a folder, don't you already have a macro with the name of the file in it?

          I would imagine some code of the following type:

          Code:
          local data_dir "path/to/data/folder"
          local data_files: dir `"`data_dir'"' files "*.dta"
          
          foreach this_file of local data_files {
          
              use `"`data_dir'/`this_file'"', clear
              ...
          }
          In this case, you don't need the macro c(filename) at all, that purpose is served by the macro this_file in the above toy code. The macro is available while you're within the foreach loop, but doesn't need the ticker data file to actually be in memory.

          Comment

          Working...
          X