English 中文(简体)
• 如何正确读用SAS宏观结构中的错误代码?
原标题:how to correctly read error code in SAS macro loop?

I m 试图制定从过去的记录看的SAS代码,重开失败的报告,并通报重新运行的状况。 I m 运用和安装;syscc. 宏观,以检查在我重新开张的尝试中是否有错误,并在每次尝试之间重新确定错误。 然而,Im在一种宏观百分数的 lo体内陷入一个问题,因为cc没有显示正确的错误代码。 I.e. 它没有显示第一胎的错误,尽管它应当消失,然后又没有回落到零。 (与以下编码部分共享) 是否有其他办法使用玩具或另一种文字方式?

%macro rerunner();
    %do i=1 %to &numrecs;
    %put &i;
    

        /*selecting a log and code*/
        proc sql;
            select cname into :  selected_code separated by  , 
            from code_logs
            where rown=&i;
        quit;   
        proc sql;
            select lname into :  selected_log separated by  , 
            from code_logs
            where rown=&i;
        quit;

        /*counting errors*/
        data _null_; 
            infile "&loglocation./&selected_log."  end=last;
            input;
            if _infile_  =:  ERROR  then error_count+1;
            if last and error_count >=1 then CALL EXECUTE( %rerun(&selected_log. ,         &selected_code.) ) ;
            if last and error_count < 1 then CALL EXECUTE( %no_rerun(&selected_log.) ) ;
        run;


    %end;



%mend;


%macro rerun(log,code);


    %let syscc = 0;
    %include "&codelocation./&code." ;

    %if &syscc. <= 6 %then %do;
            %success(&log.);
        %end;

    %else %do;
            %failure(&log.)
        %end;

%mend;


...
    
%rerunner();
问题回答

You ve got a timing issue here. Your macro %rerun is using the &syscc defined as it is before anything happens!

想到的是,这有两张通行证。 首先是“macro”通行证,然后是“SAS”通行证。

  • Macro pass: do all of the macro language things (stuff that starts with a % mostly), and create a larger SAS code
  • SAS pass: run as SAS code

这样,再开工的比例就是如此。

起始代码:

%macro rerun(log,code);    
    %let syscc = 0;
    %include "&codelocation./&code." ;
    %if &syscc. <= 6 %then %do;
            %success(&log.);
        %end;
    %else %do;
            %failure(&log.)
        %end;
%mend;

宏观发展步骤:

%let syscc=0;

页: 1

%include "&codelocation./&code.";

- 将该守则列入《联合国系统会计准则》中(但并未适用)

%if &syscc <= 6 %then %do;
     %success(&log.);
%end;

页: 1, so it is less than six, so do this part -- unroll macro success into SAS code and include it here

%else %do;
%end;

——如果是真的,那是不会的。

So now, the %rerun macro goes into the SAS code step, and actually does things. Except... it doesn t matter whether the code sets an error or not, because there s no longer a check of syscc anywhere - it s already going through the syscc=0 path.

如果你想做这样的事情,那么你就必须有办法,在宏观excecution期间,以互动方式审视 s的价值。 这是可能的,但并非最佳设计。

Better design here would be to store the value of &syscc into a dataset at the end of the execution of your macro (or whatever), and check that, probably calling the rerun from that.

data syscc_status;
input prog $ timestamp :datetime17. syscc;
datalines;
prog1 01JAN2024:01:01:01 3
prog2 01JAN2024:01:01:02 8
prog3 01JAN2024:01:01:03 6
;;;;
run;

Something like that. Then your programs when they run can be in a wrapper like so:

%macro runprogram(program=,log=);
  %let syscc=0;
  proc printto log="&log";
  run;
  %include "&program";
  proc printto;
  run;

  data syscc_append;
    prog="&program";
    timestamp = now();
    syscc=&syscc;
  run;

  proc append base=syscc_status data=syscc_append;
  run;
%mend runprogram;

现在,你可以这样做:

proc sql;
  select cats( %rerun(program= ,prog, ) ) into :proglist separated by    
   from syscc_status
    where timestamp gt dhms(today(),0,0,0)
  ;
quit;

&proglist;

缩略语 宏观仅指代相干的宏观(可能只是直截了直截了当,除非你想做其他事情)。 显然,如果不简单,你也可能需要寄出标识。

正如Joe提到的那样,你似乎被传统地滥用了CALL EXECUTE(CALL EXECUTE)来管理宏观法典。 由于CALL EXECUTE(CALL EXECUTE)提交的代码在数据步骤之后运行,你也希望法典中的宏观呼吁不会持续到数据步骤之后。 否则,宏观逻辑就会在法典之前得到评估,而代之的是,宏观造法会带来经营的机会。

因此,将宏观呼吁与国内生产总值相提并论。 您将立即注意到要求执行《联合国系统会计准则》的编码线上的区别。 如果没有宏观引用CODE的话,宏观生成就会得到响应。 随着宏观引用实际宏观呼吁本身,也得到了响应。

例:

92   %macro print(ds);
93   proc print data=&ds; run;
94   %mend print;
95
96   data _null_;
97    call execute( %print(sashelp.class) );
98    call execute( %nrstr(%print)(sashelp.class) );
99   run;

NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds


NOTE: CALL EXECUTE generated line.
1   + proc print data=sashelp.class; run;

NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: PROCEDURE PRINT used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds


2   + %print(sashelp.class)

NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: PROCEDURE PRINT used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds

It does not matter for this trivial example because the macro does not have any macro logic that makes decisions about what code to run based on the results of code that the macro had generated earlier. But your %RERUN() macro does.

避免这一陷阱的另一个容易的方法不是使用CALL EXECUTE。 相反,在数据步骤结束时,将守则写到档案中,然后使用 %的INCLUDE操作。

So something like this:

filename code temp;
data _null_;
  set somedataset;
  file code;
  if (some condition) then put  %rerun(  somevar  )  ;
run;
%include code / source2;




相关问题
SAS stack overflow: PROC SQL reading dictionary.columns

I have a program in which I am reading dictionary.columns. There is a big program with lot of code before and after the program segment in which I read dictionary.column. The program used to work ...

SAS using encrypted (PWENCODE) in EMAILPW= option

My code works fine using plain text code, but fails when I use an encrypted password filename File email emailsys = VIM emailid= "&pa_usr" emailpw= "{sasenc}39AAD23E148A9555508AC84447181DFF" ; ...

How do I change the label in a data step header?

In SAS you can do. data a(rename=(a=b) ); a = 1; run; to rename a variable in the data step data statement (or data step header as I call it). What s the syntax to change the label? I tried ...

What s the easiest way to use SQLite with SAS?

I want to investigate how to access SQLite DB from SAS. What s the easiest way of doing this? Is there a SAS product that we can license to do that? I don t want to use ODBC drivers as that seems to ...

Computing Compounded Return in SAS

I have a dataset of date(monthly), person and return(monthly). I need to calculate the compounded monthly return of the dataset from April Year t to March Year t+1 for each person. For example, ...

Exchange Server and SAS 9.1.3/9.2

has anyone successfully interacted with a microsoft exchange server in SAS 9.1.3 or 9.2? i know it can be done with SAS Ent. Guide 4.x, but i m not interested in that route if regular SAS can do it.

Logging SAS scripts

I ve been developing a lot of Java, PHP and Python. All of which offer great logging packages (Log4J, Log or logging respectively). This is a great help when debugging applications. Especially if the ...

热门标签