SAS讀取數(shù)據(jù)的規(guī)則是遇到空格時默認(rèn)該數(shù)據(jù)讀取完畢祈餐,除非是指定了數(shù)據(jù)寬度時,則以寬度值來確定數(shù)據(jù);冒號的作用是告訴SAS,如果要讀取下一個變量,需要滿足下面任一條件:要么遇到空格橘忱,要么變量的寬度讀完了。
data fh;
input city $18. zone $;
cards;
山東省蓬萊市 0536
山東省威海市乳山市 0532
;
proc print;
run;
data fh;
input city :$18. zone $;
cards;
山東省蓬萊市 0536
山東省威海市乳山市 0532
;
proc print;
run;
使用if_then語句創(chuàng)建新變量
data lx;
input id lx$;
lx1 = lx in ("有效","顯效","痊愈");
if lx in ("有效","顯效","痊愈") then lx2 = "有效";else lx2 = "無效";
datalines;
1 顯效
2 有效
3 無效
4 痊愈
;
proc print;
run;
retain 語句
data a1;
retain a 0; /告訴SAS a 的初始值谚咬,以便a = a+1表達(dá)式使用/
a = a+1;
input wt ht;
datalines;
60 170
55 166
73 162
;
proc print;
run;
do循環(huán)語句
do 變量=初始值 to 最終值 <by 增加量>
SAS語句
end
- do i = 1 to 10;
- do i = 1 to 10 by 2;
data fh;
do count = 1 to 5;
input time;
output;
end;
datalines;
23
29
49
64
87
;
proc print;
run;
SAS中有一個規(guī)則:字符串變量的長度是由第一個遇到的值的長度決定的鹦付,而且字符變量一旦產(chǎn)生,他的長度就無法改變择卦。
data age;
do id = 1 to 4;
input birth:yymmdd10. death:yymmdd10.;
age = (death - birth)/365;
if age < 60 then age1 = 50;
else if age < 70 then age1 = 60;
else age1 = 70;
format birth death yymmdd10.;
output;
end;
datalines;
1954-12-06 2014-02-03
1938-02-18 2014-01-17
1947-07-10 2014-01-11
1943-08-21 2014-03-03
;
run;
SAS 函數(shù)應(yīng)用技巧
與字符有關(guān)的函數(shù)
例子:身份證倒數(shù)第二位表示性別,奇數(shù)為男郎嫁,偶數(shù)為女秉继。
data iden;
input iden:$18.;
if length(iden) = 18 then gen = substrn(iden,17,1);
else gen = substrn(iden,15,1);
if mod(gen,2) = 1 then gender = "男";
else gender = "女";
datalines;
34082319920902371X
360533801215792
360533198208254533
360533851009226
;
proc print;
run;
新版身份證號為18位,老版的則為16位泽铛。
這兩個函數(shù)在查找多個字符的時候有很大差異:對于多個字符的查找尚辑,find必須是所有字符都完全匹配才算找到,而findc只要找到字符中的任意一個就算找到盔腔。
data book;
input book: & $100.;
sas = find(book,'sas','i');
if sas >0 then class = 'SAS書';
else class = '其他書';
datalines;
Survival Analysis Using SAS
MATLAB 程序設(shè)計
SPSS數(shù)據(jù)分析
SAS應(yīng)用分析
The Little SAS book
;
proc print;
run;
data computer;
input type$@@;
alpha = anyalpha(type);
digit = anydigit(type);
xh = substrn(type,alpha,digit-alpha);
bh = substrn(type,digit,length(type)-digit+1);
datalines;
TP340 KB320 B3519 C560 H430 LLL
;
proc print;
run;
data lx;
input id lx$;
lx1 = tranwrd(lx,"顯效","有效");
lx1 = tranwrd(lx1,"痊愈","有效");
datalines;
1 顯效
2 有效
3 無效
4 痊愈
;
proc print;
run;
data computer;
input type$@@;
xh = compress(type,,'d');
bh = compress(type,,'a');
cards;
TP320 KS230 B3214 HJ234 H324
;
proc print;
run;
data code;
input prov$ city$ country$;
code1 = cats(prov,city,country);
code2 = catx("-",prov,city,country);
code3 = prov||city||country; /*常規(guī)的連接符*/
cards;
37 05 02
37 02 21
37 06 85
;
proc print;
run;
data cloth;
input pj&:$1000.;
beauty = count(pj,"漂亮");
datalines;
裙子很漂亮杠茬,穿起來有仙女的感覺
裙子很喜歡,很漂亮弛随,不知道面料牢固不牢固
裙子很漂亮
裙子很飄逸
面料柔軟舒適瓢喉,很飄逸
很漂亮,超喜歡這顏色
質(zhì)量一般舀透,沒想象中的好
很大方栓票,不足之處是,透氣性不是很好
;
proc print;
run;
與時間和日期有關(guān)的函數(shù)
data date;
input year1$ month1$ day1$ year2$ month2$ day2$;
date1 = mdy(month1,day1,year1);
date2 = mdy(month2,day2,year2);
format date1 date2 yymmdd10.;
ydif = yrdif(date1,date2,"actual");
ddif = datdif(date1,date2,"actual");
datalines;
2013 05 21 2014 03 11
2013 03 10 2014 01 22
2013 06 05 2014 05 06
2013 07 07 2014 04 13
;
proc print;
run;
SAS中有兩個函數(shù)式專門用于變量類型之間轉(zhuǎn)換的愕够,input函數(shù)主要用于把字符型轉(zhuǎn)為數(shù)值型走贪,put函數(shù)主要用于把數(shù)值型轉(zhuǎn)為字符型。
data date;
input year1$ month1$ day1$ year2$ month2$ day2$;
date1 = catx("/",year1,month1,day1);
date2 = catx("/",year2,month2,day2);
d1 = input(date1,yymmdd10.);
d2 = input(date2,yymmdd10.);
format d1 d2 yymmdd8.;
datalines;
2013 05 21 2014 03 11
2013 03 10 2014 01 22
2013 06 05 2014 05 06
2013 07 07 2014 04 13
;
proc print;
run;