Announcement

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

  • folders command and import delimited returns invalid

    Hi,

    I have been trying to retrieve the most recent folder out of a number of folders named by dates to set the path for an import delimited but even though displaying my saved local macro works, import delimited returns invalid '2022.02.02' when I use this local macro. Does anyone know what I am doing wrong? Please see my code below.

    Thanks,
    Nicolas

    Code:
    folders "../data/"
    local date = word(r(folders), -1)
    display `date'
    
    import delimited using "../data/`date'/stuff.csv", clear

  • #2
    I believe the value of your local macro date is surrounded in quotation marks. The following example may point you in a helpful direction.
    Code:
    . macro list _date
    _date:          "2022.02.02"
    
    . display `date'
    2022.02.02
    
    . display `" import delimited using "../data/`date'/stuff.csv", clear "'
     import delimited using "../data/"2022.02.02"/stuff.csv", clear 
    
    . local date `date'
    
    . display `" import delimited using "../data/`date'/stuff.csv", clear "'
     import delimited using "../data/2022.02.02/stuff.csv", clear

    Comment


    • #3
      Thanks a lot William. That was the issue. So for others that might have my problem, like William did, the solution is to add an extra step such that you have:
      local date = word(r(folders), -1)
      local date `date'

      and using macro list _date is much better than using display `date'.

      Comment


      • #4
        For the benefit of others who are wondering, like I was: folders in #1 is a community-contributed command, available from SSC:
        Code:
        ssc install fs
        An inbuilt option would have been:
        Code:
        local folders: dir "../data/" dirs *
        local date: word `:list sizeof folders' of `folders'
        That would also obviate the need for the extra step
        Code:
        local date `date'
        Last edited by Hemanshu Kumar; 21 Oct 2022, 22:10.

        Comment


        • #5
          Thanks for #4. I wondered what folders was -- some official command I didn't know? -- and now I see that it is part of a package fs that I wrote in 2006. The moral of that is

          Please explain where community-contributed commands come from -- because even command authors who just by chance are reading your thread may not recognise their own offspring.

          Comment

          Working...
          X