一、目标:数据共有81行观测,需要统计出每组make下cou最大的值:
data test.a(keep=make drivetrain);
set sashelp.cars;
proc print data=a;
run;
proc sql;
create table test.b as
select * from
(select make,drivetrain,count(*) as cou from test.a group by make,drivetrain)
quit;
run;
proc sql;
create table c as
select * from b t
where exists (select count(*) from b as b1 where b1.cou >= t.cou and b1.make = t.make group by b1.make
having count(*)<=1);
quit;
开始运行的结果和运行结果不一致,后来以为是逻辑库的问题,因为换了逻辑库就好了(起初用的是work逻辑库),最后检验,发现是要给每个表都起别名,就OK了。
像蜗牛一样慢慢来吧,算然进步很慢但是今天比昨天的自己多知道一点点就好。
二、子查询:找出大于平均的观测:
proc sql;
select make,type,mpg_city,avg(mpg_city) as avg_mpg,weight,avg(weight) as avg_weight from test.d
having mpg_city > select avg_mpg and weight > avg_weight;
quit;
等价写法:
proc sql;
select make,type,mpg_city,weight from test.d
having mpg_city > (select avg(mpg_city) from test.d) and weight > (select avg(weight) from test.d);
quit;
三、查询数据表的所有字段名
proc contents data=sashelp.cars out=o1(keep=name label);
run;
四、参考资料
https://www.sohu.com/a/130832971_278472