提取 SAS 中字符串的第二次出现
Extract the second occurrence of the string in SAS
我有以下带有货币和金额字段的文本。
交易货币为美元,金额为200.00 交易货币为欧元,金额为150.00
我需要创建四个变量 tran1
、amt1
、tran2
和 amt2
变量
Tran1 amt1 Tran2 amt2
USD 200 EUR 150
我使用了 substr
和 find
函数,但它只使用了第一次出现。
Tran1=substr(string,find(string,”transacrioncurrency”)+21,3);
Amt1=substr(string,find(string,”the amount is”)+12,5);
要在第一个位置之后找到第二个位置:
data f;
p="The transaction currency is USD and the amount is 200.00 The transaction currency is EUR and the amount is 150.00";
p1=find(p,"The transaction currency",1);
Tran1=substr(p,p1+28,3);
p2=find(p,"the amount is",1);
Amt1=substr(p,p2+14,6);
p3=find(p,"The transaction currency",p1+1);
Tran2=substr(p,p3+28,3);
p4=find(p,"the amount is",p2+1);
Amt2=substr(p,p4+14,6);
;
run;
结果:
p p1 Tran1 p2 Amt1 p3 Tran2 p4 Amt2
The transacti.. 1 USD 37 200.00 58 EUR 94 150.00
如果您的文本在外部文件中,您可以使用 input
语句的替代方法:
data example;
input @'is ' tran1 . @;
input @'is ' amt1 :8. @;
input @'is ' tran2 . @;
input @'is ' amt2 8.;
datalines;
The transaction currency is USD and the amount is 200.00 The transaction currency is EUR and the amount is 150.00
;
run;
我有以下带有货币和金额字段的文本。
交易货币为美元,金额为200.00 交易货币为欧元,金额为150.00
我需要创建四个变量 tran1
、amt1
、tran2
和 amt2
变量
Tran1 amt1 Tran2 amt2
USD 200 EUR 150
我使用了 substr
和 find
函数,但它只使用了第一次出现。
Tran1=substr(string,find(string,”transacrioncurrency”)+21,3);
Amt1=substr(string,find(string,”the amount is”)+12,5);
要在第一个位置之后找到第二个位置:
data f;
p="The transaction currency is USD and the amount is 200.00 The transaction currency is EUR and the amount is 150.00";
p1=find(p,"The transaction currency",1);
Tran1=substr(p,p1+28,3);
p2=find(p,"the amount is",1);
Amt1=substr(p,p2+14,6);
p3=find(p,"The transaction currency",p1+1);
Tran2=substr(p,p3+28,3);
p4=find(p,"the amount is",p2+1);
Amt2=substr(p,p4+14,6);
;
run;
结果:
p p1 Tran1 p2 Amt1 p3 Tran2 p4 Amt2
The transacti.. 1 USD 37 200.00 58 EUR 94 150.00
如果您的文本在外部文件中,您可以使用 input
语句的替代方法:
data example;
input @'is ' tran1 . @;
input @'is ' amt1 :8. @;
input @'is ' tran2 . @;
input @'is ' amt2 8.;
datalines;
The transaction currency is USD and the amount is 200.00 The transaction currency is EUR and the amount is 150.00
;
run;