Sas應用之宏變量%eval
定義如下:The %EVAL function evaluates integer arithmetic or logical expressions. %EVAL operates by converting its argument from a character value to a numeric or logical expression. Then, it performs the evaluation. Finally, %EVAL converts the result back to a character value and returns that value.。
一盖呼、程序思路 做一個簡單的例子抄瓦,實現(xiàn)1到100相加的結果,通過添加和去掉%eval掌握%eval的用法造锅。
二、程序如下:
%macro a;
%let sum=0;
%do i=1 %to 100 %by 1;
%let sum=&sum+&i;
%put &sum.;
%end;
%mend;
%a;
在該宏中钩骇,實現(xiàn)從1到100的相加泛啸,每次循環(huán)都put出sum的結果;本次結果如下:
沒有實現(xiàn)數(shù)值計算属百,而是以字符的形式做了輸出记劝;為什么會是這樣的呢?原因在于sum=&sum+&i這里沒有使用%eval宏函數(shù)進行計算族扰。添加后程序為:
%macro a;
%let sum=0;
%do i=1 %to 100 %by 1;
%let sum=%eval(&sum+&i);
%put &sum.;
%end;
%mend;
%a;
結果如下:
1
3
6
10
15
21
28
36
45
55
66
78
91
105
120
136
153
171
190
210
231
253
276
300
325
351
378
406
435
465
496
528
561
595
630
666
703
741
780
820
861
903
946
990
1035
1081
1128
1176
1225
1275
1326
1378
1431
1485
1540
1596
1653
1711
1770
1830
1891
1953
2016
2080
2145
2211
2278
2346
2415
2485
2556
2628
2701
2775
2850
2926
3003
3081
3160
3240
3321
3403
3486
3570
3655
3741
3828
3916
4005
4095
4186
4278
4371
4465
4560
4656
4753
4851
4950
5050
可關注微信號:SAS應用分析