擴大一點上Itzy的答案,這裏是另一種可能解決方案:
data want;
/* Define variables */
attrib item length=$10 label='Item';
attrib class length=$10 label='Family';
attrib desc length=$80 label='Item Description';
attrib count length=8 label='Some number';
attrib year length=$4 label='Year';
attrib somevar length=$10 label='Some variable';
length countc $8; /* A temp variable */
infile 'c:\temp\delimited_temp.txt' lrecl=1000 truncover;
input;
item = scan(_infile_,1,'|','mo');
class = scan(_infile_,2,'|','mo');
countc = scan(_infile_,-3,'|','mo'); /* Temp var for numeric field */
count = inputn(countc,'8.'); /* Re-read the numeric field */
year = scan(_infile_,-2,'|','mo');
somevar = scan(_infile_,-1,'|','mo');
desc = tranwrd(
substr(_infile_
,length(item)+length(class)+3
,length(_infile_)
- (length(item)+length(class)+length(countc)
+length(year)+length(somevar)+5))
,'|',' ');
drop countc;
run;
在這種情況下它的關鍵是它直接讀取您的文件並處理分隔符你自己。這可能會很棘手,並且需要您的數據文件完全按照描述。一個更好的解決方案是回到誰提供這些數據,並要求他們以更合適的形式交付給你。祝你好運!
你怎麼知道水果和健康之間的分隔符是正確的,但健康和選擇之間的分隔符不是正確的?客觀地,你怎麼知道? –
我認爲OP意味着第2個和最後3個變量不能包含額外的分隔符。我的解決方案假定。 – itzy
這就是itzy – user1825366