Total Drek

Or, the thoughts of several frustrated intellectuals on Sociology, Gaming, Science, Politics, Science Fiction, Religion, and whatever the hell else strikes their fancy. There is absolutely no reason why you should read this blog. None. Seriously. Go hit your back button. It's up in the upper left-hand corner of your browser... it says "Back." Don't say we didn't warn you.

Monday, March 30, 2009

Advice you never asked for: Volume 1

I often find that people don't ask me for advice. This is a good thing as I don't know anything. Still, it means that in order to provide people with advice, I have to give it unsolicited. As you might expect, this works hand-in-glove with blogging which, after all, is a pass time for people who want to offer their opinions to those who haven't asked for them.

Today I want to briefly touch on a topic that some of my fellow grad students out there might be struggling with: writing loops in Stata.

A loop is a way to make software (in this case Stata) repeat the same behavior or behaviors over and over until it finishes some monotonous task. This is good because humans are not good at doing the same precise thing over and over again without mistakes. I, for example, often accidentally misspell "again" as "agin", which is still intelligible but makes me sound scottish. In Stata, loops are invoked using the "foreach" or "forvalues" commands, which you can look up in the help menu. A typical foreach command might look something like this:

generate paper=0

foreach year in 1 2 3 4 5 6 7 {
replace paper=paper+`year' if tenure~=1
replace job=0 if `year'==7 & tenure==0
}


The foreach command defines a macro, "year", which contains the values 1 through 7. Each time the program goes through the loop,* the next value of "year" is used whenever you see `year' in a command. So, the first time through the first replace statement would read, "replace paper=paper+1 if tenure~=1" while the second time it would read "replace paper=paper+2 if tenure~=1".

Now, the tricky thing with loops** is syntax. It may look in some fonts like I'm typing 'year' instead of `year'. If you can't see the difference between the terms I just wrote then, congratulations, this post is for you. See, in a correctly typed usage (i.e. `year') that first punctuation mark is not an apostrophe. Instead, it's that funny little mark that usually appears with the tilde. I've included a picture for your convenience:



If you use it properly, whenever you invoke the macro, then you're fine. If, on the other hand, you use an apostrophe, Stata returns an unhelpful error message that " ' is not a valid name". So, whenever looping, just remember that ` ~= ' and you'll be fine.




* The first time through it is referred to as "looping" the second time as "loop-de-looping" the third time as "loop-de-loop-de-looping" and so on until you go mad.

** Yeah, right, because there's only ONE tricky thing with loops.

Labels: , , , ,

2 Comments:

Blogger Marf said...

I take it you had to figure that one out the hard way?

Monday, March 30, 2009 9:58:00 PM  
Blogger Drek said...

Hard is relative. I learned loops from a book whose font made distinguishing ` from ' very difficult. Still, it was not the trickiest thing I've ever had to puzzle out. Not by a longshot.

Tuesday, March 31, 2009 6:12:00 AM  

Post a Comment

<< Home

Site Meter