tranwrd函数用法详解

tranwrd函数: 替换指定字符


data test;
X="ABCDABA";
B=Compress(X,"AB"); /*删除字符A和B*/
Y=tranwrd(X,"AB","K");/*用K代替AB*/
Z=tranwrd(X,"AB","");/*用空格代替AB*/
M=compress(tranwrd(X,"AB",""),"");/*在Z的基础上去掉空格*/
N=compress(Z);/*M等价于N,compress缺省为去掉空格*/
run;
proc print;
run;

SAS中的两个字符串处理函数:tranwrd函数、translate函数 (2014-04-16 17:04:35)转载▼
标签: sas translate tranwrd 分类: SAS笔记
1.tranwrd函数:将字符串中的某些字符替换为其他字符。
data _null_;
a='abcd';
b=tranwrd(a,'bc','xx');
put a= b=;
run;
在log中输出结果为: a=abcd b=axxd

2.translate函数:变换字符串中字符的顺序,或者替换字符。
data  _null_;
old="123456";
new=translate(old,"a","2","b","4","c","6");
put new=;
run;
在log中输出结果为: new=1a3b5c

data _null_;
A='04/20/2017';
B=translate(a,'-','/');
put B=;
run;
在log中输出结果为: B=04-20-2017

3.字符串替换
函数translate会替换某个字符,而Transwrd会替换某个单词。例如
data trans;
input answer : $5.;
answer = translate(answer,'ABCDE','12345');
datalines;
14325
AB123
51492
;
run;

得到结果如下
Answer
ADCBE
ABABC

EAD9B


  • 3
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值