Announcement

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

  • Manage Macrolist

    Dear Stata Users,

    I compile a list of names of files in a directory:

    Code:
    local filenames: dir "${datapth}" files "prefix_*_*.dta"
    foreach file of local filenames {
        local quarters `quarters' substr("`file'", strlen("`file'")-9,6)
    }
    (I believe I cannot escape using the foreach loop here because substr is not an extended macro function, right?)

    This gives me a list of quarters like "2015Q4". Now, I am running into the following problems:
    • I want to delete duplicates in case, for example, "2015Q4" is contained more than once. This did not work:
      Code:
      local quarters: list uniq quarters
    • From the resulting list of unique quarters, I may want to drop a certain quarter. This did not work:
      Code:
      local quarters: list quarters - "2015Q4"
    I found the thread[*] interesting, but it did not work in my case. What am I doing wrong?

    Thank you very much.[*] https://stackoverflow.com/questions/...macro-in-stata

  • #2
    Originally posted by jhonny-econ View Post
    I compile a list of names of files in a directory:

    Code:
    local filenames: dir "${datapth}" files "prefix_*_*.dta"
    foreach file of local filenames {
    local quarters `quarters' substr("`file'", strlen("`file'")-9,6)
    }
    This gives me a list of quarters like "2015Q4".
    No, it does not. It gives you a list that looks something like

    Code:
    substr("prefix_..._....dta", strlen("prefix_..._....dta")-9, 6) substr("prefix_..._....dta", strlen("prefix_..._....dta")-9, 6) ...
    You need to store the results of the string functions into a local, first, before adding them to the list.

    [...] This did not work
    Please take the time and review the FAQs, especially regarding this sentence, which states

    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.
    Also, note our preferences for full real names here on Statalist.

    Edit:

    I did not answer your question because I believe that you are already using the correct macro list functions.

    Best
    Daniel
    Last edited by daniel klein; 11 Jul 2018, 14:07.

    Comment


    • #3
      Dear Daniel,

      Thank you very much for your response! Please let me address your comments in turn

      Originally posted by daniel klein View Post
      No, it does not. It gives you a list that looks something like
      Code:
      substr("prefix_..._....dta", strlen("prefix_..._....dta")-9, 6) substr("prefix_..._....dta", strlen("prefix_..._....dta")-9, 6) ...
      When I execute
      Code:
      display `quarters'
      after the foreach loop I get
      Code:
      2013q42014q12013q42014q12014q1
      So I am not sure why you come to the conclusion that I should not.

      Originally posted by daniel klein View Post
      You need to store the results of the string functions into a local, first, before adding them to the list.
      I gave that a try and it does not change the result, I get the exact same.

      Originally posted by daniel klein View Post
      Please take the time and review the FAQs, especially regarding this sentence, which states [...]
      I agree that this was an imprecise statement. When I am saying that "It did not work" I meant that placing a
      Code:
      display `quarters'
      before and after the corresponding command in question, the result was exactly the same. It did not get a syntax error.

      Thank you for your interest in my problem.
      Last edited by jhonny-econ; 11 Jul 2018, 15:22.

      Comment


      • #4
        jhonny,

        the following is the code you claimed to have used (I added the display line at the end to see the result, all changes are immaterial to the problem)

        Code:
        local filenames: dir "e:/foo" files "*.dta"
        foreach file of local filenames {
            local quarters `quarters' substr("`file'", strlen("`file'")-9,6)
        }
        
        display `"`quarters'"'
        I get

        Code:
        . local filenames: dir "e:/foo" files "*.dta"
        
        . foreach file of local filenames {
          2.     local quarters `quarters' substr("`file'", strlen("`file'")-9,6)
          3. }
        
        .
        . display `"`quarters'"'
        substr("foo20014q4.dta", strlen("foo20014q4.dta")-9,6) substr("bar20014q4.dta", strlen("bar20014q4.dta")-9,6)

        Edit (major)

        Now I see what is going on. When you type

        Code:
        display `quarters'
        omitting the (compound) double quotes, display sees what I correctly assumed you would get and what I have replicated above. display, however, understands string functions and evaluates them before showing the results; this is what you see. By now, you seem to have corrected the -9 in your substr() to read -10.

        Anyway, following my earlier advice, you will get what you want.

        Code:
        local filenames: dir "e:/foo" files "*.dta"
        foreach file of local filenames {
            local add = substr("`file'", strlen("`file'")-10,6)
            local quarters `quarters' `add'
        }
        display `"`quarters'"'
        local quarters : list uniq quarters
        display `"`quarters'"'
        gives

        Code:
        . local filenames: dir "e:/foo" files "*.dta"
        
        . foreach file of local filenames {
          2.     local add = substr("`file'", strlen("`file'")-10,6)
          3.         local quarters `quarters' `add'
          4. }
        
        . display `"`quarters'"'
        20014q 20014q
        
        . local quarters : list uniq quarters
        
        . display `"`quarters'"'
        20014q
        Best
        Daniel
        Last edited by daniel klein; 11 Jul 2018, 16:06.

        Comment

        Working...
        X