Saturday, April 4, 2009

Retrieving Data From A Website Into Excel

In this post, I'll show you how to query data from a website using Microsoft Excel. In this example, we will be extracting the stock quote and relevant information for Macy (NYSE:M) from the Yahoo! finance page. First, launch Excel. From the Data taskbar, select Import -> External Data -> New Web Query.


The web query will open a window. In the address section (look for the first red circle), type in the web address. Here I typed in "http://finance.yahoo.com/q?s=M", which is the address for looking up the stock quote for Macy's in Yahoo! finance. Click on Import (the second red circle).

Excel will give you the option to output the data in the existing spreadsheet or a new spreadsheet. Make your choice and click on OK.

Now you should have the data in your Excel spreadsheet. You can analyze the data as you wish.


You can save this entire process in a macro, and launch it with the press of a button. Depending on how you design your macro, you can kick off the process everyday and get the most current information.


You can even have SAS kick off this macro, and read in the data from the Excel spreadsheet. Using this method, you can write a SAS program - maybe to analyze a particular stock - which can pull data from a finance website for that stock. Please see my post on August 27, 2008 (Tools of the Trade V: Kicking Excel VBA Macros From SAS) on how to launch Excel macros from SAS, and see my post on December 29, 2007 (Tools of the Trade II: Using SAS to Extract the Data) on how to read in data from Excel into your SAS program.