In honor of Amazon’s official release date for the book, we offer this blog entry.
Both SAS and R can be used to find the Amazon Sales Rank for a book by downloading the desired web page and ferreting out the appropriate line. This code is likely to break if Amazon’s page format is changed (but it worked as of July, 2009). In this example, we find the sales rank for our book. Some interesting information about interpreting the rank can be found here or here.
Both SAS and R code below rely on section 1.1.3, ”Reading more complex text ﬁles.” Note that in the displayed SAS and R code, the long URL has been broken onto several lines, while it would have to be entered on a single line to run correctly.
In SAS, we assign the URL an internal name (section 1.1.6), then input the ﬁle using a data step. We exclude all the lines which don’t contain the sales rank, using the count function (section 1.4.6). We then extract the number using the substr function (section 1.4.3), with the find function (section 1.4.6) employed to locate the number within the line. The last step is to turn the extracted text (which contains a comma) into a numeric variable.
filename amazon url "http://www.amazon.com/
infile amazon truncover;
input @1 line $256.;
if count(line, "Amazon.com Sales Rank") ne 0;
rankchar = substr(line, find(line, "#")+1,
find(line, "in Books") - find(line, "#") - 2);
rank = input(rankchar, comma9.);
proc print data=test noobs;
# grab contents of web page
urlcontents <- readLines("http://www.amazon.com/
# find line with sales rank
linenum <- suppressWarnings(grep("Amazon.com Sales Rank:",
# split line into multiple elements
linevals <- strsplit(urlcontents[linenum], ' ')[]
# find element with sales rank number
entry <- grep("#", linevals)
# snag that entry
charrank <- linevals[entry]
# kill '#' at start
charrank <- substr(charrank, 2, nchar(charrank))
# remove commas
charrank <- gsub(',','', charrank)
# turn it into a numeric opject
salesrank <- as.numeric(charrank)
The resulting output (on July 16, 2009) is