最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會(huì)員登陸 & 注冊(cè)

SAS Training Vol5. 新生代農(nóng)名工必知細(xì)節(jié)

2021-08-16 22:59 作者:陸震同學(xué)  | 我要投稿


人在家中坐,認(rèn)證官方來(lái)。都收收傲氣,官方已經(jīng)給 title 了??

今天講講 SAS 對(duì)字符串的操作,主要是兩類 function:handle blanks 和 concatenate character strings。

第一類

處理字符串中的空白,主要有這些 function:TRIM,TRIMN,STRIP,LEFT,COMPRESS,COMPBL。

通過(guò)例子一一來(lái)看:

trim vs. trimn

data sample;
? ?input string $char14.;
? ?datalines;
Mary Smith ? ? ? /* contains trailing blanks */
? ?John Brown ? /* contains leading blanks ?*/
?Alice Park ? ? /* contains leading and trailing blanks */
Tom Wang ? ? ? ?/* contains leading, trailing and multiple blanks in between */
? ? ? ? ? ? ? ? /* contains a blank string */
? ?;
run;
data sample;
? ?set sample;
? ?original= "*" || string ? ? ? ?|| "*";
? ?trim= ? ? "*" || trim(string) ?|| "*";
? ?trimn= ? ?"*" || trimn(string) || "*";
run;
proc print data=sample(drop= string) noobs;
? ?title2 "Output of TRIM and TRIMN";
run;

我們用星號(hào)來(lái)標(biāo)注字符串位置,之所以這么做,是想提醒大家,當(dāng)你設(shè)定 character variable length 時(shí),若字符串本身達(dá)不到這個(gè)長(zhǎng)度,剩下的位置會(huì)以空格補(bǔ)齊,注意這一點(diǎn)很重要,有時(shí)候能在 debug 的時(shí)候幫助你快速找到問(wèn)題所在。我們看到,都是 remove trailing blanks,二者只有在處理第四行空字符串時(shí)有所差異,trim 對(duì)空字符串返回一個(gè)空格,trimn 作長(zhǎng)度為 0 的字符串返回。

strip

data sample;
? ?set sample;
? ?strip= ? ? ?"*" || strip(string) ? ? ? || "*";
? ?trim_left= ?"*" || trim(left(string)) ?|| "*";
? ?trimn_left= "*" || trimn(left(string)) || "*";
run;
proc print data= sample noobs;
? ?title2 "Output of STRIP, TRIM(LEFT) and TRIMN(LEFT)";
? ?var original strip trim_left trimn_left;
run;

這里要提的是,strip 同時(shí) remove leading and trailing blanks,left 左對(duì)齊并 remove leading blanks,在這一點(diǎn)上,strip 等價(jià)于 trim(left),只不過(guò),strip 要更為高效。

compress vs. compbl

先來(lái)看 compress:

data zipcode;
? ?input zipcode $14.;
? ?zipcode1= compress(zipcode); ? ? ? ? ?/* to remove blanks */
? ?zipcode2= compress(zipcode,' ()?'); ? /* to remove blanks, () and ? */
? ?zipcode3= compress(zipcode,'- ()?'); ?/* to remove dash, blanks, () and ? */
? ?datalines;
22168- 12 34
22168- (1234?)
;
run;
proc print data= zipcode noobs;
? ?title2 "Listing of Zipcodes";
run;

compress 可以刪除指定字符,如空格、破折號(hào)、括號(hào)等,默認(rèn)刪除空格。但是要注意:一旦我們指定 compress 的第二個(gè)參數(shù),它就不再默認(rèn)刪除空格,要想同時(shí)刪除空格,必須要顯式地指定參數(shù)含有空格。

data sample;
? ?set sample;
? ?compress= "*" || compress(string) || "*";
? ?compbl= ? "*" || compbl(string) ? || "*";
run;
proc print data=sample noobs;
? ?title2 "Output of COMPRESS and COMPBL";
? ?var original compress compbl;
run;

compbl 與它類似,唯一不同的是,compbl 無(wú)論是處理多個(gè)空格還是單個(gè)空格,總要留下一個(gè)空格不處理。

第二類

處理完空格后,我們想自如地 concatenate 這些字符串,怎么辦?借助 cat,catt,cats,catx。

當(dāng)然,最簡(jiǎn)單的還是 concatenation operator ||,上面例子中,我們已經(jīng)見識(shí)過(guò)它的級(jí)聯(lián)作用了,它不對(duì)前后字符串作任何 leading、trailing blanks 的處理。當(dāng)小規(guī)模的級(jí)聯(lián)時(shí),它還算方便,太多字符串復(fù)雜連接時(shí),它還是 tedious。

cat,catt,cats,catx

data sample;
? ?set sample;
? ?length cat catt cats $16 catx $20;
? ?text='Hello';
? ?cat= ?cat('*',string,'*'); ? /* (= ||) */
? ?catt= catt('*',string,'*'); ?/* (= TRIM || or TRIMN ||) */
? ?cats= cats('*',string,'*'); ?/* (= STRIP ||)) */
? ?catx= catx('!',text,string); /* (= STRIP || separator) */
run;
proc print data=sample noobs; var cat catt cats catx;
? ?title2 "Output of Concatenation Functions";
run;

cat 其實(shí)相當(dāng)于 ||,不對(duì)前后字符串作任何 leading、trailing blanks 的處理;catt 你當(dāng)它是 trimn + ||;cats 你當(dāng)作是 strip + ||;catx 就有意思了,不僅有 cats 的全部功能,還能夠在相連的字符串之間插入分隔符。

注意:這四個(gè)函數(shù)輸出字符串的默認(rèn)長(zhǎng)度是 200,如果想控制輸出字符串的實(shí)際長(zhǎng)度,最好提前 length 規(guī)定字符串的長(zhǎng)度屬性。

最后,來(lái)一個(gè)便捷操作:對(duì) variable list 使用 of。

data mailing;
? ?length mail_1-mail_3 $35;
? ?address1= '123 Main St.';
? ?address2= 'Eden';
? ?address3= 'VT';
? ?address4= '05060';
? ?mail_1= catx(' ', of address1-address4); ? /* Insert space as separator */
? ?mail_2= catx(',', of address1-address4); ? /* Insert comma as separator */
? ?mail_3= catx(', ', of address1-address4); ?/* Insert comma and space as separator */
run;
proc print data=mailing noobs;
? ?var mail_1 mail_2 mail_3;
? ?title2 "Listing of Data Set: Mailing";
run;


SAS Training Vol5. 新生代農(nóng)名工必知細(xì)節(jié)的評(píng)論 (共 條)

分享到微博請(qǐng)遵守國(guó)家法律
宣武区| 松潘县| 濉溪县| 元江| 黎城县| 舞阳县| 师宗县| 拜城县| 宣城市| 兴文县| 澎湖县| 中宁县| 正宁县| 梁山县| 瓮安县| 怀集县| 资兴市| 庐江县| 桂阳县| 仁布县| 沈阳市| 云浮市| 五大连池市| 桂林市| 饶河县| 河北省| 黄山市| 津市市| 威宁| 石河子市| 常熟市| 镇宁| 运城市| 鄯善县| 布拖县| 寿阳县| 新郑市| 开远市| 淮滨县| 松溪县| 旅游|