![編程data步程序簡介_第1頁](http://file3.renrendoc.com/fileroot_temp3/2022-3/17/3489ac8d-f714-4d12-8e76-96d2d7fab893/3489ac8d-f714-4d12-8e76-96d2d7fab8931.gif)
![編程data步程序簡介_第2頁](http://file3.renrendoc.com/fileroot_temp3/2022-3/17/3489ac8d-f714-4d12-8e76-96d2d7fab893/3489ac8d-f714-4d12-8e76-96d2d7fab8932.gif)
![編程data步程序簡介_第3頁](http://file3.renrendoc.com/fileroot_temp3/2022-3/17/3489ac8d-f714-4d12-8e76-96d2d7fab893/3489ac8d-f714-4d12-8e76-96d2d7fab8933.gif)
![編程data步程序簡介_第4頁](http://file3.renrendoc.com/fileroot_temp3/2022-3/17/3489ac8d-f714-4d12-8e76-96d2d7fab893/3489ac8d-f714-4d12-8e76-96d2d7fab8934.gif)
![編程data步程序簡介_第5頁](http://file3.renrendoc.com/fileroot_temp3/2022-3/17/3489ac8d-f714-4d12-8e76-96d2d7fab893/3489ac8d-f714-4d12-8e76-96d2d7fab8935.gif)
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、1、建立數(shù)據(jù)集data MM;input a $ b c;m=b+c;n+1;/*直接聲明一個(gè)變量n,數(shù)據(jù)為累加型定義時(shí)不需要等號(hào)*/label m=results;cards;aa 1 2bb 3 4 cc 5 6 ;run;proc sort data=MM;/*如果前面使用了rename語句對(duì)變量名重新進(jìn)行了賦值的話,在排序時(shí)須使用新的變量名多級(jí)排序先總后分,與sql中相同*/by descending m ;run;proc print data=MM;var _numeric_;var _character_;titlechulijieg;run;注意:整體步驟為先生成數(shù)據(jù)集,然后再
2、排序;最后輸出結(jié)果;Var之后要有空格;排序時(shí),by desecending m;必須用變量名m,不可用 標(biāo)簽result;A為字符串時(shí),需在后面加符號(hào)$,不規(guī)定字節(jié)數(shù)的話不用加點(diǎn);數(shù)據(jù)輸入時(shí),每個(gè)對(duì)象須單獨(dú)起一行;數(shù)據(jù)輸入結(jié)束時(shí),分號(hào)必須另起一行;下劃線的輸入:減號(hào);若 var numeric 在先,則輸出時(shí)數(shù)字列在前;若character在先,則輸出時(shí)字符列在先;2、生成空數(shù)據(jù)集data t2;length name $8. percent 8.;/*定義兩個(gè)變量name、perceng的類型、長度,各參數(shù)均不可省略*/stop;/*沒有stop的話會(huì)生成缺省變量*/run;2 用infi
3、le語句導(dǎo)入外部數(shù)據(jù) data graph1; infile e:number.txt delimiter=09x;/*數(shù)據(jù)以txt文檔存儲(chǔ),位置為e:number.txt直接從excel中黏貼至文本文檔,分隔符為制表符制表符的表示方法為 09x */ input a b c d; /*因前面已經(jīng)指明分隔符為制表符,且變量皆為數(shù)值型,故雖未再進(jìn)一步說明,程序仍會(huì)自動(dòng)以分隔符分割變量*/ run; proc print; run;linesize 語句:指定行長度,可突破262字節(jié)的限制2、讀取指定位置的文件在e盤建立文件夾ee,其中一份sas數(shù)據(jù)集名為data1.讀取data1 的程序?yàn)閘ib
4、name tt1 e:ee;/*將文件及ee的路徑指定為 tt1*/data a;set tt1.data1;/*復(fù)制 路徑tt1下名為 data1的數(shù)據(jù)集*/run;若要獲取data1 的頭文件:不平衡分組數(shù)據(jù)的讀入data a;do j=1 to 2;input n;do i = 1 to n;input x y ;output;end;end;drop n i;cards;8/*必須換行,程序讀數(shù)據(jù)8之后,轉(zhuǎn)向執(zhí)行do i語句,再次讀取數(shù)據(jù)時(shí)指針會(huì)自動(dòng)跳轉(zhuǎn)至下一行*/1 11 2 22 3 33 4 44 5 55 6 66 7 77 8 8851 51 2 52 3 53 4 54 5
5、 55;run;3 delimiter 定義分隔符data new1; infile cards delimiter=,;/*定義逗號(hào)為分隔符注意。 Infile cards語句需放在下面的 input變量語句之前*/ input x y z; /*輸入至一行介紹時(shí)會(huì)自動(dòng)結(jié)束。轉(zhuǎn)至下一行,但若定義了字符型變量長度的話則不會(huì)開始下一變量輸入,而是將下一行數(shù)據(jù)繼續(xù)認(rèn)為是未輸入完的變量的一部分*/ cards;1,2,34,5,6;data new2; infile cards dlm=ab; /*a b的所有組合形式均將被認(rèn)為是分隔符*/input x y z;cards;1aa2ab34bb5b
6、a67a8b9;run;proc print data=new1;run;proc print data=new2;run;3.永久存儲(chǔ)變量libname number e:;/*首先指定物理路徑*/data number.first/*命名時(shí)物理路徑在前*/(drop=blabel=analysisrename=(a=location b=time c=proct d=wanted);/*drop語句在先,則變量b不會(huì)進(jìn)入數(shù)據(jù)集,對(duì)b進(jìn)行的命名等操作也不會(huì)進(jìn)行*/infile e:number.txt delimiter=09x;input a b c d;run;proc sort dat
7、a=number.first;by descending location;/*a變量名稱已改為location*/run;proc print;run;4 將數(shù)據(jù)拆分為多列data a; input type $ ;/*表示下面的操作均在這一行進(jìn)行*/ if type=c then input course $ prof $;/*then 后面需用 input命令*/ else if type=s then input name $ id 3.2; cards; c math zxs s zhao 58888 ; proc print; run;5 讀取指定數(shù)據(jù)Data a; input 1
8、 x 5.2 6 y 2.1 +2 z #2 xx;/*1置于變量前,表示從第一列開始讀*/*5.2 共占五位,小數(shù)點(diǎn)后占兩位*/*+2置于變量前,表示向后跳兩列讀取。注意,是在將要讀取的列上向后跳,而不是以已讀完的列為基點(diǎn)向后跳*/*#2 從第二行開始讀取*/ cards;234;proc print;run;6 dsd : & 字符輸入data topics9; infile datalines dsd;/* dsd 將引號(hào)內(nèi)的內(nèi)容作為一個(gè)字符串整體(不再輸出引號(hào),缺省的話引號(hào)內(nèi)的部分將可能被拆開),默認(rèn)逗號(hào)為分隔符(可通過 delimiter= 語句修改分隔符類型),若遇見相鄰的分隔符,
9、則認(rèn)為中間有一缺省值在設(shè)置輸出格式時(shí),若變量內(nèi)包含逗號(hào),則使用雙引號(hào)將該變量包圍,以避免與分隔符混淆沒有的情況下,認(rèn)為換行也是分隔符 */ input speaker : $25. title $40. location & $10.;/*: 第一次遇見分隔符時(shí)即認(rèn)為該變量輸入結(jié)束*/* 允許輸入值中保留引號(hào),遇見引號(hào)右側(cè)的第一個(gè)分隔符即認(rèn)為該變量輸入結(jié)束1、 若變量自左引號(hào)開始讀取,至右引號(hào)位置發(fā)現(xiàn)分隔符,但仍不足規(guī)定位數(shù)時(shí),則自動(dòng)認(rèn)為該變量輸入結(jié)束;2、若至右引號(hào)處未發(fā)現(xiàn)分隔符,且仍有字符,則繼續(xù)讀取,直至發(fā)現(xiàn)分隔符或讀取了規(guī)定長度的字符;3、若已讀取了規(guī)定位數(shù),但仍未發(fā)現(xiàn)右引號(hào)及分隔符,
10、則自動(dòng)結(jié)束該變量讀取,余下的內(nèi)容及右引號(hào)、右引號(hào)右側(cè)與分隔符之間的內(nèi)容都將不被輸入,指針直接跳至右引號(hào)右側(cè)的第一個(gè)分隔符處。若向右一直未發(fā)現(xiàn)分隔符,指針將一直跳至換行符處4、沒有dsd的話,若使用,則引號(hào)內(nèi)的空格(分隔符)也將導(dǎo)致變量輸入結(jié)束,因?yàn)橐?hào)內(nèi)的部分在沒有dsd的情況下不被認(rèn)為是一個(gè)字符串整體*/*& 允許字符變量中有空格,或某變量全為空格。此為默認(rèn)選項(xiàng)& : 同時(shí)有& :時(shí),位置在前的&生效,:不再起作用*/*定義字符型變量長度時(shí),$與表示長度的數(shù)字和點(diǎn)必須連接在一起,不能有空格*/*既沒有 dsd也不規(guī)定字符長度的話,系統(tǒng)一直尋找至數(shù)據(jù)再開始讀取,不計(jì)算其中有多少分隔符 既沒有:
11、也沒有的話,程序優(yōu)先考慮按照字節(jié)長度劃分變量,而非分隔符規(guī)定了字符長度時(shí),讀取字符之后讀取相鄰的數(shù)字?jǐn)?shù)據(jù)時(shí)仍不受分隔符個(gè)數(shù)影響,但讀取字符數(shù)據(jù)時(shí)受其影響*/ datalines;/*datelines 與cards通用*/ Whitfield,Looking at Lift,Blue Room Puentes,Red Room Townsend,Peace in Our Times,Green Room ; proc print ; run;方法2data b;infile datalines delimiter=,;input x1 $ x2 $30. x3 &$15.;cards;Whit
12、field,Looking at Lift,Blue RoomPuentes,Red RoomTownsend,Peace in Our Times,Green Room;run;使用空格作為分隔符data b;infile datalines dsd delimiter= ;input x1 :$10. x2 $30. x3 $15.;/*沒有dsd的話,若使用,則引號(hào)內(nèi)的空格(分隔符)也將導(dǎo)致變量輸入結(jié)束,因?yàn)橐?hào)內(nèi)的部分在沒有dsd的情況下不被認(rèn)為是一個(gè)字符串整體*/cards;Whitfield Looking at Lift Blue RoomPuentes Red RoomTown
13、send Peace in Our Times Green Room;run;proc print;run;另例data a;infile cards dsd delimiter=;/*需定義分隔符且直接輸入數(shù)據(jù)時(shí),在input語句前需使用infile cards語句*/* dsd :分隔符后直接換行將被認(rèn)為是出現(xiàn)一個(gè)缺省值*/*數(shù)據(jù)直接黏貼在sas中時(shí),excel的制表符應(yīng)先替換為空格,否則第二變量僅含有一個(gè)字符時(shí)易出錯(cuò)*/input a b $50. ;/*因?yàn)榭崭駷榉指舴欠裨趇nput語句中使用&,可參考運(yùn)行結(jié)果確定 */cards;1119- 1-25112- 11 -319, 5
14、0- 11; run;proc print;run;定長、含分隔符數(shù)據(jù)的讀入(:的作用)data a;input (x1-x5) ($1.);cards;ab/cdefgh/ig/k;run;data _null_;file e:ooo.txt delimiter=?;set a;put x1-x5 ;run;data a2;infile e:ooo.txt delimiter=?;input x1 :$1. x2 :$1. x3 :$1. x4 :$1. x5 :$1. ;/* 指定分隔符及長度時(shí),需有:,否則分隔符會(huì)被認(rèn)為是字符而讀取至結(jié)果中。本例中,讀取第一個(gè)變量a后,字節(jié)長度被滿足,程
15、序?qū)⒆詣?dòng)認(rèn)為第二個(gè)變量的讀取開始,在沒有:的情況下,字節(jié)長度被滿足前任何內(nèi)容(包括分隔符)都將被認(rèn)為是要輸入的內(nèi)容*/run;7 多次重復(fù)輸入文本data;file print; /*file print;*/ /* change the output window*/ put 1230*SAS;/*1230*文本 將 文本 輸入1230次 */ run; proc print ; run;8 顯示控制data put;file print; input x y; z=x+y; put _infile_;/*每次運(yùn)行均顯示輸入的數(shù)值 */ put _all_;/*所有變量、內(nèi)置變量*/ car
16、ds;10 20100 2001000 2000;run;data put;file print; input x y; z=x+y; put _infile_;/*每運(yùn)行一次,即將下列值顯示一次*/ put _N_;/*顯示已經(jīng)運(yùn)行的次數(shù)put _page_ 意為在新窗口中輸出;*/ cards;10 20100 2001000 2000;run;9 set語句合并數(shù)據(jù)集data a; input id ming $ sex $ ; cards; 1 MARY F 3 ANN F 4 TOM M ;/*生成數(shù)據(jù)集a*/data b; input id name $ sex $ ; cards
17、; 2 JOSE F 5 ERIE M 6 MAY F 1 MARY M; /*生成數(shù)據(jù)集b*/data result; set a(rename=(ming=name) in=a) b(in=inb);/*同時(shí)復(fù)制a、b,注意,a、b的數(shù)據(jù)結(jié)構(gòu)、類型均相同 進(jìn)行邏輯判斷,數(shù)值是否存在于b數(shù)據(jù)集中 Set語句后分號(hào)斷開*/ by id; if inb=1 then bonus=500;/*如果是的話,那么變量bonus的值為500,否則為空*/run;proc print; title;run;10 選擇復(fù)制數(shù)據(jù)程序1有問題data zxs; do n=2 to total;/* do語句*/
18、 set sasuser.flow point=n nobs=total;/*point=開始位置Nobs=結(jié)束位置Obs=結(jié)束位置 */ /*if _error_=1 then abort;*/ output; end; stop; run;PROC PRINT data=zxs;RUN;data d3;do n = 3 to 6;set d2 point=n;time+1;drop _NAME_;output;end;stop;/* stop可防止程序進(jìn)入死循環(huán)*/run;data a;input x;cards;123456;run;data b;do i=5 to 6; set a p
19、oint=i;output;end;stop;run;11 merge語句合并數(shù)據(jù)集data person; input name $ sex $; cards;MARY FJNN FTOM M ;data place; input name $ city $ region; cards;MARY MIAMI 2JNN TAMPA 6JOSE ERIE 5MARY TAMPA 7; /*proc sort data=person; by name;proc sort data=place; by name;進(jìn)行排列前需先進(jìn)行排序*/data result; merge person place
20、;/* 全部姓名均會(huì)出現(xiàn),最好是一對(duì)一合并*/ by name;proc print; titleDATA SET RESULT;run;示例data a;input x y $;cards;1 a2 b3 d4 f;run;data b;input x z;cards;1 23 456 788 90;run;data c;merge a b;by x;run;12 updata 升級(jí)數(shù)據(jù)集data c; update a b;/*以數(shù)據(jù)集b中的值覆蓋a中的值*/ by id ;/*以id為共同字段進(jìn)行修改注意,每行的id字段必須是唯一的,不可兩行相同且所有的id均會(huì)出現(xiàn)*/run;示例dat
21、a a;input x y $;cards;1 a2 b3 d4 f;run;data b;input x y $;cards;1 23 456 788 90;run;data c;update a b;by x;run;13 以dsd進(jìn)行分隔data new2; infile cards dlm=ab;/*以a、b的各種組合均被識(shí)別為分隔符*/input x y z;cards;1aa2ab34bb5ba67a8b9;run;14 missover/flowover/truncover/stopover處理數(shù)據(jù)行末尾Infile datalines misover;數(shù)據(jù)行末尾位數(shù)不足規(guī)定位數(shù)
22、時(shí)作為缺省值處理Infile datalines truncover;數(shù)據(jù)行末尾位數(shù)不足規(guī)定位數(shù)時(shí)直接讀入Infile flowover;數(shù)據(jù)行末尾位數(shù)不足規(guī)定位數(shù)時(shí)讀入下一條記錄Infile cards stopover;報(bào)告出錯(cuò),中止數(shù)據(jù)讀取15 pad lreclInfile 物理路徑 pad;源數(shù)據(jù)文件行寬度不夠時(shí),在最后以空格補(bǔ)齊16 deletedata jn; input a b c; file print; put _n_; if a3 then delete;/*如果a3,則停止當(dāng)前觀測,回到DATA步開頭開始處理下一個(gè)觀測值*/ *put _n_; *total=a+b;
23、cards;1 2 33 3 25 3 13 3 3;proc print;run;17 lostcard語句處理缺失變量data ins; input id 1-3 reject 8-10 #2 idc 1-3 pass; if id ne idc then lostcard;/*如果id與idc不相等的話,自動(dòng)處理下一個(gè)兩行相等的數(shù)據(jù)若使用delete語句的話,則所涉及的兩行數(shù)據(jù)都會(huì)被刪除,并可能導(dǎo)致整個(gè)程序無法運(yùn)行所以設(shè)計(jì)行數(shù)據(jù)要合并在一行是應(yīng)選用lostcard語句若使用Delete語句,則程序讀入411 46,然后在數(shù)據(jù)集中刪除這一行的四個(gè)數(shù)據(jù),再從第二個(gè)411處(411 99551
24、)向下運(yùn)行,搜尋下一個(gè)411。Lostcard語句中,指針在400數(shù)據(jù)運(yùn)行時(shí),搜尋完源數(shù)據(jù)中的下一行(411 46),發(fā)現(xiàn)與400不相等時(shí),刪除數(shù)據(jù)集中的400行,指針會(huì)停留在原數(shù)據(jù)中的這個(gè)411處(411 46),將其讀入一個(gè)新的數(shù)據(jù)行,并搜尋下一個(gè)411*/cards;301 32301 61432302 53302 83171400 92845411 46411 99551;proc print; title ;run;三行數(shù)據(jù)的情況data ins; input id 1-3 reject 8-10 #2 idc 1-3 pass #3 mnp 1-3; if id ne idc or
25、 idc ne mnp or id ne mnp then lostcard; /*delete input*/cards;301 32301 61432301 53302 83171302 83172411 92845411 46411 99551;proc print; title ;run;18 where語句進(jìn)行條件運(yùn)行data aa; set sasuser.business; where employs50; *where industry=Food; *where employs between 50 and 70;/*是否在50-70之間*/ *where employs in
26、 (61,62);/*是否為61,62這兩個(gè)數(shù)據(jù)中的一個(gè)*/ *where employs is missing; *where industry ? Ele; /* ? means contains*/ *where industry like %t%; /* % can substitute many char 代表任意字符,整個(gè)語句意為包含字母t*/ *where industry like O_l; /* _ can substitute one char _代表一個(gè)字符*/run;proc print;run;示例data a;input x1 $;cards;aaartyytyyr
27、ooiprbip;run;data b;set a;where x1 ? t;/* ? 表示包含*/run;data c;set a;where x1 like %a%;run;data d;set a;where x1 like %r_i%;run;data e;set a;where x1 like %r_i%;/*每個(gè)橫線代表一個(gè)字符*/run;多個(gè)條件data a;do i= 1 to 10;output;end;run;data b;set a;wherei gt 2 and i lt 4;run;字符型data a;input x1 $;cards;abcsdabcabcmdabc
28、mmmsdacoabcsdacossfddg;run;data b;set a;wherex1 like %a_c%;run;data c;set a;wherex1 like %a_c% and x1 like %s_c%;run;19 do 循環(huán)語句data a;input x ; do i=1 to 4; y=4*x; output;end;/*每讀入一個(gè)變量,則將do與end之間的語句循環(huán)四次不加0utput語句的話則只輸出最后一次運(yùn)行后的y值,其余不顯示*/ cards; 11 22 33 44 55 66 ; run;proc print;run;20 output 語句進(jìn)行輸出D
29、ata步每次迭代時(shí),后臺(tái)自動(dòng)進(jìn)行output,但若主動(dòng)加上output語句,則取消自動(dòng)輸出,變?yōu)閮H在條件下輸出。data;file print;input a b;if a=1 then output;/*僅當(dāng)a=1時(shí)將結(jié)果輸出Output語句亦可單獨(dú)使用,見下例*/cards;2 31 52 41 6;run;proc print;run;21 output 結(jié)果輸出在幾個(gè)數(shù)據(jù)集中data year81 year82 year83;/*同時(shí)創(chuàng)建3個(gè)數(shù)據(jù)集*/ input year x1-x2; if year=1981 then output year81; else if year=198
30、2 then output year82; else if year=1983 then output year83;/*數(shù)據(jù)集year83的前面不需要 data */ cards;1981 1 21982 2 31983 3 4;run;proc print;run;22 Output 將一行變量拆分為多行data repeat; input subject $ meal1-meal3; /*此處亦可 drop=meal1-meal3;意為不輸出這三個(gè)變量的值*/ mea=meal1;output; /*定義一個(gè)變量mea,其值等于meal1,輸出此行關(guān)鍵是output導(dǎo)致了本行的輸出,并最
31、終使本行被輸出了三次*/ mea=meal2;output; mea=meal3;output; cards;a 2 5 4b 3 6 2;proc print;run;23 output 將多行數(shù)據(jù)運(yùn)算為一行(見24)24 Set 語句進(jìn)行分組分別運(yùn)算data subtotal; set pay; by area;/*產(chǎn)生每組的首末識(shí)別*/ if first.area then total=0;/*在后臺(tái)聲明一個(gè)參數(shù)total,初始值為0,并迭代保存若使用input 語句聲明了變量,則每次運(yùn)行都從初始聲明的值開始,不進(jìn)行迭代保存*/ total+amount;/*if語句以分號(hào)結(jié)束后,其下面
32、的語句的執(zhí)行與if無關(guān),即每讀入一行數(shù)據(jù)都將執(zhí)行注意,自身迭代賦值變量的語法不需要等號(hào)*/ drop amount; if last.area then output;/*僅在組結(jié)束行輸出運(yùn)算結(jié)果,故將實(shí)現(xiàn)了多行運(yùn)算的為一行 */run;proc print;run;24 call語句調(diào)用函數(shù)data b; seed3=161321804; seed4=936674311; do i=1 to 5; call ranuni(seed3,x3);/*X3 意為結(jié)果識(shí)別為變量X3 */x4=ranuni(seed4);/*效果同 call rannui(seed4, x4) */ output;
33、end;run;25 do while data while; n=0; do while (n=4);/*n=4時(shí)也不再執(zhí)行 即邊界即停止即使條件為n=0,程序也會(huì)完整運(yùn)行一次*/ file print; put n=;n+1; end;run;27 do exit 規(guī)定結(jié)束標(biāo)準(zhǔn)的do語句data a;input x;exit=5;/*5次即結(jié)束*/y=0;i=1;do i=1 to exit;y+x;if y=5 then i=exit;/*y大于等于5即結(jié)束*/output;end;cards;123;run;proc print;run;27 select(when) 條件運(yùn)行data
34、 old; input grade $ ; cards;A B1 B2 B3 C D;run;data new; set old ; x=1000; select(grade); when(A) salary=x*1.5; when(B1,B2,B3) salary=x*1.3;/*多個(gè)時(shí)以逗號(hào)分隔*/ when(C) salary=x*1.1; otherwise salary=x;end;run;PROC PRINT;RUN;28 if語句 條件執(zhí)行(條件為表達(dá)式)data;input x ;if x=1 then y=x;else if x=2 then y=2*x;/*其他情況時(shí),須用
35、else if,其中的if不可省略或用 otherwise*/else if x ne 1|x ne 2 then y=x*10;/*|表示 or 即或者的意思/cards;1 2 3 4 5 6 7 8;proc print;run;29 if語句條件執(zhí)行(條件為變量,處理缺失、0值情況)data aa; input x ; if x then y=x;/*如果x的值缺失或?yàn)?時(shí)則不運(yùn)行*/ cards;1 0 . 4;run;proc print;run;30 return語句 輸出并返回data survey; input x y z; if x=y then return;/*輸出目前
36、已有的結(jié)果,并返回data步的開頭,執(zhí)行下一行變量*/ x=y+z ; a=x*2; cards;1 1 32 3 3;/*每個(gè)data步的結(jié)尾都隱含一個(gè)return語句*/proc print;run;31 goto 跳轉(zhuǎn)語句data info; input x ; file print; if 1=x=5 then goto ok; /*goto后必須跟標(biāo)簽,且標(biāo)簽必須也在這一data步中*/ put x;/*此句與if無關(guān) */ count+1;/*此句與if無關(guān) */ ok:y+x;/*ok為與goto對(duì)應(yīng)的標(biāo)簽當(dāng)不執(zhí)行g(shù)oto語句時(shí),ok后的語句也將被執(zhí)行 即ok語句的執(zhí)行與goto
37、無關(guān) */ cards; 7 2 16 5 323 1 11 ;proc print;run;32 link 跳轉(zhuǎn)用于if語句下層還需進(jìn)一步分類執(zhí)行的情況data; input type $ wd station $ ;elev=.;if type=ALUV then link calcu;/*calcu為標(biāo)簽*/year=1985;return;/*整個(gè)程序的結(jié)束,不可缺少,否則繼續(xù)向下運(yùn)行 */calcu: if station=SITE_1 then elev=6650-wd;if station=SITE_2 then elev=5500-wd;return;/*calcu步運(yùn)行至 r
38、eturn時(shí)結(jié)束,但并不跳轉(zhuǎn)至data步開頭,而是跳轉(zhuǎn)至link語句后的程序并開始運(yùn)行*/cards;ALUV 523 SITE_1UPPA 234 SITE_2ALUY 666 SITE_2;proc print;run;33 continue 停止當(dāng)前數(shù)據(jù)行,進(jìn)行下一循環(huán)s34 leave 停止循環(huán),返回主程序data week; input name $ idno start; bonus=0; do year=start to 1991; if bonus ge 500 then leave; /*中止循環(huán)時(shí)將輸出后臺(tái)保留的最后一次成功運(yùn)行所得到的bonus值*/ bonus+50;e
39、nd;cards;Jones 9011 1990 Thomas 876 1976 Barnes 7899 1991 Harrell 1250 1975 Richards 1002 1990 Kelly 85 1981 Stone 091 1990 ;PROC PRINT;RUN;35 do(do) 嵌套的do語句data; capital=1; /*每次總循環(huán)開始時(shí)的初始值須最先賦值給定,即使是0也須定義,不可缺省*/ do year=1 to 2; capital+1; do month=1 to 2;interest=capital+1;capital+interest;end; end;
40、 run; /*1.1:capital:4000 interest:25 total=4025 1.2:capital:4025 interest:25.15 total=4050.15 2.1:capital:6050.15 interest:37.81 total=6087.81 2.2:capital:6087.81 interest:38.04 total=6126*/ proc print; run;運(yùn)行結(jié)果為51,所有結(jié)果均記錄在日志中,只輸出最后一次的計(jì)算結(jié)果36 嵌套的do select語句data; file print; do mon=JAN,FEB,MAR,APR,MAY
41、,JUN,JUL,AUG,SEP; select ; when(mon=MAR|mon=APR|mon=MAY)put springmon=; when(mon=JUN|mon=JUL|mon=AUG)put summermon=; otherwise put fallmon=; end;end;run;37 array語句定義數(shù)組數(shù)組內(nèi)元素屬性必須一致,即全為數(shù)值或全為文本通過數(shù)組運(yùn)算可實(shí)現(xiàn)同一行內(nèi)的迭代運(yùn)算data; array x3 a b c (1 2 3); /*array 名稱 變量個(gè)數(shù)說明 變量列表 變量初始值定義數(shù)組變量用大括號(hào)*/ file print; put x1= x2
42、= x3=; run;data; array x2,3 aa bb cc dd ee ff(1 2 3 4 5 6);/*兩行三列*/ file print; put x2,1=; run;data; array x2 $ aaa bbb(data process);/*文本型,需$符號(hào),初始變量列表中需要用引號(hào)??崭駷榉指舴?/ file print; put x2=; run;data; array x6:9,0:9 x60-x99; /*6、7、8、9,共四行 0-9,共10列*/ file print; put x7,0;/*行號(hào)為7,列號(hào)為0的元素,即第二行第一個(gè)*/ run;實(shí)例d
43、ata a;input x1-x15 ;cards;1 2 3 4 5 6 7 8 9 11 12 13 21 22 23;run;data b;set a;array mm1:5,1:3 x1-x15;file print;put mm5,2;run;/*數(shù)據(jù)需在一行,僅僅是綁定名稱,并不影響數(shù)據(jù)在表中的排列-仍為一行,與表a相同*/38 dim 函數(shù)計(jì)算數(shù)組元素個(gè)數(shù)用于橫向迭代data zz; input d1-d7; array day(*) d1-d7;/*通過變量個(gè)數(shù)確定數(shù)組下標(biāo)。本例中為7*/ do i=1 to dim(day); day(i)=day(i)+10; end; d
44、rop i; cards;99 11 22 222 22 3 44 1 2 3 4 5 6 7;run;PROC PRINT;RUN;39 Array 數(shù)組名稱(變量個(gè)數(shù))_temporary_臨時(shí)數(shù)組data a;input var1 var2 var3;cards; 10 20 30100 . 300. 40 400;run;data b(drop=i);set a;array array1(3) _TEMPORARY_ (.1 .2 .3) /*三個(gè)元素,0.1,0.2,0.3,空格為分隔符*/;array array2(3) var1 var2 var3;/*三個(gè)變量,var1-var
45、3,與array1同*/ do i=1 to 3; if array2(i)=. then array2(i)=array1(i);/*i位于元素名稱位置*/ end;proc print;run;另例 3.2橫向迭代運(yùn)算示例data a;input x1-x5;cards;1 2 3 4 52 3 4 5 64 5 6 7 8;run;data b;set a;array test(5) x1-x5;prod=0;do i=1 to dim(test)-1; if i=1 then prod=test(i); prod=prod*test(i+1);end;/*分行運(yùn)行,運(yùn)行完一行及輸出*/
46、proc print;run;另例 3.3 關(guān)于空白文本元素的替代data a;input (x1-x3) ($);cards;a b cd . f. h i;/*輸入數(shù)據(jù)時(shí),空白文本元素也用點(diǎn)表示*/run;data b;set a;array n(3)$ _temporary_ (m m m);/*文本元素需用引號(hào)*/array test(3)$ x1-x3;do i=1 to 3;if test(i)= then test(i)= n(i);/*if語句中,空白字符數(shù)據(jù)用表示*/end;run;proc print;run;40 array 隱含變量數(shù)量數(shù)組data test; inpu
47、t sco1-sco5; array s sco1-sco5;/*定義數(shù)組變量 s 時(shí)未列出下標(biāo),則自動(dòng)默認(rèn) _i_為下標(biāo)變量,且_i_值為變量元素個(gè)數(shù)*/ do _i_ =1 to 5;s=s*100;/*_i_含義與一般意義的i相同,代表數(shù)組元素個(gè)數(shù)對(duì)s操作,s有明確定義,為數(shù)值中所對(duì)應(yīng)元素,非迭代產(chǎn)生*/ end; cards; 0.95 0.88 0.57 0.90 0.65 1 2 3 4 5 ; run;PROC PRINT;RUN;41 do over 對(duì)隱含下標(biāo)數(shù)組的do重復(fù)操作data test; input sc01-sc05; array s sc01-sc05; do
48、over s;/*同 do I = 1 to k,k為數(shù)組元素?cái)?shù)量即對(duì)所有數(shù)組元素均執(zhí)行該操作*/ s=s*100;end;cards;0.95 0.88 0.57 0.90 0.651 2 3 4 5;PROC PRINT;RUN;42 informat語句 定義讀取時(shí)數(shù)據(jù)格式(缺省時(shí))data infex; informat default=3.1 default=$char4. x1 2.2;/*informat語句規(guī)定了數(shù)據(jù)格式通用規(guī)定,除x1的格式在程序中做了專門說明外,其他均為缺省。此時(shí)自動(dòng)執(zhí)行informat語句中的規(guī)定對(duì)于w.d型的數(shù)據(jù)格式,程序執(zhí)行時(shí)會(huì)自動(dòng)忽略代表總位數(shù)的 w,只保證小數(shù)點(diǎn)后的位數(shù)*/ input x1-x5 name $; file print; put x1-x5 name; cards;111
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(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年度棒球場租賃與賽事宣傳合作合同
- 人力資源公司合作合同
- 食堂承包合同書
- 交通運(yùn)輸行業(yè)智能交通出行服務(wù)平臺(tái)方案
- 服裝廠縫紉機(jī)設(shè)備買賣合同書
- 物流市場分析與規(guī)劃作業(yè)指導(dǎo)書
- 買賣房屋交接合同協(xié)議書
- 人工智能系統(tǒng)開發(fā)與部署作業(yè)指導(dǎo)書
- 帶擔(dān)保的借款合同
- 工業(yè)互聯(lián)網(wǎng)背景下智能倉儲(chǔ)管理解決方案
- 2024年濟(jì)南護(hù)理職業(yè)學(xué)院高職單招職業(yè)技能測驗(yàn)歷年參考題庫(頻考版)含答案解析
- 四川省綿陽市2025屆高三第二次診斷性考試英語試題(含答案無聽力原文及音頻)
- 2025年八省適應(yīng)性 歷史試卷(西北卷)
- 《企業(yè)償債能力存在的問題及優(yōu)化建議:以S地產(chǎn)公司為例》9500字(論文)
- 2025年上半年水利部長江水利委員會(huì)事業(yè)單位招聘68人(湖北武漢)重點(diǎn)基礎(chǔ)提升(共500題)附帶答案詳解
- (2024)云南省公務(wù)員考試《行測》真題及答案解析
- 地方政府專項(xiàng)發(fā)債項(xiàng)目培訓(xùn)課件
- 寧德時(shí)代筆試題庫
- 食品感官評(píng)價(jià)員的選拔與培訓(xùn)
- 五年級(jí)下冊(cè)北京版英語單詞
- 疥瘡護(hù)理查房
評(píng)論
0/150
提交評(píng)論