proc sql <options>;
create table <tablename> as 生成数据集
select 选取列
from 指定查询数据集
where 按指定条件生成数据集
group by 分组
order by 排序
quit;
join函数
case 1创建了一个名为 class 的表,该表是从 sashelp.class 中选择的所有列;
proc sql;
/* create table class as*/
select *
from sashelp.class
/* where */
/* group by*/
/* order by*/
;
quit;
case 2 创建了一个名为 class 的表,该表是从 sashelp.class 中选择name height列
proc sql;
/* create table class as*/
select name, height
from sashelp.class
/* where */
/* group by*/
/* order by*/
;
quit;
case 2.1创建了一个名为 class 的表,该表包含了从 sashelp.class 中选择的独特(不重复)的性别(sex)和年龄(age)列的数据
proc sql;
/* create table class as*/
select distinct sex, age
from sashelp.class
/* where */
/* group by*/
/* order by*/
;
quit;
case 3 选取年龄大于14的记录
proc sql;
/* create table class as*/
select *
from sashelp.class
where age > 14
/* group by*/
/* order by*/
;
quit;
case 4 sashelp.class 中选择所有列的数据,并限制输出观测值的数量为 5
proc sql outobs = 5;
/* create table class as*/
select *
from sashelp.class
/* where */
/* group by*/
/* order by*/
;
quit;
case 5创建了一个新的名为 class 的表,从 sashelp.class 中选择了名字 (name)、性别 (sex),并进行了一些计算和转换,例如将身高从英寸转换为厘米,将体重从磅转换为千克,并计算了 BMI 值。最后,只包括性别为 'M' 的记录;
proc sql;
create table class as
select name, sex as male, 'Male' as sex_char, 1 as sex_number,
height*2.54 as height1, weight*0.453 as weight1, weight*0.453/(height*2.54/100)**2 as bmi1
from sashelp.class
where male = 'M'
/* group by*/
/* order by*/
;
quit;
case 6这段代码从 sashelp.class 中选择了所有列的数据,并添加了三个额外的计算列:minAge 包含了年龄的最小值,maxH 包含了身高的最大值;
proc sql;
/* create table class as*/
select *, min(age) as minAge, max(height) as maxH, mean(weight) as meanW
from sashelp.class
/* where */
/* group by */
/* order by*/
;
quit;
case 7;计算每个年龄的出现次数,并将结果存储在一个新的数据集中
proc sql;
/* create table class as*/
select age, count(*) as countAge
from sashelp.class
/* where */
group by age
/* order by*/
;
quit;
case8计算每个年龄和性别组合的出现次数,并将结果存储在一个新的数据集中
**case 8;
proc sql;
/* create table class as*/
select age, sex, count(*) as countAgeSex
from sashelp.class
/* where */
group by age, sex
/* order by*/
;
quit;
case 9;计算每个性别的身高均值,并将结果存储在一个新的数据集中
proc sql;
/* create table class as*/
select sex, mean(height) as meanSexH
from sashelp.class
/* where */
group by sex
/* order by*/
;
quit;
*case 10选择所有列,并按照身高对数据进行排序
proc sql;
/* create table class as*/
select *
from sashelp.class
/* where */
/* group by */
order by height
;
quit;
**case 11;选择所有列,并按照性别、年龄和身高对数据进行排序
**case 11;
proc sql;
/* create table class as*/
select *
from sashelp.class
/* where */
/* group by */
order by sex, age ,height
;
quit;
**create table for join process;
data A;
input name $8. sex $2. age;
cards;
Alfred M 14
Alice F 13
Barbara F 13
Carol F 14
Henry M 14
;
run;
data B;
input name $8. animal $6.;
cards;
Alfred cat
Alfred dog
Carol rabbit
James fish
;
run;
**case 12;
proc sql;
/* create table class as*/
select *
from a, b
/* where */
/* group by */
/* order by */
;
quit;
**case 13;筛选出在数据集 a
中有但在数据集 b
中没有的记录。这段代码的作用是创建一个名为 class
的新数据集,其中包含了这些记录
proc sql;
/* create table class as*/
select *
from a left join b on a.name = b.name
where ^b.name
/* group by */
/* order by */
;
quit;
case 14筛选出在数据集 b
中有但在数据集 a
中没有的记录。这段代码的作用是创建一个名为 class
的新数据集,其中包含了这些记录
proc sql;
/* create table class as*/
select *
from a right join b on a.name = b.name
where ^a.name
/* group by */
/* order by */
;
quit;
**case 15;
从数据集 a
和数据集 b
中选择所有的列。执行了一个内连接操作(inner join
),这意味着只有那些在数据集 a
和数据集 b
中都存在的 name
值的记录才会被包括在结果中。
**case 15;
proc sql;
/* create table class as*/
select *
from a inner join b on a.name = b.name
/* where */
/* group by */
/* order by */
;
quit;
case16 full join
会将数据集 a
和数据集 b
中的记录合并在一起,不管是否存在匹配的 name
值
**case 16;
proc sql;
/* create table class as*/
select *
from a full join b on a.name = b.name
where ^a.name or ^b.name ^(a.name and b.name)
/* group by */
/* order by */
;
quit;
case17 外连接
**case 17;
proc sql;
/* create table class as*/
select *
from a
outer unoin corr
select *
from b
;
quit;