Dsstatgroupby is a wonderful tool, but it could be a lot more general. A good approach to this may be creating a SQL-like front-end language.