楼主: Tigflanker
1725 6

[问答] 请bobguy老师进下,谢谢。 [推广有奖]

  • 8关注
  • 18粉丝

副教授

49%

还不是VIP/贵宾

-

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

楼主
Tigflanker 发表于 2012-10-28 13:32:13 |AI写论文
500论坛币
bobguy老师你好,我发过几次求助帖,你给出的解答都很厉害,但是我都权衡选择了别人的答案,望你别介意。

我想再问你一个问题,我近来常常会遇到:

  1. %macro check(vt);

  2. %do i=1 %to 4;
  3. %let ty=%sysfunc(rank(%substr(&vt,&i,1)));
  4. %if (&ty=82 or &ty=114) %then %let r=1;
  5. %if (&ty=74 or &ty=106) %then %let j=1;
  6. %if (&ty=89 or &ty=121) %then %let y=1;
  7. %if (&ty=77 or &ty=109) %then %let m=1;
  8. %end;

  9. %mend;

  10. %check(Rjm);
复制代码
如代码中,我想在某个宏参数中加入识别菜单的形式,例如我的宏中提供四种功能:rjym。我想用第一个参数无论顺序和大小写,指明这个宏下面要包括的内容。

代码中,字符串Rjm,我实际想直接表达的意思如下:

%if (%substr(&vt,3,1)^=m) %then 如何如何。。。

不过报错,①请问%if内可以比较字符么?谢谢。

另外:②上面引用代码会报错,内容是:宏引用没有提及‘y’,所以出现rank(.)的情况,请问如果一定这样写的话,有办法优化么。谢谢。

最佳答案

davil2000 查看完整内容

1. %if 语句中可以使用比较运算符; 2. 嵌套多了会降低编译效率,也不易发现错误。 %macro check(vt); %do i=1 %to 3; data _null_; x= substr("&vt", &i,1); y= rank(x); call symput('tx',x); call symput('ty',y); run; %if (&ty=82 or &ty=114) %then %let r=1; %if (&ty=74 or &ty=106) %then %let j=1; %if (&ty=89 or &ty=121) %then %let y=1; %if (&ty=77 or &ty=109) %then %let m=1 ...
关键词:Bob Guy sysfunc SUBSTR check 老师
Bye SAS.
若有缘,能重聚。

沙发
davil2000 发表于 2012-10-28 13:32:14
1. %if 语句中可以使用比较运算符;
2. 嵌套多了会降低编译效率,也不易发现错误。

%macro check(vt);
%do i=1 %to 3;

  data _null_;
  x= substr("&vt", &i,1);
  y= rank(x);
  call symput('tx',x);
  call symput('ty',y);
  run;

  %if (&ty=82 or &ty=114) %then %let r=1;
  %if (&ty=74 or &ty=106) %then %let j=1;
  %if (&ty=89 or &ty=121) %then %let y=1;
  %if (&ty=77 or &ty=109) %then %let m=1;
  %put *--- The ASCII position of "%trim(&tx)" is %left(&ty) %str(---*);

%end;
%mend;

%check(Rjm);


已有 3 人评分经验 论坛币 学术水平 热心指数 信用等级 收起 理由
admin_kefu + 100 热心帮助其他会员
数据分析师3K + 80 + 60 + 5 + 5 + 5 热心帮助其他会员
Tigflanker + 1 + 1 + 1 热心帮助其他会员

总评分: 经验 + 80  论坛币 + 160  学术水平 + 6  热心指数 + 6  信用等级 + 6   查看全部评分

R是万能的,SAS是不可战胜的!

藤椅
davil2000 发表于 2012-11-1 16:42:34
%macro check(vt);
%do i=1 %to 3;
%let tx = %substr(&vt,&i,1);
%let ty= %sysfunc(rank(%substr(&vt, &i,1)));
/*  %if (&ty=82 or &ty=114) %then %let r=1;*/
/*  %if (&ty=74 or &ty=106) %then %let j=1;*/
/*  %if (&ty=89 or &ty=121) %then %let y=1;*/
/*  %if (&ty=77 or &ty=109) %then %let m=1;*/
  %put *--- The ASCII position of "%trim(&tx)" is %left(&ty) %str(---*);
%end;
%mend;

%check(Rjm);
已有 2 人评分经验 学术水平 热心指数 信用等级 收起 理由
大数据之魂 + 100 + 5 + 5 + 5 根据规定进行奖励
数据分析师3K + 5 精彩帖子

总评分: 经验 + 100  学术水平 + 10  热心指数 + 5  信用等级 + 5   查看全部评分

R是万能的,SAS是不可战胜的!

板凳
Tigflanker 发表于 2012-11-1 17:50:55
davil2000 发表于 2012-11-1 14:37
1. %if 语句中可以使用比较运算符;
2. 嵌套多了会降低编译效率,也不易发现错误。
感谢davil老师指点,不过您用的也是rank方法;我知道在data步中可以用字符之间的比较,不过宏里面似乎不可以。呵呵,只好妥协了。
Bye SAS.
若有缘,能重聚。

报纸
davil2000 发表于 2012-11-1 18:53:43
Tigflanker 发表于 2012-11-1 17:50
感谢davil老师指点,不过您用的也是rank方法;我知道在data步中可以用字符之间的比较,不过宏里面似乎不可 ...
%IF expression %THEN action;  
<%ELSEaction;>  

Here,expression is any macro expression that resolves to an integer.  One can use arithmetic and logical expressions in specific macro functions and statements.Of course, he can also do some comparison in these expressions.

Expressions in which comparison operators surround a macro expression, as in 10<&X<20, may or may not be the equivalent of a DATA step compound expression (depending on what the expression resolves into). To be safe, write the connecting operator explicitly, as in the expression 10<&X AND &X<20.
已有 2 人评分经验 学术水平 热心指数 信用等级 收起 理由
南海游客 + 5 + 5 + 5 热心帮助其他会员
数据分析师3K + 60 + 5 + 1 分析的有道理

总评分: 经验 + 60  学术水平 + 5  热心指数 + 10  信用等级 + 6   查看全部评分

R是万能的,SAS是不可战胜的!

地板
南海游客 发表于 2012-11-1 19:49:42
davil2000 发表于 2012-11-1 18:53
%IF expression %THEN action;
学习了 感谢分享

7
bobguy 发表于 2012-11-3 10:24:04
Your problem can be simply coded as below.

Here are some tips.
1) use macro functions as much as possible.
2) yes. a macro error is difficulty to debug. But adding %put statement is a big plus.
3) In this case using a data step + macro is a clumsy way.

SAS macro is not easy to learn. A SAS macro is usually to generate SAS programs. If one's SAS skill is not quite good, it is hard to believe that generated SAS programs will be any better.  A good macro involves lot of thinking before hand. These requirements are probably  for SAS developers.

4       %macro check(vt);
95
96       %do i=1 %to %length(&vt);
97       %let ty=%substr(&vt,&i,1);
98       %if "%upcase(&ty)" ne "R" %then %put >>>>&i). ty=&ty here if is executed<<<<;
99       %else %put >>>>&i). ty=&ty here else is executed<<<<;;
100      %end;
101
102      %mend;
103
104      %check(Rjmrab);
>>>>1). ty=R here else is executed<<<<
>>>>2). ty=j here if is executed<<<<
>>>>3). ty=m here if is executed<<<<
>>>>4). ty=r here else is executed<<<<
>>>>5). ty=a here if is executed<<<<
>>>>6). ty=b here if is executed<<<<
已有 1 人评分学术水平 热心指数 信用等级 收起 理由
Tigflanker + 1 + 1 + 1 Nice,I really appreciate your help again

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

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

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2026-2-2 20:41