![SAS中用到的SQL語(yǔ)法及結(jié)構(gòu)_第1頁(yè)](http://file2.renrendoc.com/fileroot_temp3/2021-11/25/31be9dd5-31a7-437a-acec-47eab9a20a39/31be9dd5-31a7-437a-acec-47eab9a20a391.gif)
![SAS中用到的SQL語(yǔ)法及結(jié)構(gòu)_第2頁(yè)](http://file2.renrendoc.com/fileroot_temp3/2021-11/25/31be9dd5-31a7-437a-acec-47eab9a20a39/31be9dd5-31a7-437a-acec-47eab9a20a392.gif)
![SAS中用到的SQL語(yǔ)法及結(jié)構(gòu)_第3頁(yè)](http://file2.renrendoc.com/fileroot_temp3/2021-11/25/31be9dd5-31a7-437a-acec-47eab9a20a39/31be9dd5-31a7-437a-acec-47eab9a20a393.gif)
![SAS中用到的SQL語(yǔ)法及結(jié)構(gòu)_第4頁(yè)](http://file2.renrendoc.com/fileroot_temp3/2021-11/25/31be9dd5-31a7-437a-acec-47eab9a20a39/31be9dd5-31a7-437a-acec-47eab9a20a394.gif)
![SAS中用到的SQL語(yǔ)法及結(jié)構(gòu)_第5頁(yè)](http://file2.renrendoc.com/fileroot_temp3/2021-11/25/31be9dd5-31a7-437a-acec-47eab9a20a39/31be9dd5-31a7-437a-acec-47eab9a20a395.gif)
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、sas中的sql語(yǔ)句完全教程之一:sql簡(jiǎn)介與基本查詢功能1 sql過程步介紹1.1 sql過程步可以實(shí)現(xiàn)下列功能:查詢sas數(shù)據(jù)集、從sas數(shù)據(jù)集中生成報(bào)表、以不同方式實(shí)現(xiàn)數(shù)據(jù)集合并、創(chuàng)建或刪除sas 數(shù)據(jù)集、視圖、索引等、更新已存在的數(shù)據(jù)集、使得sas系統(tǒng)可以使用sql語(yǔ)句、可以和 sas的數(shù)據(jù)步進(jìn)行替換使用。注意,sql過程步并不是用來代替sas數(shù)據(jù)步,也不是一個(gè)客 戶化的報(bào)表工具,而是數(shù)據(jù)處理用到的查詢工具。1.2 sql過程步的特征sql過程步并不需要對(duì)每一個(gè)查詢進(jìn)行重復(fù)、每條語(yǔ)句都是單獨(dú)處理、不需要print過程步 就能打印出查詢結(jié)果、也不用sort過程步進(jìn)行排序、不需要run、要
2、quit來結(jié)束sql過程步1.3 sql過程步語(yǔ)句select:查詢數(shù)據(jù)表中的數(shù)據(jù)alter:增加、刪除或修改數(shù)據(jù)表的列create:創(chuàng)建一個(gè)數(shù)據(jù)表delete:刪除數(shù)據(jù)表中的列describe:列出數(shù)據(jù)表的屬性drop:刪除數(shù)據(jù)表、視圖或索引insert:對(duì)數(shù)據(jù)表插入數(shù)據(jù)reset:沒用過,不知道什么意思select:選擇列進(jìn)行打印update:對(duì)已存在的數(shù)據(jù)集的列的值進(jìn)行修改2 sql基本査詢功能2.1 select語(yǔ)句基本語(yǔ)法介紹select <distinct> object-item <, .object-item>from from-list<wher
3、e sql-expression><group by groupbyitem <7 group-by-item»<having sql-expression_r><order by order-by-item <,. order-by-item»這里select:指定被選擇的列from:指定被查詢的表名where:子數(shù)據(jù)集的條件group by:將數(shù)據(jù)集通過group進(jìn)行分類having:根據(jù)group by的變量得到數(shù)據(jù)子集order by:對(duì)數(shù)據(jù)集進(jìn)行排序2.2 select語(yǔ)句的特征選擇滿足條件的數(shù)據(jù)、數(shù)據(jù)分組、對(duì)數(shù)據(jù)進(jìn)行排序
4、、對(duì)數(shù)據(jù)指定格式、一次最多查詢32個(gè)表。這里還要提到的就是,在sas系統(tǒng)中,對(duì)于表名和變量名一般不超過32個(gè)字符,對(duì)于 庫(kù)名,文件引用名,格式等不能超過8個(gè)字符2.3 validate 關(guān)鍵字validate關(guān)鍵字只存在于select語(yǔ)句中、可以在不運(yùn)行查詢的情況下測(cè)試語(yǔ)句的語(yǔ)法、檢查 列名是否合法、對(duì)于不正確的查詢將打印其消息。例:1 proc sql;2 validate3 select region,product,sales4 from sashelp.shoes5 where region二'africa1;note: proc sql語(yǔ)句有有效語(yǔ)法。6 quit;此外,我們
5、還可以用noexec選項(xiàng)也可以用來進(jìn)行語(yǔ)法測(cè)試。例:7 proc sql noexec;8 select region, product,sales9 from sashelp.shoes10 where region 二'africa'note:由于noexec選項(xiàng),未執(zhí)行語(yǔ)句。11 quit;這里提示未執(zhí)行,未提示錯(cuò)誤,說明該語(yǔ)句沒有語(yǔ)法錯(cuò)誤。但是如果加入一個(gè)sashelp.shoes 表里沒有字段,這里就會(huì)出現(xiàn)錯(cuò)誤,例:12 proc sql noexec;13 select region, productsalestest14 from sashelp.shoes15
6、where region = 'africa*;error:以下這些列在起作用的表中沒有找到:test.16 quit;2.4查詢列我們可以像2.3那樣查詢指定列,也可以用*來查詢所有列。例:proc sql;select *from sashelp.shoes;quit;這里我們可以用feedback選項(xiàng)來查看到底我們選擇了哪些列:17 proc sql feedback;18 select *19 from sashelp.shoes;note: statement transtorms to:select shoes.region, shoes.product, shoes.su
7、bsidiary, shoes.stores, shoes.sales,shoes.inventory, shoes.returnsfrom sashelp.shoes;這時(shí),我們可以看到從sashelp.shoes表中選擇了 8個(gè)列2.5消除重復(fù)值我們可以用distinct選項(xiàng)來消除重復(fù)值。例如,我們要得到?jīng)]有重復(fù)的所有地區(qū)的名稱:proc sql;select distinct regionfrom sashelp.shoesquit;2.6 where子集查詢2.6.1比較運(yùn)算符先列出where語(yǔ)句用到的比較運(yùn)算符:lt<小于gt>大于eq=等于le<=小于或等于ge&
8、gt;=大于或等于nea=不等于例如,我們要查詢sales大于100000的所有數(shù)據(jù):proc sql;select *from sashelp.shoeswhere sales>100000;quit;2.6.2 in:只要滿足in里的任意一個(gè)值,表達(dá)式即為真,例如,我們要選擇region在africa 和eastern europe的所有數(shù)據(jù):proc sql;select *from sashelp.shoeswhere region in ('africa'/eastern europe');quit;2.6.3邏輯運(yùn)算符or |或and & 是n
9、ot a 非例如,選擇region在africa和eastern europe,且銷售額大于100000的所有數(shù)據(jù):proc sql;select *from sashelp.shoeswhere region in ('africa'/eastern europe') and sales>100000;2.6.4 contains或?:判斷某列是否包含指定字符串例如,選擇列region包含'afr'的數(shù)據(jù):proc sql;select *from sashelp.shoeswhere region ? 'afr'quit;2.6
10、.5 is null或is missing:判斷某列數(shù)據(jù)是否為空例如,如果找出region為空的數(shù)據(jù):proc sql;select *from sashelp.shoeswhere region is missing;quit;注意,這里我們還可以用以下表達(dá)式對(duì)where語(yǔ)句進(jìn)行替換。如果region為數(shù)值型變量, 則可以用region二.,如果region為字符型變量,則可以用region=進(jìn)行替換。2.6.6 between and:選擇某一區(qū)間的數(shù)據(jù)例如選擇sales大于100000,但小于200000的所有數(shù)據(jù):proc sql;select *from sashelp.shoesw
11、here sales between 100000 and 200000;quit;2.6.7 like:判斷是否能匹配某些字符例如,選擇以region以a開頭的所有地區(qū)proc sql;select *from sashelp.shoeswhere region like 'a%'quit;這里注意有兩類通配符,''可以通配任意個(gè)任意字符,只能通配一個(gè)任意字符2.6.8=*:類似匹配這里由于sashelp.shoes里沒有符合要求的數(shù)據(jù),所有就用書上的例子說明一下吧:where lastname=* 'smith',出來的結(jié)果可能是:smith
12、, smythe 等2.7表達(dá)式我們可以通過己有的列進(jìn)行計(jì)算來得到新的列,這時(shí)用關(guān)鍵詞as來給新的列賦列名,例如: proc sql;select region, product,sales,stores,sales/stores as salesperstoresfrom sashelp.shoesquit;這吋結(jié)果就會(huì)多一列salesperstores,用來得到該地區(qū)該產(chǎn)品每個(gè)商店的平均銷售量。這里要 注意的是,在創(chuàng)建表達(dá)式吋,我們還可以在sql里用到sas中的除lag和diff之外的所有 函數(shù)。這里我們還可以用表達(dá)式計(jì)算出來的結(jié)果來進(jìn)行子集查詢,但一定要記住用calculated關(guān)鍵 詞
13、。例如我們要找出商店平均銷售量大于5000的數(shù)據(jù):方法一:proc sql;select region, producsalestoresales/stores as salesperstoresfrom sashelp.shoeswhere sales/stores>5000;quit;方法二proc sql;select region, product,sales,stores,sales/stores as salesperstoresfrom sashelp.shoeswhere calculated salesperstores>5000;quit;2.8查詢結(jié)果展示2.
14、8.1 order by數(shù)據(jù)排序默認(rèn)的排序方式是升序,我們可以用desc關(guān)鍵詞來進(jìn)行降序排列。例如以sales降序排列 數(shù)據(jù):proc sql;select *from sashelp.shoesorder by sales desc;quit;這里提示一下,我們可以用任意多列進(jìn)行排序,包括表達(dá)式結(jié)果(不用calculated),但最好 是選擇的列。2.8.2 label 與 formatlabel:改變輸出變量名的內(nèi)容format:改變列的值的輸出方式例如,改變 salesperstores 的 label 和 formatproc sql;select region, producsale
15、s,stores,sales/stores as salesperstoreslabel='sales per stores1format=dollarl2.2from sashelp.shoes;2.9處理sql常用函數(shù)mean或avg:均值count或n或freq:非缺失值個(gè)數(shù)max:最大值min:最小值nmiss:缺失值個(gè)數(shù)std:標(biāo)準(zhǔn)差sum:求和var:方差2.9.1 求和 sumproc sql;select region, product,sales,stores, sum(sales,lnventoryreturns) as total from sashelp.sho
16、es;quit;2.9.2求均值avgproc sql;select region, product,sales,stores, avg(sales) as salesavg from sashelp.shoes;quit;2.9.3分組求均值group byproc sql;select region, avg(sales) as salesavg from sashelp.shoes group by region;quit;2.9.4 計(jì)數(shù) countproc sql;select regiorcount(*) as count from sashelp.shoes group by r
17、egion;quit;2.9.5 having數(shù)據(jù)子集proc sql;select region,count(*) as count from sashelp.shoes group by regionhaving count(*)>50;quit;其它的就不多作介紹了,多用用就熟悉了2.10子查詢2.10.1找出regions平均sales大于全部平均sales的region proc sql;select region,avg(sales) as salesavgfrom sashelp.shoesgroup by regionhaving avg(sales)>(selec
18、t avg(sales) from sashelp.shoes);quit;2.10.2 any關(guān)鍵詞介紹>any(20,30,40)最終效果:>20<any(20,30,40)最終效果:<40=any(20/30,40)最終效果:=20 or =30 or =40例如,選擇出region為united state的sales小于任意region為africa的sales的數(shù)據(jù) proc sql;select region,salesfrom sashelp.shoeswhere regionunited states1and sales<any(select s
19、ales from sashelp.shoes where region二'africa');quit;這個(gè)例子沒有多少意義,只是說明一下any的用法2.10.3 all關(guān)鍵詞介紹>all (20,30,40)最終效果:>40<all (20,30,40)最終效果:<20例如,選擇出region為united state的sales小于所有region為africa的sales的數(shù)據(jù) proc sql;select region,salesfrom sashelp.shoeswhere region='united states'and
20、salesoll(select sales from sashelp.shoes where region二'africa');quit;2.10.4 exists 與 not existsproc sql;select *from sashelp.shoeswhere exists(select * from sashelp.orsales);quit;sas中的sql語(yǔ)句完全教程之二:數(shù)據(jù)合并與建表、建視圖索引等1連接joins分為內(nèi)連接inner joins和外連接outer joins內(nèi)連接:僅返回匹配的數(shù)據(jù),最多可以有32個(gè)表同時(shí)進(jìn)行內(nèi)連接外連接:返回所有匹配的數(shù)據(jù)和
21、非匹配的數(shù)據(jù),一次只能有兩個(gè)表或視圖進(jìn)行外連接迪卡爾積:返回表內(nèi)所有可能的匹配情況。例如表a有10*20的數(shù)據(jù),表b有30*40的數(shù) 據(jù),則兩個(gè)表的迪卡爾積有(10+30) * (20+40) =40*60的數(shù)據(jù)我們先建立兩個(gè)數(shù)據(jù)集:data march;in put flight $3 +5 date date7 +3 depart time5 +2 orig $3.+3 dest $3.+7 miles +6 boarded +6 capacity;format date date7 depart time5.;in format date date7 depart time5.;card
22、s;21901mar949:31lgalon344219825062201mar9412:19lgafra385720725013201mar9415:35lgayyz36611517827101mar9413:17lgapar363513825030201mar9420:22lgawas22910518011402mar947:10lgalax247511921020202mar9410:43lgaord74012021021902mar949:31lgalon344214725013202mar9415:35lgayyz36610617820203mar9410:43lgaord74011
23、821021903mar949:31lgalon344219725062203mar9412:19lgafra385718025027103mar9413:17lgapar363514725020204mar9410:43lgaord74014821021904mar949:31lgalon344223225062204mar9412:19lgafra385713725013204mar9415:35lgayyz36611717827104mar9413:17lgapar363514625030204mar9420:22lgawas22911518011405mar947:10lgalax24
24、7511721020205mar9410:43lgaord74010421021905mar949:31lgalon344216025062205mar9412:19lgafra385718525013205mar9415:35lgayyz36615717827105mar9413:17lgapar363517725011406mar947:10lgalax247512821020206mar9410:43lgaord74011521021906mar949:31lgalon344216325013206mar9415:35lgayyz36615017830206mar9420:22lgawa
25、s2296618011407mar947:10lgalax247516021013207mar9415:35lgayyz36616417827107mar9413:17lgapar363515525030207mar9420:22lgawas229135180run;data delay;input flight $3. +5 date date7. +2 orig $3. +3 dest $3. +3 delaycat $15 +2 destype $15. +8 delay;informat date date7.;format date date7.;cards;11401mar94lg
26、alax1-10 minutesdomestic820201mar94lgaordno delaydomestic-562201mar94lgafrano delayin ter national-513201mar94lgayyz11+ minutesin ternati onal1430201mar94lgawasno delaydomestic-211402mar94lgalaxno delaydomestic020202mar94lgaord1-10 minutesdomestic521902mar94lgalon11+ minutesin ternatio nal1862202mar
27、94lgafrano delayinter national013202mar94lgayyz1-10 minutesin ternati onal527102mar94lgapar1-10 minutesin ter national430202mar94lgawasno delaydomestic011403mar94lgalaxno delaydomestic120203mar94lgaordno delaydomestic-121903mar94lgalon1-10 minutesin ternatio nal462203mar94lgafrano delayinter nationa
28、l-213203mar94lgayyz1-10 minutesin ternati onal627103mar94lgapar1-10 minutesin ternatio nal230203mar94lgawas1-10 minutesdomestic511405mar94lgalaxno delaydomestic-220206mar94lgaordno delaydomestic-321906mar94lgalon11+ minutesinter natio nal2713206mar94lgayyz1-10 minutesin ter national730206mar94lgawas
29、1-10 minutesdomestic162207mar94lgafra11+ minutesin ternatio nal2113207mar94lgayyzno delayin ternatio nal227107mar94lgapar1-10 minutesinternational430207mar94lgawasno delaydomestic0run;1.1內(nèi)連接proc sql;create table innerjoins asselectfrom march a,delay bwhere a.flight=b.flight and a.date=b.date; quit;1
30、.2外連接1.2.1 左連接 left joinproc sql;create table leftjoins asselect *from march aleft join delay bon a.flight=b.flight and a.date=b.date; quit;1.2.2 右連接 right joinproc sql;create table rightjoins asselect *from march arightjoin delay bon a.flight=b.flight and a.date=b.date;quit;1.2.3 全連接 full joinproc
31、sql;create table fulljoins asselect *from march afull join delay bon a.flight=b.flight and a.date=b.date;quit;1.3迪卡爾積 proc sql;create table cartesian asselect a.*,b.*from march a.delay b;quit;這里再大概說明一下內(nèi)外連接的實(shí)現(xiàn)的基本原理:首先生成兩個(gè)數(shù)據(jù)表的辿卡爾積,然后再 根據(jù)where語(yǔ)句來選擇符合條件的數(shù)據(jù)作為輸出結(jié)果。當(dāng)然,在實(shí)際處理過程中,sql過程 步會(huì)對(duì)這個(gè)迪卡爾積的數(shù)據(jù)集進(jìn)行優(yōu)化,將其劃分成
32、小塊數(shù)據(jù)進(jìn)行處理。2復(fù)雜的連接這里因?yàn)檎也坏胶芎玫睦觼碚f明如何處理復(fù)雜的連接,因此就不講解了,大概說一下思路 吧:簡(jiǎn)單地說,對(duì)于復(fù)雜的查詢,我們應(yīng)該將該查詢分解成幾個(gè)小的子查詢,然后對(duì)每個(gè)子 查詢的結(jié)果進(jìn)行測(cè)試,最后將所有的子查詢結(jié)合起來就組成了這個(gè)復(fù)雜的查詢。這樣會(huì)比一 來就寫復(fù)雜的查詢?nèi)菀椎枚唷?數(shù)據(jù)集set操作這里有四類 set 操作,分別是 except、intersect> union、outer unionoexcept:得到除第二個(gè)數(shù)據(jù)集以外的所有第一個(gè)數(shù)據(jù)集里的數(shù)據(jù)intersect :得到第一個(gè)數(shù)據(jù)集和第二個(gè)數(shù)據(jù)集都有的數(shù)據(jù)union:得到兩個(gè)數(shù)據(jù)集所有的數(shù)據(jù),這里如
33、果兩個(gè)數(shù)據(jù)集有相同的數(shù)據(jù),重復(fù)數(shù)據(jù)只出現(xiàn) 一次outer union:得到兩個(gè)數(shù)據(jù)集所有的數(shù)據(jù)。還冇兩個(gè)關(guān)鍵詞來修改set操作的行為:all和correspondingall:并不刪除重復(fù)值,不能與0uter union合用(注意,用all關(guān)鍵詞,一種情況是你不 管是否會(huì)有重復(fù)值,另一個(gè)情況是不可能出現(xiàn)重復(fù)值,例如有主鍵的數(shù)據(jù))corresponding:只保留兩個(gè)數(shù)據(jù)集都有的字段,一般簡(jiǎn)寫為corr3.1 except找出所有沒有延遲的航班和h期:proc sql;select flight,datefrom marchexceptselect flight,datefrom delay;q
34、uit;注意:這里重復(fù)值已經(jīng)被刪除。如果要得到所冇的包含重復(fù)值的數(shù)據(jù),就要用到關(guān)鍵詞all: proc sql;select flight,datefrom marchexcept allselect flight,datefrom delay;quit;如果只保留兩個(gè)數(shù)據(jù)集都有的字段,則用到關(guān)鍵詞corresponding (可簡(jiǎn)寫為corr): proc sql;select *from marchexcept corrselect *from delay;quit;3.2 intersect找出所有延遲的航班和口期:proc sql;select flight,datefrom marc
35、hintersectselect flight,datefrom delay;quit;關(guān)鍵詞all和corresponding與在except >|>樣,這里不再作介紹3.3 union選擇所有的數(shù)據(jù),這里的重復(fù)值只出現(xiàn)一次proc sql;select flight,datefrom marchunionselect flight,datefrom delay;quit;關(guān)鍵詞all和corresponding與在except中一樣,這里不再作介紹3.4 outer unionproc sql;select flight,datefrom marchouter unionsel
36、ect flight,datefrom delay;quit;這里,如果我們用到關(guān)鍵詞corr,就可以將相同字段的數(shù)據(jù)結(jié)合到一起:proc sql;select *from marchouter union corr select *from delay;quit;4建表4.1建空表4.1.1通過指定變量建空表proc sql noprint;create table percent(varname char(30),industry char(4),begindate num format date9. iabel=fthe beginning date1, enddate num form
37、at date9 iabel='the ending date1,p_1 num,p_5 num,p_95 num,p_99 num);quit;4.1.2拷貝數(shù)據(jù)集來建空表proc sql noprint;create table delaycopy(keep=flight date) like delay;quit;4.1.3查詢語(yǔ)句中,用outobs選項(xiàng)來建空表proc sql noprint outobs=0;create table flightll4 asselect *from delay;quit;4.2對(duì)數(shù)據(jù)表增加數(shù)據(jù)主要有三種方法:4.2.1 set 語(yǔ)句proc
38、sql noprint;insert into flightll4set flight='3o2',date='07mar94,d,orig='lga'zdest='was,/delaycat=,no delay1,destype 二'domestic:delay=0;quit;4.2.2 values 語(yǔ)句proc sql noprint;insert into flightll4values('271707mar94,d;lga';par,/,l-10minutes,;lnternational,/4);quit;4.
39、2.3條件查詢:建表和導(dǎo)入數(shù)據(jù)同時(shí)完成proc sql noprint;create table flightll4 asselect *from delaywhere flight='114,;quit;5完整性約束完整性約束的好處是保證sas數(shù)據(jù)集的連續(xù)性和正確性,它在我們更新數(shù)據(jù)或插入新的數(shù)據(jù) 時(shí),驗(yàn)證新的數(shù)據(jù)是否符合該變量的約束條件。完整性約束需要8.0以上版本,符合ansi標(biāo)準(zhǔn),可以在表建立吋或表有數(shù)據(jù)后建立約束, 但不能被用于視圖,也不能用于低于8.0版本的sas系統(tǒng)建立的數(shù)據(jù)集。五大類完整性限制:not null、check> unique、primary key&
40、gt; foreign keyonot null:不許出現(xiàn)缺失值check:指定該變量可以輸入哪些值unique:每個(gè)值必須是唯一的,其值可以為空,但只能有一個(gè)值為空primary key:主鍵,每個(gè)值必須是唯一且非空的foreign key:其它表的主鍵,即外鍵,其值為關(guān)聯(lián)表的主鍵的值且非空5.1 check例:proc sql noprint;create table percent(varname char(30),industry char(4),begindate num format date9. iabel='the begirining date1,enddate nu
41、m format date9. iabel='the ending date:percent num,constraint percent _check check(percent le 1.0);quit;這時(shí),如果我們插入的數(shù)據(jù)中,percent>l的話,將會(huì)出現(xiàn)錯(cuò)誤。5.2 回滾 rollbacks當(dāng)我們用insert或update的時(shí)候,操作要等到發(fā)牛錯(cuò)誤的時(shí)候才會(huì)停止,這時(shí)會(huì)出現(xiàn)一 個(gè)問題,就是數(shù)據(jù)表的一些數(shù)據(jù)更新了,而另一些數(shù)據(jù)沒更新,這時(shí)如果我們要回到原來的 狀態(tài),就需要用到undo_policy選項(xiàng)進(jìn)行回滾。undo_policy有三個(gè)選項(xiàng):required:缺省
42、選項(xiàng),取消所有的更新或新插入的數(shù)據(jù)。這里要注意的是,此操作不一定 完全能成功。none:阻止所有的與約朿不符的更新或新數(shù)據(jù)optoinal:取消所有可以成功取消的更新或新插入的數(shù)據(jù)本功能用得很少,所以只是翻譯一下,請(qǐng)大家查閱相關(guān)文獻(xiàn)。6創(chuàng)建視圖與索引6.1視圖視圖最大的好處是它只是一個(gè)存儲(chǔ)的查詢,因此不包含任何數(shù)據(jù),這可以減少磁盤的使用空 間。其它的功能與數(shù)據(jù)庫(kù)的表類似。創(chuàng)建視圖語(yǔ)法:create view view-name asquery-expressi on_r;例:proc sql noprint outobs=0;create view delayview asselect *fr
43、om delay;quit;6.2索引語(yǔ)法:create vunique> index index-nameon table-name (columnname,coluame);視圖和索引都用得很少,所以這里就不作講解了,大家查閱相關(guān)文獻(xiàn)吧。7維護(hù)表這里主要講一下如何更新或刪除已存在的表的數(shù)據(jù),如何對(duì)一個(gè)表增加、減少、或改變其列 的屬性,如何刪除表、視圖和索引。7.1更新數(shù)據(jù)這里主要用update來更新表的數(shù)據(jù),語(yǔ)法如下update table-nameset column-name=expression_r,set columnname=expressionwhere expressi
44、on_r;注意,這里的where -定要寫清楚,如果沒有的話,則會(huì)更新所有的數(shù)據(jù)。例:proc sql noprint;update delayset delaycat=,delay1where delay=0;quit;7.2條件處理用case語(yǔ)句來實(shí)現(xiàn)條件處理。例如,對(duì)sashelp.shoes根據(jù)sales的大小進(jìn)行分類: 語(yǔ)法:select column.case <case-operand>when when-condition then result-expression_r< when when-condition then result-expression_
45、r><else result-expression_r >end;例:proc sql;create table shoesrank asselect *,(casewhen sales>100000 then 'good1else 'bad'end) as rankfrom sashelp.shoes;quit;7.3刪除行語(yǔ)法:delete from table-namewhere expression_r;例:proc sql;delete from shoesrankwhere region二"africa:quit;7.4改變
46、列語(yǔ)法:alter table table-nameadd column-definition,column-definitiondrop column-nametolumnam巳modify columndefinition,column-definition,.対已存在的數(shù)據(jù)表增加一個(gè)列:例:proc sql;alter table shoesrankadd addcolumn num format=commal0.2,addcolumnmore char(10);quit;從己存在的數(shù)據(jù)表中刪除列,例:proc sql;alter table shoesrankdrop addcolum
47、n;quit;修改某列的屬性,例:proc sql;alter table shoesrankmodify addcolumnmore char(20) label=,modify add laber;quit;7.5刪除表、視圖、索引語(yǔ)法:drop table table-name,table-name,.;drop view view-nameview-name,.;drop index index-name,index-name,;from table-name;例:proc sql;drop table shoesrank;quit;sas中的sql語(yǔ)句完全教程之三:sql過程步的其它
48、特征1 sql過程步選項(xiàng)sql過程步選項(xiàng)的作用主要是可以從更細(xì)節(jié)的方式去控制sql過程步,并且可以在不執(zhí)行過 程的情況下對(duì)程序進(jìn)行測(cè)試等。下面介紹一下列出來的選項(xiàng),這些選項(xiàng)大多經(jīng)常用到,更多的選項(xiàng)可以參考sas幫助。 inobs:進(jìn)行一個(gè)查詢時(shí),對(duì)每個(gè)源數(shù)據(jù)表進(jìn)行n行限制,僅對(duì)這n行的數(shù)據(jù)進(jìn)行查詢。 outobs:指定查詢輸岀結(jié)果的觀測(cè)數(shù)loops:指定sql過程步內(nèi)循壞的次數(shù)(此選項(xiàng)我用得比較少,誰(shuí)明白的對(duì)以講一下,多謝) noprompt和prompt:修改上述三個(gè)選項(xiàng)的效果,從而讓你選擇是否繼續(xù)或停止選項(xiàng)的效 果。print和noprint:控制是否打印選擇的數(shù)據(jù)結(jié)果nonumber和n
49、umber:控制是否在笫一列打印觀測(cè)值編號(hào)double和nobouble:輸入報(bào)表是否隔行顯示noflow和flow和flow=n和flow=n m :指定列寬,n指定列寬,m指定行寬? ? ?1.1 double 選項(xiàng)proc sql double;select flight,datefrom marchunionselect flight,datefrom delay;quit;1.2 inobs 選項(xiàng)注意:這里inobs選項(xiàng)只讀取每個(gè)源表前10條數(shù)據(jù)進(jìn)行后續(xù)的操作,如下面的日志所示。 兩個(gè)表都只讀取10條數(shù)據(jù)進(jìn)行關(guān)聯(lián),最后得到7條關(guān)聯(lián)好的數(shù)據(jù)。237 proc sql inobs=20;
50、238 create table tmp as239 select a.*,b.*240 from march a,delay b241 where a.flight=b.flight and a.date=b.date;warning:變量“flight”已經(jīng)存在于文件 work.tmp中。warning:變量“date”已經(jīng)存在于文件 work.tmp中。warning:變量“orig”已經(jīng)存在于文件 work.tmp中。warning:變量“dest”己經(jīng)存在于文件work.tmp中。warning: only 10 records were read from work.delay (
51、別名二b) 由于 inobs=選項(xiàng)。warning: 0nly 10 records were read from work.march (別名二a) 由于 inobs=選項(xiàng)。note:表 work.tmp創(chuàng)建完成,有7行,11歹i。1.3 outobs 選項(xiàng)這個(gè)選項(xiàng)的功能與inobs相似,不同的是,這個(gè)選項(xiàng)指定結(jié)杲的觀測(cè)值數(shù)量,而不是讀取源 表的數(shù)量,這在我們控制輸出結(jié)果的數(shù)據(jù)量吋非常有用。243 proc sql outobs=10;244 create table tmpl as245 select a.*,b.*246 from march a,delay b247 where a.f
52、light=b.flight and a.date=b.date;warning:變量“flight”已經(jīng)存在于文件work.tmp1中。warning:變量“date”己經(jīng)存在于文件work.tmp1中。warning:變量“orig”已經(jīng)存在于文件work.tmp1屮。warning:變量“dest”已經(jīng)存在于文件 work.tmp1中。warning:語(yǔ)句由于outobs=10選項(xiàng)而過早終止。note:表 work.tmp1創(chuàng)建完成,有10行,11歹i。1.4 prompt 選項(xiàng)這里prompt選項(xiàng),可以讓你選擇是否還是按照原來的設(shè)置進(jìn)行動(dòng)作,或者繼續(xù)操作。如下 面的口志所以,如果直接選
53、擇停止,則得到1.2的結(jié)果,如果一直按繼續(xù),則得到如下結(jié)果:255 proc sql inobs=10 prompt;256 create table tmp3 as257 select a.*,b.*258 from march a,delay b259 where a.flight=b.flight and a.date=b.date;warning:變量“flight”已經(jīng)存在于文件 w0rk.tmp3中。warning:變量“date”已經(jīng)存在于文件w0rk.tmp3中。warning:變量“orig”已經(jīng)存在于文件w0rk.tmp3中。warning:變量“dest”己經(jīng)存在于文件w0rk.tmp3中。note:表 w0rk.tmp3創(chuàng)建完成,有19行,11列。1.5 number 選項(xiàng)proc sql outobs=4 number;select * from march;quit;1.6 reset 選項(xiàng)對(duì)上述sql語(yǔ)句加上reset選項(xiàng),使其不輸出序號(hào)proc sql outobs=4 number;reset nonumber;select * from march;quit;2 dictionary2.1 diction
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年血液透析機(jī)(人工腎)合作協(xié)議書
- 2025年基因工程乙型肝炎疫苗(酵母)合作協(xié)議書
- 2024-2025學(xué)年河南省鄭州市管城區(qū)四年級(jí)(上)期末數(shù)學(xué)試卷
- 2025年排氣系統(tǒng)管件合作協(xié)議書
- 人教版期中測(cè)試卷-四年級(jí)數(shù)學(xué)下冊(cè)(培優(yōu)卷)(含解析)
- 2025年九年級(jí)教研組年終總結(jié)樣本(3篇)
- 2025年乳膠漆粉刷施工合同協(xié)議(2篇)
- 2025年產(chǎn)品買賣合同范文(2篇)
- 2025年二手房分期購(gòu)房合同(三篇)
- 2025年個(gè)人車位租賃合同參考模板(三篇)
- 藥膳與食療試題及答案高中
- 手術(shù)室植入物的管理
- 電力供電系統(tǒng)試驗(yàn)調(diào)試和聯(lián)調(diào)方法
- Unit6AtthesnackbarStorytimeDiningwithdragons(課件)譯林版英語(yǔ)四年級(jí)上冊(cè)
- 滬教版 九年級(jí)(上)數(shù)學(xué) 秋季課程 第12講 統(tǒng)計(jì)初步(解析版)
- 2023年四川省公務(wù)員錄用考試《行測(cè)》真題卷及答案解析
- 機(jī)電一體化系統(tǒng)設(shè)計(jì)-第5章-特性分析
- 2025年高考物理復(fù)習(xí)壓軸題:電磁感應(yīng)綜合問題(原卷版)
- 《辛德勒的名單》電影賞析
- 20S515 鋼筋混凝土及磚砌排水檢查井
- 雨棚鋼結(jié)構(gòu)施工組織設(shè)計(jì)正式版
評(píng)論
0/150
提交評(píng)論