@MatthewVita and @robertdown ,
I don't know python very well, but I think storing everything in array as build the demoset will really be limiting. Isn't there a way to throw each patient into output via the generate_sql function rather than push it into an array?
(I know the work around is to just run the script multiple times, but then you lose ability to increment things like pid)