SAS中文论坛

 找回密码
 立即注册

扫一扫,访问微社区

查看: 843|回复: 7
打印 上一主题 下一主题

Error: No matching %marco with this %mend

[复制链接]

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
楼主
 楼主| 发表于 2010-5-3 05:05:30 | 只看该作者

Error: No matching %marco with this %mend

运行macro的时候出现神奇的错误:

%macro cash(num_cycles=);
%do p=1 %to &num_cycles;
  %let year_cycle=1950+&p;
。。。。。。。。。。中间等等步骤都ok的
%if &p=1 %then
  %do;
        data worklib.d_cash_study;
        set d_cash;
  %end;
%else
  %do;
      proc append base=worklib.d_cash_study data=d_cash;
      run;
  %end;
%end;

%mend cash;

%cash(num_cycles=2);

然后出现错误:
[color=#FF0000:2viia1v4]ERROR: The %IF statement is not valid in open code.
MPRINT(CASH):   data worklib.d_cash_study;
MPRINT(CASH):   set d_cash;
ERROR: The %END statement is not valid in open code.
ERROR: The %ELSE statement is not valid in open code.[/color:2viia1v4]

NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORKLIB.D_CASH_STUDY may be incomplete.  When this step was stopped there
         were 0 observations and 7 variables.
WARNING: Data set WORKLIB.D_CASH_STUDY was not replaced because this step was stopped.
NOTE: DATA statement used (Total process time):
      real time           0.03 seconds
      cpu time            0.01 seconds



MPRINT(CASH):   proc append base=worklib.d_cash_study data=d_cash;
MPRINT(CASH):   run;

NOTE: Appending WORK.D_CASH to WORKLIB.D_CASH_STUDY.
NOTE: There were 10178 observations read from the data set WORK.D_CASH.
NOTE: 10178 observations added.
NOTE: The data set WORKLIB.D_CASH_STUDY has 122136 observations and 7 variables.
NOTE: PROCEDURE APPEND used (Total process time):
      real time           0.02 seconds
      cpu time            0.01 seconds

[color=#FF0040:2viia1v4]
ERROR: The %END statement is not valid in open code.[color=#FF0000][/color:2viia1v4]
ERROR: The %END statement is not valid in open code.
ERROR: No matching %MACRO statement for this %MEND statement.[/color]

我已经被这个问题弄抓狂了~提前谢谢各位!
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
沙发
 楼主| 发表于 2010-5-3 08:03:15 | 只看该作者

Re: Error: No matching %marco with this %mend

你怀疑的代码好象是良民,是不是看看OK的那部分有没有敌特?
[code:12xgtfzn]%macro cash(num_cycles=);
        %do p=1 %to &num_cycles;
                %let year_cycle=%eval(1950+&p);
                %put NOTE:do loop:&p--> %nrstr(&year_cycle)=&year_cycle;
/*                。。。。。。。。。。中间等等步骤都ok的*/
                %if &p=1 %then
                        %do;
/*                                data worklib.d_cash_study;*/
/*                                        set d_cash;*/
/*                                run;*/
                                %put NOTE:data step ok!;
                        %end;
                %else
                        %do;
/*                                proc append base=worklib.d_cash_study data=d_cash;*/
/*                                run;*/
                                %put NOTE:proc step ok!;
                        %end;
        %end;
%mend cash;

%cash(num_cycles=2);[/code:12xgtfzn]
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
板凳
 楼主| 发表于 2010-5-3 10:17:22 | 只看该作者

Re: Error: No matching %marco with this %mend

那我干脆全贴上来?就怕太多了能解答的人没耐心看;

%macro cash(num_cycles=);
%do i=1 %to &num_cycles;
  %let year_cycle=1950+&i;

  %*Get monthly market return;
  proc sql;
  create table d as
  select
  d_ff.*,
  (year(d_ff.date)*100+month(d_ff.date)) as month
  from
   d_ff
  where
   &year_cycle-4<=year(d_ff.date)<=&year_cycle;

%*Number of months;
  proc sort data=d out=d_months (keep=month) nodupkeys;
  by month;

  data d_months;
   set d_months;
   i+1;

  proc sort data=d_months;
   by month;

%*Compute average return of all stocks as proxy for monthly market return;
  proc sort data=d;
    by month;

  proc means data=d noprint;
  by month;
  output out=d_mkt_ret
  mean(ret)=month_mkt_ret;

  proc sort data=d_mkt_ret (keep=month month_mkt_ret);
  by month;

  data d;
  merge
  d_mkt_ret(keep=month month_mkt_ret)
  d_months(keep=i month);
  by month;

  proc sort data=d nodupkeys;
  by month;

  %*Merge market return and stock return;
  proc sql;
  create table d_1 as
  select
     d_ff.permno,
         d_ff.ret,
         (year(d_ff.date)*100+month(d_ff.date)) as month
  from
   d_ff
  where
   &year_cycle-4<=year(d_ff.date)<=&year_cycle;

  proc sort data=d_1;
  by month;

  data d_all;
  merge
    d_1(keep=permno month ret)
    d(keep=month i month_mkt_ret);
  by month;

  %*Keep only securities with returns in all 12 months;
  proc sort data=d_all;
  by permno;

  proc means data=d_all noprint;
  var ret;
  output out=d_all_stats_1 n=n_1;
  by permno;
  where
    1<=i<=48;  

  proc means data=d_all noprint;
  var ret;
  output out=d_all_stats_2 n=n_2;
  by permno;
  where
   48+1<=i<=48+12;

  data d_all_1;
  merge
   d_all d_all_stats_1 d_all_stats_2;
  by permno;
  if
   n_1=48 and n_2=12;

  %*Compute betas for firms;
  proc reg data=d_all_1 outest=d_all_reg (keep=intercept month_mkt_ret permno) noprint;
    model ret=month_mkt_ret;
    by permno;
    where
    1<=i<=48;
  run;
  data d_all_reg (rename=(intercept=alpha month_mkt_ret=beta));
  set d_all_reg;

  data d_all;
  merge
    d_all_1 d_all_reg;
   by permno;

  data d_all;
    set d_all;
   drop n_1 n_2 _type_ _freq_;

   %*Define portfolio according to company's cash holding;

   data d_cash_holding_1;
   set d_cash_holding;
   where
    year(date)=&year_cycle;

    proc sort data=d_cash_holding_1 (keep=permno cash_holding_prev_year) nodupkeys;
    by permno;

    proc sql;
    create table d_cash as
    select
            d_all.permno,
            d_all.ret,
            d_all.beta,
            d_all.month,
            d_cash_holding_1.cash_holding_prev_year as cash,
            (d_all.ret-d_all.alpha-d_all.beta*d_all.month_mkt_ret) as ab_ret
   from
        d_cash_holding_1 left join d_all
    on
        d_cash_holding_1.permno=d_all.permno
            and
             48+1<=i<=48+12
        order by
          permno,month;


    data d_cash;
    set d_cash;
    if 0<cash<0.20
    then
    sample=1;
    else
    if
    cash>=0.20
    then
    sample=2;
    else
   sample=0;

   retain sample;

   proc sort data=d_cash;
   by sample month;

    %if &i=1 %then
    %do;
        data worklib.d_cash_study;
        set d_cash;
    %end;
    %else
    %do;
      proc append base=worklib.d_cash_study data=d_cash;
      run;
    %end;
%end;

run;

%mend cash;
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
地板
 楼主| 发表于 2010-5-3 10:55:45 | 只看该作者

Re: Error: No matching %marco with this %mend

第113行,用后一种注释方式试试
[code:1kdafep4]%*Define portfolio according to company's cash holding;
/*Define portfolio according to company's cash holding*/[/code:1kdafep4]
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
5#
 楼主| 发表于 2010-5-3 13:17:00 | 只看该作者

Re: Error: No matching %marco with this %mend

to hopewell
你好厉害啊。这么细弱的问题也可以找得到。
的确,雷同于*, %* 不能包含 ;和‘, 单引号和分号。

你给我看看这个问题
在GTL里,如何写出 x ^2? 就是x和一个superscript 2
如果在entry 里,写法类似于 "x"{sup "2"}.为什么写到x轴label里就不起作用了呢?比如
[code:395t5tyq]columnaxes;                               
columnaxis/griddisplay = on display = (label tickvalues) label = '"Change of X"{SUP "2"}';                               
endcolumnaxes;[/code:395t5tyq]
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
6#
 楼主| 发表于 2010-5-3 14:45:36 | 只看该作者

Re: Error: No matching %marco with this %mend

现在才看到大神给的回复,果然很强。
我也自己发现了这个问题了,居然被一个单引号搞了2天。。。
崩溃ing

谢谢各位的热心解答!!!
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
7#
 楼主| 发表于 2010-5-3 21:02:12 | 只看该作者

Re: Error: No matching %marco with this %mend

To: 国际Su
好象是只能用于entry,瞎写一个,看看能用不
[code:3u1fct3l]data raw;
        do x=1 to 4;
                y1=ceil(3*ranuni(123));
                y2=ceil(3*ranuni(123));
                y3=ceil(3*ranuni(123));
                y4=ceil(3*ranuni(123));
        output;
        end;
run;
proc template;
        define statgraph test;
                begingraph;
                        entrytitle "ScatterPlot";
                        layout lattice / rows=2 columns=2
                                                        rowdatarange=union columndatarange=union
                                                        rowgutter=2px columngutter=2px;
                                scatterplot x=x y=y1;
                                scatterplot x=x y=y2;
                                scatterplot x=x y=y3;
                                scatterplot x=x y=y4;
                                rowaxes;
                                        rowaxis / griddisplay=on display=(tickvalues);
                                        rowaxis / griddisplay=on display=(tickvalues);
                                endrowaxes;
                                columnaxes;
                                        columnaxis / display=(tickvalues);
                                        columnaxis / display=(tickvalues);
                                endcolumnaxes;
                                rowheaders;
                                        entry "Rowheader1: Y "{sup "2"} / rotate=90;
                                        entry "Rowheader2: Y "{sup "2"} / rotate=90;
                                endrowheaders;
                                columnheaders;
                                        entry "ColumnHeader1: X "{sup "2"};
                                        entry "ColumnHeader2: X "{sup "2"};
                                endcolumnheaders;
                                sidebar / align=left;
                                        entry "SidebarLeft: Y "{sup "2"} / rotate=90;
                                endsidebar;
                                sidebar / align=bottom;
                                        entry "SidebarBottom: X "{sup "2"};
                                endsidebar;
                        endlayout;
                endgraph;
        end;
run;
proc sgrender data=raw template=test;
run;[/code:3u1fct3l]
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
8#
 楼主| 发表于 2010-5-3 23:11:39 | 只看该作者

Re: Error: No matching %marco with this %mend

to hopewell
A perfect example to illustrate how to use headers and sidbars. And the sidebar at bottom can exactly satisfy with me by replacing the -Y- label.
Cheers, JingJu
回复 支持 反对

使用道具 举报

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

本版积分规则

QQ|小黑屋|手机版|Archiver|SAS中文论坛  

GMT+8, 2026-2-4 06:46 , Processed in 0.075809 second(s), 20 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表