2017-06-16 267 views
-1

我試圖在SAS中執行以下操作。將一個字符串拆分爲多個行(​​SAS)

我有一個這樣的數據集:

| F2         | F3  | PERIOD | 
___________________________________________________________ 
| Text1(1001) Text2(1002) Text3(1003) |  | 10-02-03 | 
| Text4        | 1004 | 10-02-08 | 
| Text5(1005) Text6(1006)    |  | 10-02-12 | 
| Text7        | 1007 | 10-03-01 | 

我想要做的是分裂F2列了,如果有多個值。因此,該數據集是這樣的:

| F2         | F3  | Period | 
___________________________________________________________ 
| Text1        | 1001 | 10-02-03 | 
| Text2        | 1002 | 10-02-03 | 
| Text3        | 1003 | 10-02-03 | 
| Text4        | 1004 | 10-02-08 | 
| Text5        | 1005 | 10-02-12 | 
| Text6        | 1006 | 10-02-12 | 
| Text7        | 1007 | 10-03-01 | 

所以在括號中的數值在F3列結束,時段列保持不變。

我希望有人可以提供幫助。

回答

1

第一個datastep模擬輸入的前兩行,第二個執行你所需要的內容。

乾杯,弗朗西斯

data in; 
length f2 $64 
     f3 $4 
     period $8; 
f2="Text1(1001) Text2(1002) Text3(1003)"; 
period="10-02-03"; 
output; 
f2="Text4"; 
f3="1004"; 
period="10-02-08"; 
output; 
run; 

data out; 
length outF2 $16; 
set in; 

noOfBlanks = countc(strip(f2),' '); 

if noOfBlanks then do i=1 to noOfBlanks+1; 
    outF2=scan(f2,i," "); 
    f3 = prxchange("s/.*\(|\)//",-1,outF2); 
    outF2 = prxchange("s/\(.*\)//",-1,outF2); 
    output; 
end; 
else do; 
    outF2=f2; 
    output; 
end; 

drop f2 noOfBlanks i; 
rename outF2 = f2; 

run; 
+0

你是一個傳奇。非常感謝! – crellee