Stata Programming Lecture [updated]

October 14, 2010 at 3:50 pm 3 comments

| Gabriel |

I gave my introduction to Stata programming lecture again. This time the lecture was cross-listed between my graduate statistics course and the UCLA ATS faculty seminar series. Here are the lecture notes.

Entry filed under: Uncategorized. Tags: , , .

The Emperor’s New Lunch Counter fsx.ado, fork of fs.ado (capture ls as macro)


  • 1. Nick Cox  |  October 15, 2010 at 8:05 am

    Many useful tips.

    On p.5 “In Stata a macro is something held in memory aside from the main dataset”. I’d omit the “something” : otherwise this could be read like a definition, which it isn’t. That way it is a useful and accurate comment.

    -fs- from SSC is a convenience wrapper to get lists of filenames.

    • 2. gabrielrossman  |  October 15, 2010 at 2:56 pm


      glad you liked it and thanks for the tips, I’ll post an updated version soon.

      about “fs”, this looks like a great program since, as you can see, I often want to store “ls” as a macro and seed a loop. some of my earliest posts on this blog were really hideous byzantine code attempting to do all that.

      unfortunately, when I tried running “fs” it collects the hidden files beginning with a dot, kind of like the “ls -a” command in Unix. this is odd because the default behavior of ls in Stata (or Unix) is to suppress these files. i don’t complete understand the way you coded it, but my hunch is that the bug originates in the line

      else local files : dir . files "`f'" 

      anyway, in Unix I would just do this.

      ls -F . | grep [^/]$ 

      because there is no -a flag the hidden files are suppressed and then I rely on the -F flag and the grep to kick out the directories. you could use “c(os)”, “if”, and “shell” to work this in to the ado file.

      another solution would be to take your existing file, but tack something on to the end to kick out any system files. this would have the advantage that the post-processing could be suppressed by an option if for some bizarre reason somebody actually wanted to use Stata to read their system files. here’s the kind of thing I’m thinking of, written to just use “disp” instead of “return” because it’s just a proof of concept.

      foreach f in `r(files)' {
        if regexm("`f'","^[^.]")==1 {
          disp "`f'"
  • 3. Nick Cox  |  October 17, 2010 at 7:12 am

    I am glad you like -fs-.

    In this post, however, “bug” seems to be your word for “does not do what I want”. A programmer can talk thus about their own program, but not a user! -fs- used without argument catches all files in a directory or folder; that’s not a bug from my point of view.

    Another way of tackling this is to use -fs- twice with different calls and then get the set difference by local macro manipulation. In turn that pair of commands could be wrapped into a command.

The Culture Geeks

%d bloggers like this: