SAS從變量中提出數(shù)值

data test;

input id 5. str $60. rank $14. sicence $16.;

cards;

1001 語文成績:119,數(shù)學(xué)成績:125,英語成績:136,總分:380 在1班排名:1? 語文_數(shù)學(xué)_英語

1002 語文成績:102,數(shù)學(xué)成績:125,英語成績:126,總分:353 在1班排名:3? 語文_數(shù)學(xué)_英語

1003 語文成績:129,數(shù)學(xué)成績:115,英語成績:106,總分:350 在1班排名:4? 語文_數(shù)學(xué)_英語

1004 語文成績:93,數(shù)學(xué)成績:145,英語成績:131,總分:369 在1班排名:2? 語文_數(shù)學(xué)_英語

;

run;

test數(shù)據(jù)集

*利用compress取數(shù)值;

data x1;

set test(keep=id rank);

var1=compress(rank,"1234567890.-","k");/*取rank字段中的數(shù)字*/

var2=index(rank,":"); /*定位":"的位置*/

var3=substr(rank,index(rank,":"),5);

var4=input(compress(substr(rank,index(rank,":"),5),"1234567890.-","k"),best.);/*input將文本格式轉(zhuǎn)化為數(shù)值型*/

run;


*分列(按照固定標(biāo)點符號分裂);

*注:scan按中文中文符號分列有時會出錯;

data x2;

set test;

a=scan(sicence,1,"_");?

b=scan(sicence,2,"_");

c=scan(sicence,3,"_");

d=count(str,',');/*統(tǒng)計str字段中逗號的數(shù)量*/

put a b c d;

run;

/***利用數(shù)組分列后取值***/

*將中文逗號轉(zhuǎn)為英文逗號;

data x3_1;

set test;

str=tranwrd(str,',',',');

run;

*method1;

data x3;

length group1-group4 $16.;

array groups[4] group1-group4; /*比逗號數(shù)據(jù)多1個*/

set x3_1;

rest=str;

do i=1 to 4;

loc=index(rest,",");

if loc>0 then do;

groups[i]=input(compress(substr(rest,1,loc-1),"1234567890.-","k"),best.);

rest=substr(rest,loc+1,length(rest));

end;

else do;

groups[i]=input(compress(rest,"1234567890.-","k"),best.);

i=5;

end;

end;

drop rest i loc;

run;

*method2;

data x4;

length group1-group4 $16.;

array groups[4] group1-group4; /*比逗號數(shù)據(jù)多1個*/

set x3_1;

do i=1 to 4;

groups[i]=input(compress(scan(str,i,','),"1234567890.-","k"),best.);

end;

run;

*just for fun;

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容