楼主: Tigflanker
6446 11

[问答] 求助字符串匹配查找 [推广有奖]

  • 8关注
  • 18粉丝

副教授

49%

还不是VIP/贵宾

-

威望
0
论坛币
2321 个
通用积分
9.9128
学术水平
179 点
热心指数
194 点
信用等级
167 点
经验
27443 点
帖子
622
精华
0
在线时间
851 小时
注册时间
2011-3-27
最后登录
2023-5-14

楼主
Tigflanker 发表于 2013-9-21 21:05:26 |AI写论文
50论坛币
求助各位老师一个问题:

  1. %macro a;
  2. %let a=  tt cc fdx d x e r t s d   ;

  3. %let c=%sysfunc(tranwrd(&a,t, ));
  4. %put &c;

  5. %mend;%a;
复制代码


代码中,例如我想查找并替换 &a 中 ‘t’ 的值,但是无论用任何办法,SAS只能返回第一个tt的位置,请问有什么办法能全字匹配查找么?谢谢

更直接点说,我想把:

  1. %let a=  tt cc fdx d x t c f    ;
  2. %let b=t c f;
复制代码
在&a中出现的&b挖掉:

  1. %let c=%???????(&a,&b);
  2. %put >>&c<<;
  3. >>tt cc fdx d x<<
复制代码


关键词:字符串匹配 字符串 sysfunc Macro let 字符串
Bye SAS.
若有缘,能重聚。

沙发
yongyitian 发表于 2013-9-21 21:05:27
复制代码
  1. /* Please note that the leading and trailing
  2. spaces in the macro variable defined by %let
  3. statement will be removed; */

  4. %macro aa;
  5. %let a = tt cc fdx d x e r t s d ;

  6. %let c=%sysfunc(indexw(&a,t));
  7. %put &a;
  8. %put &c;

  9. %mend;%aa;
  10. %aa;

  11. %macro a;
  12. %let a= tt cc fdx d x e r t s d ;
  13. %let b = t s d;
  14. %let c=%sysfunc(tranwrd(&a,&b, %str() ));
  15. %put &c;

  16. %mend;
  17. %a;
复制代码

藤椅
Tigflanker 发表于 2013-9-21 21:08:44
目前我用的是:%if "%upcase(%scan(&a,&i,%str( )))" eq "T"

请问有更直接的办法么?
Bye SAS.
若有缘,能重聚。

板凳
妖帝东皇 发表于 2013-9-21 21:42:11
yongyitian 发表于 2013-9-21 21:20
从15行开始才是正点!
┏━━━━━━━━━━━━━━┓
   ☞❤学而无友必然孤陋寡闻!❤
┗━━━━━━━━━━━━━━┛

报纸
Tigflanker 发表于 2013-9-21 22:08:31
yongyitian 发表于 2013-9-21 21:20
indexw function is the one I'm looking for, thank you.

By the way, do you know how can identify sevel cases like :

%let a= tt cc fdx d x e r t s d ;
%let b = t d s;

I'm sorry, I omit the same order with &b and &c.
Bye SAS.
若有缘,能重聚。

地板
yongyitian 发表于 2013-9-21 22:20:53
Tigflanker 发表于 2013-9-21 22:08
indexw function is the one I'm looking for, thank you.

By the way, do you know how can identify ...
If there is more than one case, you can try %do loop
or %if .. %then
      %do;
        ...;
      %end;
    %else %then %do; ... %end;

7
Tigflanker 发表于 2013-9-21 22:22:17
yongyitian 发表于 2013-9-21 21:05
For example :
  1. %macro a;
  2. %let a= ttt cc fdx d x e r t s d ;
  3. %let c=%sysfunc(tranwrd(&a,t,ABC));
  4. %put &c;
  5. %mend;%a;
复制代码
The result is : ABCABCABC cc fdx d x e r ABC s d
But not : ttt cc fdx d x e r ABC s d

So, If you have any idear to avoid this problem.

Thank you !!
Bye SAS.
若有缘,能重聚。

8
yongyitian 发表于 2013-9-21 23:38:55
Tigflanker 发表于 2013-9-21 22:22
For example :The result is : ABCABCABC cc fdx d x e r ABC s d
But not : ttt cc fdx d x e r ABC s  ...
The idea is to add a space before and after t,
               and a space before and after ABC,
whitch means to use ' ABC ' to replace ' t '.
已有 1 人评分学术水平 热心指数 信用等级 收起 理由
Tigflanker + 1 + 1 + 1 Thx, It works. %str( t )

总评分: 学术水平 + 1  热心指数 + 1  信用等级 + 1   查看全部评分

9
gaotao0727 发表于 2013-9-22 15:24:46
学习学习~~
衣带渐宽终不悔,为伊消得人憔悴~~

10
双修阁主の 发表于 2013-9-23 10:57:40
不错,学习了

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-30 10:04