sas - Extract the second occurrence of the string in SAS
问题描述
I have the following text with the currency and amount fields.
The transaction currency is USD and the amount is 200.00 The transaction currency is EUR and the amount is 150.00
I need to create four variables tran1
, amt1
, tran2
and amt2
variables
Tran1 amt1 Tran2 amt2
USD 200 EUR 150
I used substr
and find
functions but it is taking the first occurrence only.
Tran1=substr(string,find(string,”transacrioncurrency”)+21,3);
Amt1=substr(string,find(string,”the amount is”)+12,5);
解决方案
To find the second occurrence start after the position of the fisrt one :
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;
result :
p p1 Tran1 p2 Amt1 p3 Tran2 p4 Amt2
The transacti.. 1 USD 37 200.00 58 EUR 94 150.00
推荐阅读
- java - 如果 Java 中的语句不能正常工作
- python - 使用 IE11 Python Selenium 处理帧
- contiki - udp-client 可以在 contiki-ng 中加入特定的 RPL 实例 ID 吗?
- python-3.x - 搜索字典中是否存在两个值的正确语法是什么?
- python - 我怎样才能摆脱“数组[],dtype ='
我正在编写代码来分析不同产品(StockCode)的销售频率,所以这是获取频率的代码:
stockCode = df['StockCode'].values.tolist() non_repeated_list = [] frequencies = [] list2d= [] for i in stockCode: if i not in non_repea
- javascript - 在javascript中将列表转换为地图
- r - 数据从宽格式到长格式,具有多个不同类型的重复列
- c# - 如何使用 UserManager 按 ID、姓名、电子邮件或其他搜索条件获取用户(不是当前登录的用户)?
- node.js - Mongoose - 替换所有数组元素
- r - 关于变量中的 paste0 包括空格和 ()