<?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; Excel</title>
	<atom:link href="http://enoriver.net/index.php/tag/excel/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>Data from Excel to Stata, take two</title>
		<link>http://enoriver.net/index.php/2008/11/12/data-from-excel-to-stata-take-two/</link>
		<comments>http://enoriver.net/index.php/2008/11/12/data-from-excel-to-stata-take-two/#comments</comments>
		<pubDate>Wed, 12 Nov 2008 04:49:49 +0000</pubDate>
		<dc:creator>Gabi Huiber</dc:creator>
				<category><![CDATA[Stata]]></category>
		<category><![CDATA[Excel]]></category>
		<category><![CDATA[odbc load]]></category>

		<guid isPermaLink="false">http://enoriver.net/?p=290</guid>
		<description><![CDATA[I don't like spreadsheets. I like data sets and matrices. But I get data in Excel workbooks all the time, and it's a pain in the neck. This will never go away altogether, but if my Stata-using colleagues could be persuaded to quit sending me spreadsheets, that would at least cut down on the volume. [...]]]></description>
			<content:encoded><![CDATA[<p>I don't like spreadsheets. I like data sets and matrices. But I get data in Excel workbooks all the time, and it's a pain in the neck. This will never go away altogether, but if my Stata-using colleagues could be persuaded to quit sending me spreadsheets, that would at least cut down on the volume. I've tinkered with all kinds of ways to make data extraction from spreadsheets into Stata easier. This is the latest:<br />
<code><br />
clear<br />
set mem 10m<br />
set more off<br />
set type double<br />
</code><code><br />
local here "`c(pwd)'/"<br />
</code><code><br />
// declare name and date of Excel file to read from<br />
local xlsfile  "revised PriceIncr20080815-TargetTempl.xls"<br />
local filedate "20081110"<br />
</code><code><br />
// list the worksheets of interest<br />
local sheets ""Move these data" "And these too""<br />
</code><code><br />
// Extract data from Excel to Stata. Leave this part alone.<br />
local sheetno: list sizeof sheets<br />
forvalues i=1/`sheetno' {<br />
   local k: word `i' of `sheets'<br />
   local my_path     "`here'`xlsfile'"<br />
   local my_sheet    "`k'"<br />
   local source "Excel Files;DBQ=`my_path'"<br />
</code><code><br />
   clear<br />
   odbc load, dsn("`source'") table("`my_sheet'$")<br />
</code><code><br />
   unab varz: _all<br />
   foreach z in `varz' {<br />
      local lower=lower("`z'")<br />
      if "`lower'"!="`z'" {<br />
         rename `z' `lower'<br />
      }<br />
   }<br />
</code><code><br />
   describe<br />
   compress<br />
   save "`here'`k'`filedate'.dta", replace<br />
}<br />
</code><br />
Notice the c(pwd) function. It returns the path to the working directory. Essentially, the do-file above translates the worksheets listed in the local `sheets' into Stata files saved in the same directory as the do-file is in. The original Excel file may be in the same directory, as in this example, or it may be somewhere else. The `filedate' local is optional. Also optional, of course, is the loop that turns variable names to lower case. I just like mine that way.</p>
]]></content:encoded>
			<wfw:commentRss>http://enoriver.net/index.php/2008/11/12/data-from-excel-to-stata-take-two/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

