SAS中文论坛

标题: SAS advance exam question 4: Macro Variable [打印本页]

作者: shiyiming    时间: 2010-6-9 05:05
标题: SAS advance exam question 4: Macro Variable
The following SAS program is submitted:
%let Value=9;
%let Add=5;
%let Newval=%eval(&Value/&Add);
%put &Newval;

What is the value of the macro variable Newval when the %PUT statement executes?
A. 0.555
B. 2
C. 1.8
D. 1

Why the correct answer is 1? What is the function of %eval ?

What if the following SAS codes is submitted?
%let Value=9;
%let Add=5;
%let Newval=&Value/&Add;
%put &Newval;

Is the value of the macro variable Newval  still 1 when the %PUT statement executes? Why?

Thanks in advance.
作者: shiyiming    时间: 2010-6-9 13:27
标题: Re: SAS advance exam question 4: Macro Variable
还容易考完了,再看这些题都要吐了,简单点说,就别考虑是否严谨了
这题的知识点是maroc的%eval()函数的用法
1 只能对整数执行计算,%eval(5+5.5)会报错
2 返回值也是个整数,计算出的小数部分自动就被截掉了,1.8就只省1了. 如果需要计算浮点数可以用%sysevalf()函数. %sysevalf(1/3.3)是OK的.
作者: shiyiming    时间: 2010-6-10 21:22
标题: Re: SAS advance exam question 4: Macro Variable
多谢
作者: shiyiming    时间: 2010-6-10 22:07
标题: Re: SAS advance exam question 4: Macro Variable
21   %let Value=9;
22   %let Add=5;
23   %let Newval_1=%eval(&Value/&Add);
24   %put Newval_1 = &Newval_1;
Newval_1 = 1

25
26   %let Newval_2=%sysevalf(&Value/&Add);
27   %put Newval_2 = &Newval_2;
Newval_2 = 1.8

28
29   %let Newval_3=&Value/&Add;
30   %put Newval_3 = &Newval_3;
Newval_3 = 9/5

31   %let Value=9.1;
32   %let Add=5.1;
33   %let Newval_4=%eval(&Value/&Add);
ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand
       is required. The condition was: 9.1/5.1
34   %put Newval_4 = &Newval_1;
Newval_4 = 1

35   %let Value=9.1;
36   %let Add=5.1;
37   %let Newval_4=%sysevalf(&Value/&Add);
38   %put Newval_4 = &Newval_1;
Newval_4 = 1

39   %let Newval_4=%eval(&Value+&Add);
ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand
       is required. The condition was: 9.1+5.1
40   %put Newval_4 = &Newval_1;
Newval_4 = 1

41   %let Newval_4=%sysevalf(&Value+&Add);
42   %put Newval_4 = &Newval_1;
Newval_4 = 1




欢迎光临 SAS中文论坛 (https://mysas.net/forum/) Powered by Discuz! X3.2