sas sql中有类似mysql的 g_SAS中的SQL
自我愚見,望有錯指出改之。
/*SQL 學習*/
/*數據定義語言(DDL):create、drop、alter*/
/*數據操作語言(DML):insert、update、delete*/
/*數據查詢語言(DQL):select*/
/*數據控制語言(DCL):grant、remove、commit、rollback*/
/*SELECT語句*/
proc sql;
select ...
from..
where..
group by..
having..根據group
by得到子數據集
order by..;
/*EX1*/
data a;
set sashelp.class;
where a<70;
run;
proc sql;
select sex='F'
from sashelp.classfit;
quit;
proc sql;
select sex='F'
from sashelp.classfit;
quit; /*s輸出的結果是111000.... 表示如果是符合sex=‘F’的就是1*/
/*利用SQL創建新表*/
proc sql;
create table
new(表的名字) as
select * (代表選擇全部)
from ..;
quit;
/*利用列創建新變量*/
proc sql;
create table
new as
select*,
weight/height as ratio /*這個蠻有意思
ratio是新的列名*/
format=5.2 label='體重:身高比'
from
sashelp.classfit;
quit;
/*利用SQL進行數據統計 類似Proc
summary */
proc sql;
create table
new1 as
select min(age) as youngest,
max(age) as oldest,
mean(height) as h format=5.1
from sashelp.classfit;
quit;
/*等同于下面的語句*/
proc summary data=sashelp.classfit;
var age height;
output out
=new2(drop=_type_ _freq_)/*刪去兩個列的統計量*/
min(age)=yougest
max(age)=oldgest
mean(height)=h; /*記住分號別忘了*/
run;
/*利用SQL進行分組統計 加入了group
by*/
proc sql;
create table
new3 as
select sex,
min(age) as youngest,
max(age) as oldest,
mean(height) as h format=5.1
from sashelp.classfit
group by
sex;
quit;
/*等同于下面的語句*/
proc summary data=sashelp.classfit;
class sex;
var age height;
output out
=new4(drop=_type_ _freq_)
min(age)=yougest
max(age)=oldgest
mean(height)=h;
run;
/*根據條件語句創建數據集
CASE WHEN ..THEN*/
proc sql;
create table
new5 as
select name,age,sex,
case when
age=11 then 'zoo'
when sex='F' then 'man'
else '[none]'
end
as nenene /*是對新的 zoo man 這列命名*/
from new;
quit;
/*利用where創建子數據集 簡單*/
proc sql;
create table
new6 as
select *
from new
where sex='F';
quit;
/*利用having從句代替where從句
*/
proc sql;
create table
new7 as
select*
from new
group by
sex,age
having sex='F';
quit;
/**必記:having只能在group
by 之后 where必須在其之前**/
/*利用distinct剔除重復記錄 **這個有陷阱見筆記*/
proc sql;
create table
new8 as
select distinct*
from new;
quit; /*這個例子看不出效果 是以行為標準*/
/*****************************************************/
/*SQL提升*/
/*一.將原始變量和二次分析生成的變量進行合并*/
proc sql;
create table
new9 as
select name,sex,
count(*) as
many label='統計'
from
new
group by age
order by name ;
quit;
proc sql;
create table
new10 as
select name,sex,
count(distinct
sex) as many label='統計'
from
new
group by age
order by name ;
quit; /** Really 看不懂這的結果
為什么輸出來那樣???怎么做到的統計
哦哦 看明白了 是利用年齡的統計 后面distinct age 剔除重復的年齡再統計*/
/*二.SQL語言的嵌套*/
proc sql;
create table
new11 as
select *
from(select
age, /*標點*/
count(*)as
many
from new
group by age
)
having many=max(many);/*重點*/
quit;
/*下面是不嵌套的*/
proc sql;
create table
new12 as
select age,
count(*)as
many
from new
group by age;
proc sql;
create table
new13 as
select *
from new12
having many=max(many);
quit;
/*三、連接 難難難~~~~*/
/*簡單合并用merge在data步*/
/*交叉合并
cross join 、 full join(key)、nature
full join(key)
inner join、left
join、right join筆記本上*/
data one;
do a=1,2;
output;
end;
run;
data two;
do b=10,9,8;
output;
end;
run;
data combined;
merge one two;
run;
proc sql;
create table
combined as
select *
from one cross join two;
quit; /*兩種合并處理效果不一樣!!!*/
/*四、where exists、where in(where any)查詢數據庫中已有的觀測*/
data H;
input name $age;
cards;
John 12
;
proc sql;
create table
HH as
select *
from H
where exists
(select*
from new
where h.name=new.name
);
quit; /*有趣哈哈哈哈*/
proc sql;
create table
HHH as
select *
from h
where name in
(select name
from new
);
quit;
proc sql;
create table
HHHH as
select *
from h
where name=any /*補充ANY用法 >any(20,30)相當于>20
(select name
from new
);
quit;
/*五、intersect(交集),union(并集),expect(補集),
余集(得另外編程沒有特定的代碼)、union all(全集)、的區別*/
/*補充ALL用法 >all(20,30)相當于大于30,
/*any就是大于小的小于大的,all就是小于小的大于大的*/
/*小結:對小型表連接 數據步更有效,其余肯定SQL更有效啦*/
/*六、去掉標題(title;),顯示程序效率 (stimer)*/
TITLE;
proc sql feedback stimer;
select *
from new
order by
age;
reset nostimer outobs=3;
select *
from new
order by
age;
quit; /*厲害這樣顯示*/
/*SQL在幾個主要程序測試與效率
exec/noexec:控制SQL語句是否在執行
nostimer/stimer:在sas日志中報告每個SQL語句的效率統計數據
noerrorstop/errorstop:批處理時控制當有錯誤發生時,是否要檢查語法
*/
/*七、利用宏變量*/
data w;
input ID $ a1-a4;
cards;
A 11 12 13 14
B 21 22 23 24
;
/*計算各個變量之和*/
proc sql;
create table
sums as
select sum(a1) as sum1,
sum(a2) as sum2,
sum(a3) as sum3,
sum(a4) as sum4 /*記住這沒有點號!!!*/
from W
;
quit;
/*等價*/
proc summary data=w;
var a1-a4;
output out=sumss(drop=_type_
_freq_)
sum=sum1-sum4;
run;
data sumsss;
set w end=last;
ARRAY _a{*}a1-a4; /*定義數組*/
ARRAY _sum{*}sum1-sum4;
keep sum1-sum4;
do i=1 to 4;
_sum{i]+_a{i};
end;
if last then output;
run;
/*變量更多時 就用宏*/
%macro selectsums(maxindex=);
%do n=1 %to &maxindex;
sum(a&n)
as sum&n
%if &n NE &maxindex %then ,
;
%END;
%mend selectsums;
PROC SQL;
create table
sumssss as
select %selectsums(maxindex=4)
from w
;
quit; /*完美!*/
/*八、利用SQL生成報表*/
/*九、混合復雜操作*/
/*行列轉置 常規的轉置有proc transpose ..*/
proc sql;
create table
ww as
select id,'a1' as item,
a1 as vlaue
from w
union all
select id,'a2' as item,
a2
from w
union all
select id,'a3' as item,
a3
from w
union all
select id,'a4' as item,
a4
from w;
quit;
proc sql;
create table
verticalsums as
select *
from ww;
group by
item;
quit;
by tt.
總結
以上是生活随笔為你收集整理的sas sql中有类似mysql的 g_SAS中的SQL的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 阳荷根的功效与作用、禁忌和食用方法
- 下一篇: Cef mysql.exe_CEF3.2