It's a little difficult because you're summarising at more than one level, so I've used PROC SUMMARY
and chosen different _TYPE_
values. See below:
data have; infile datalines; input school $ name $ subject : $10. picked saving expenses; datalines; raget John math 10 10500 3500 raget John spanish 5 1200 2000 raget Ruby nosubject 10 5000 1000 raget Ruby nosubject 2 3000 0 raget Ruby math 3 2000 500 raget peter geography 2 1000 0 raget noname nosubject 0 0 1200 ; run; proc summary data=have; class school name subject; var picked saving expenses; output out=want1 sum(picked)=picked sum(saving)=saving sum(expenses)=expenses; run; proc transpose data=want1 (where=(_type_=5)) out=subs (where=(_NAME_='picked')); by school; id subject; run; proc transpose data=want1 (where=(_type_=6)) out=names (where=(_NAME_='picked')); by school; id name; run; proc sql; create table want (drop=_TYPE_ _FREQ_ name subject) as select n.*, s.*, w.* from want1 (where=(_TYPE_=4)) w, names (drop=_NAME_) n, subs (drop=_NAME_) s where w.school = n.school and w.school = s.school; quit;
I've also tested this code by adding new schools, names and subjects and they do appear in the final table. You'll note that I haven't hardcoded anything (e.g. no reference to math
or John
), so the code is dynamic enough.