Announcement

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

  • Importing data conditional on something

    Hi everyone!

    I am facing the following problem:
    I want to import an excel that is created by another do-file.

    However, in days when I don't receive new data, this excel is not created.
    Therefore, if I simply try to run:

    import excel "this excel - ${today}.xlsx" , ///
    firstrow clear


    I will receive an error all the days that I haven't received data (which means there would be no excel for ${today}.

    Is there any way I can conditional the import of the excel on the existence of the file?
    For example, with an "IF"?

    Thanks in advance!!
    Isabela

  • #2
    Perhaps this untested example code will start you in a useful direction.
    Code:
    if fileexists("this excel - ${today}.xlsx") == 0 {
        display "no data for you today!"
        local newdata = 0
    }
    else {
        import excel "this excel - ${today}.xlsx" , firstrow clear
        local newdata = 1
    }

    Comment


    • #3
      Hey Isabela, welcome to Statalist.

      I've actually ran into this issue before (pretty recently I think), where I download a dataset (CDC Data, was it?) using Python. I couldn't have a fixed name for it because the file name changed by the day, so it would be like 090921-blah-blah, 091021-blah-blah...

      To circumnavigate this, building on the code above, I would do something like

      Code:
      local files : dir "`c(pwd)'" files "*.xlsx"
      
      cap conf f `files'
       if _rc ~= 0 {    
      
      display "Nope! No data for you today!"
      
      }
      
      else {    
      
      import exc "`files'" , firstrow clear
      
      display "Good job!!!!"
      
      }
      I will be upfront that my code assumes a few things: That you're always importing an xlsx file, and that the global macro $today you made is not relevant elsewhere in your programs. Additionally, I presume that you have no other files with the similar extension in your working directory at the same time. Isabela Campos
      Last edited by Jared Greathouse; 09 Sep 2021, 12:32.

      Comment

      Working...
      X