楼主: xiezuosheng
7208 14

[原创博文] 关于宏运行出错,请高手指教 [推广有奖]

11
bobguy 发表于 2010-10-9 11:19:23
xiezuosheng 发表于 2010-10-7 11:36
各位大虾们,下面是我运行朱世武教授书上的程序如下:
%macro create;
data=temp;
set Resdat.&dat end=final;
if year(date)=&year then do;
n+1;
output;
end;
if final then call symput("number",n);
/*创建的宏变量为number,其值为n*/
run;

%mend create;
%macro plot;
proc gplot data=temp;
title2"&pr &year1时序图";
plot &price*date=1;
symbol1 v=star i=join r=1 c=red;
%mend plot;
run;

%macro analyze(getdata,dat,year,pr,price,year1);
%if %upcase(&getdata)=yes %then %create;
footnote "plot of &number of observations";
%plot;
%mend analyze;
run;

%analyze(yes,Idx000001,1999,收盘价,clpr,1999);
%analyze(yes,Idx000002,1999,收盘价,clpr,1999);
%analyze(yes,Idx000001,1999,收盘价,clpr,2000);
%analyze(yes,Idx000002,1999,收盘价,clpr,2000);
run;


在前面编译都非常正常,但
是运行到宏analyse时出错,log上面显示错误如下:

597  %analyze(yes,Idx000001,1999,收盘价,clpr,1999);
WARNING: Apparent symbolic reference NUMBER not resolved.
NOTE: The SAS System stopped processing this step because of errors.
NOTE: PROCEDURE GPLOT used:
      real time           8.80 seconds
      cpu time            0.01 seconds

ERROR:File WORK.TEMP.DATA does not exist.
WARNING: Apparent symbolic reference YEAR1时序图 not resolved.
WARNING: The TITLE statement is ambiguous due to invalid options or unquoted text.
598  %analyze(yes,Idx000002,1999,收盘价,clpr,1999);
WARNING: Apparent symbolic reference NUMBER not resolved.
NOTE: The SAS System stopped processing this step because of errors.
NOTE: PROCEDURE GPLOT used:
      real time           0.01 seconds
      cpu time            0.01 seconds

ERROR: File WORK.TEMP.DATA does not exist.
WARNING: Apparent symbolic reference YEAR1时序图 not resolved.
WARNING: The TITLE statement is ambiguous due to invalid options or unquoted text.
599  %analyze(yes,Idx000001,1999,收盘价,clpr,2000);
WARNING: Apparent symbolic reference NUMBER not resolved.
NOTE: The SAS System stopped processing this step because of errors.
NOTE: PROCEDURE GPLOT used:
      real time           0.01 seconds
      cpu time            0.00 seconds

ERROR: File WORK.TEMP.DATA does not exist.
WARNING: Apparent symbolic reference YEAR1时序图 not resolved.
WARNING: The TITLE statement is ambiguous due to invalid options or unquoted text.
600  %analyze(yes,Idx000002,1999,收盘价,clpr,2000);
WARNING: Apparent symbolic reference NUMBER not resolved.
NOTE: The SAS System stopped processing this step because of errors.
NOTE: PROCEDURE GPLOT used:
      real time           0.01 seconds
      cpu time            0.00 seconds

ERROR: File WORK.TEMP.DATA does not exist.
WARNING: Apparent symbolic reference YEAR1时序图 not resolved.
WARNING: The TITLE statement is ambiguous due to invalid options or unquoted text.
601  run;
WARNING: RUN statement ignored due to previous errors. Submit QUIT; to terminate the procedure.
I just give some comments here.

1) when one defines a macro, one should also define a parameter with it. This way SAS will create a local macro table to hold all macro variables. Those macro variables will disappear when its enviroment ends
example  %macro create(dummy);

2) a macro call needs not a semicolomn (;) as its end and there is no run needed.


12
PharmD 发表于 2010-10-9 14:02:42
xiezuosheng 发表于 2010-10-8 09:35
3# PharmD 按照你们的提示修改后还是 %analyze(yes,Idx000001,1999,收盘价,clpr,1999);
这个地方出错,我是刚刚开始学习宏,对错误的原因不太明白,请多多指导,谢谢!
错误的根源不在这个地方。在ERROR: File WORK.TEMP.DATA does not exist.

你的数据文件temp.sas7bdat到底放在哪里?不把这个解决下面的都无法运行。

13
PharmD 发表于 2010-10-9 14:05:37
jingju11 发表于 2010-10-8 09:47
globalize 'number'
for exmpale in %create:
%global number;
用call symput生成的宏变量本来就都是全局的,不需要再“全局化”了。楼主的问题在于因为那个temp数据集不在work中,所以从第一步开始就不能运行。

14
xiezuosheng 发表于 2010-10-9 22:41:50
%macro create;
data temp;
set Resdat.&dat end=final;
13# PharmD 我也认为问题的症结在这里,我的本意是要定义一个temp数据集,这个数据集就是复制逻辑库Resdat中某个数据集的内容!如果不在宏里面运行,肯定是没问题的,
%macro create;
data temp;
set Resdat.&dat end=final;

15
PharmD 发表于 2010-10-9 23:26:32
xiezuosheng 发表于 2010-10-9 07:41
%macro create;
data temp;
set Resdat.&dat end=final;
13# PharmD 我也认为问题的症结在这里,我的本意是要定义一个temp数据集,这个数据集就是复制逻辑库Resdat中某个数据集的内容!如果不在宏里面运行,肯定是没问题的,
%macro create;
data temp;
set Resdat.&dat end=final;
我又回去看了一遍,发现你的create和plot宏写得有问题。像下面这样为每个宏定义参数,应该就可以了。注意这只是改正了你的语法错误而不是(可能的)算法错误。比如说plot宏里面你到底要用year还是year1我也不清楚。
  1. %macro create(dat, year);
  2. data temp;
  3. set Resdat.&dat. end=final;
  4. if year(date)=&year. then do;
  5. n+1;
  6. output;
  7. end;
  8. if final then call symput("number",n);
  9. /*创建的宏变量为number,其值为n*/
  10. run;

  11. %mend create;
  12. %macro plot(pr, year, price);
  13. proc gplot data=temp;
  14. title2"&pr. &year1.时序图";
  15. plot &price.*date=1;
  16. symbol1 v=star i=join r=1 c=red;
  17. run;
  18. quit;
  19. %mend plot;


  20. %macro analyze(getdata,dat,year,pr,price,year1);
  21. %if %upcase(&getdata.)="YES" %then %create(&dat., &year.);
  22. footnote "plot of &number. of observations";
  23. %plot(&pr., &year., &price.);
  24. %mend analyze;
  25. run;

  26. %analyze(yes,Idx000001,1999,收盘价,clpr,1999);
  27. %analyze(yes,Idx000002,1999,收盘价,clpr,1999);
  28. %analyze(yes,Idx000001,1999,收盘价,clpr,2000);
  29. %analyze(yes,Idx000002,1999,收盘价,clpr,2000);
复制代码
建议仔细研读你所用教科书上关于全局宏变量和局部宏变量的区别。

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

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