<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>The Stata Things &#187; loops</title>
	<atom:link href="http://enoriver.net/index.php/tag/loops/feed/" rel="self" type="application/rss+xml" />
	<link>http://enoriver.net</link>
	<description>computing for fun and profit</description>
	<lastBuildDate>Mon, 07 May 2012 13:43:02 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Looping across observations</title>
		<link>http://enoriver.net/index.php/2008/11/26/looping-across-observations/</link>
		<comments>http://enoriver.net/index.php/2008/11/26/looping-across-observations/#comments</comments>
		<pubDate>Wed, 26 Nov 2008 16:29:09 +0000</pubDate>
		<dc:creator>Gabi Huiber</dc:creator>
				<category><![CDATA[Stata]]></category>
		<category><![CDATA[loops]]></category>

		<guid isPermaLink="false">http://enoriver.net/?p=331</guid>
		<description><![CDATA[People who stress that you very seldom have to do this in Stata are right. That's why I always forget how it's done. So here, for my reference and yours: Today I looked at a table in .rtf format. It had test scores for a bunch of students in Poli Sci at the University of [...]]]></description>
			<content:encoded><![CDATA[<p>People who stress that you very seldom have to do this in Stata are right. That's why I always forget how it's done. So here, for my reference and yours:</p>
<p>Today I looked at a table in .rtf format. It had test scores for a bunch of students in Poli Sci at the University of Cluj, in Romania. The table is simple: two columns, with the students' full names on the first and their respective test score on the second.</p>
<p>I tried to copy and paste this table straight into the Stata data editor and surprise: I got a single column, with twice the number of original rows and alternating names and scores. The odd-numbered rows had names, the even-numbered ones had scores. Evidently the RTF column delimiter looks like an end-of-line character to both Stata and Notepad++.</p>
<p>So here was my chance to loop across observations in order to turn this table back into the two-column format. The code is below:<br />
<code><br />
set more off<br />
count<br />
local obs=r(N)/2<br />
di `obs'<br />
capture drop var2<br />
gen var2=""<br />
</code><code><br />
forvalues i=1/`obs' {<br />
  local there=`i'*2<br />
  local here=`there'-1<br />
  replace var2=var1[`there'] in `here'<br />
}<br />
drop if var2==""<br />
destring var2, replace<br />
</code><br />
There's not much to it. Looping across observations is intuitively simple, but it's awfully inefficient. Do it if you must with small data sets, but consider alternatives, which are plentiful, if you are dealing with thousands of observations.</p>
]]></content:encoded>
			<wfw:commentRss>http://enoriver.net/index.php/2008/11/26/looping-across-observations/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

