There is a database called Compustat
that is run by S&P, you could gather the data you want from it, however the database is at the individual security level so there would have to be some aggregation (either when you run the compustat query or in a separate database once you have the raw data). Also not sure if there is a flag for membership in the S&P 500, and you'd also have to figure out how to account for stocks that get added/dropped from the index.
also has an interface to Compustat. The library might have a subscription, but you're more likely to find it at a research institution / graduate business school.