# Attaching WolframAlpha data

I have used WolframAlpha since it was launched (now a pro member) but am new to Mathematica 8. Is there a way to automatically add data from WA to a Mathematica dataset? For example, what if I have data on annual sales and I want to add the USA's GDP in the given year as the third column. I have used the WolframAlpha[] function but am not sure how to integrate it into my data. I'd want to add a "GDP" column to this data:

In:= sales = Import["C:\\Users\\Andrew\\Desktop\\DemoData.xlsx"]

Out= {{{"Year", "Sales"}, {2000., 105704.}, {2001.,
120042.}, {2002., 118973.}, {2003., 126539.}, {2004.,
105582.}, {2005., 126510.}, {2006., 101745.}, {2007.,
105821.}, {2008., 127313.}, {2009., 100908.}, {2010.,
115692.}, {2011., 116188.}}}


This isn't the best example, but this kind of thing would make my grad school life markedly easier. No more downloading, cleaning, and merging independent variable sets from random sources!

You can access historical GPD data via the WolframAlpha command like so:

gdpData = WolframAlpha["us gdp",
{{"History:GrossDomesticProduct:EconomicData",1},"ComputableData"}];


This returns quarterly data going back to 1947 in a structured list. You can manipulate the data just as you would any data in Mathematica. Thus, you can get the data points at the first of the year in the date range desired like so:

gdpDataRestricted = First/@ SplitBy[Select[gdpData,
2000<=#[[1,1]]<=2011&],#[[1,1]]&];


You can then combine it with your data and display the result.

yourData = {{2000.,105704.},{2001.,120042.},{2002.,118973.},
{2003.,126539.},{2004.,105582.},{2005.,126510.},{2006.,101745.},
{2007.,105821.},{2008.,127313.},{2009.,100908.},{2010.,115692.},
{2011.,116188.}};
allData = Table[Append[yourData[[k]],gdpDataRestricted[[k,-1]]],
{k,1,Length[yourData]}];
Grid[Prepend[allData,{"Year","Sales", "GDP"}], Dividers -> All] Figuring out the WolframAlpha command can be tricky. I usually start with the most basic command (in this case, WolframAlpha["us gdp"]) and then use the + signs in the upper right portion of the pods to find the correct invocation to get at the actual data.

• +1 I was just about to edit your post and include a screenshot how one can extract the specific WolframAlpha call when you added it ;-) Aug 17, 2012 at 3:23
• Exactly what I wanted! You'd be surprised at how many researchers, professors, and TAs spend hours adding data by processing and combining data files. Aug 17, 2012 at 3:24