簡體   English   中英

在宏內使用數據集名稱內的變量時,SAS語法錯誤22和200

[英]SAS syntax error 22 and 200 when using variable inside dataset name inside macro

我正在嘗試使用宏內的循環,在數據集名稱內使用數字列表(其中一些數字帶有前導零)對SAS中的幾個數據集進行排序(例如,在示例代碼中為01,02 ),並且該代碼還將指導我要構建的其他一些循環。

我使用了SAS指南,以從此處開始的宏DO循環代碼循環遍歷一個不連續的值列表: http : //support.sas.com/kb/26/155.html

data dir1201;
   input compid directorid X;
   format ;
   datalines;
01 12 11  
02 15 5  
;
run;

data dir1202;
   input compid directorid X;
   format ;
   datalines;
01 12 1  
03 18 8  
;
run;

%macro loops1(values);                                                                                                         
     /* Count the number of values in the string */                                                                                                                                   
     %let count=%sysfunc(countw(&values)); 
     /* Loop through the total number of values */                                                                                         
     %do i = 1 %to &count;
      %let value=%qscan(&values,&i,%str(,)); 
      proc sort data=dir12&value out=dir12sorted&value nodupkey;
      by directorid compid;
      run;
      %end;
%mend;
options mprint mlogic;
%loops1(%str(01,02))  

我假設非順序列表需要str ,但是當我想保留前導零時這也很有用。

我看到宏變量似乎在日志中包含了01或02,但是隨后我立即收到錯誤22和200。 這是使用此示例代碼的日志錯誤代碼段:

339  %macro loops1(values);
340       /* Count the number of values in the string */
341       %let count=%sysfunc(countw(&values));
342       /* Loop through the total number of values */
343       %do i = 1 %to &count;
344        %let value=%qscan(&values,&i,%str(,));
345        proc sort data=dir12&value out=dir12sorted&value nodupkey;
346        by directorid compid;
347        run;
348        %end;
349  %mend;
350  options mprint mlogic;
351  %loops1(%str(01,02))
MLOGIC(LOOPS1):  Beginning execution.
MLOGIC(LOOPS1):  Parameter VALUES has value 0102
MLOGIC(LOOPS1):  %LET (variable name is COUNT)
MLOGIC(LOOPS1):  %DO loop beginning; index variable I; start value is 1; stop value is 2; by
      value is 1.
MLOGIC(LOOPS1):  %LET (variable name is VALUE)
NOTE: Line generated by the macro variable "VALUE".
1    dir1201
          --
          22
           --
           200
ERROR: File WORK.DIR12.DATA does not exist.

我不明白為什么顯示dir1201 ,但是錯誤是引用數據集work.dir12 (忽略01

宏引用使解析器誤以為宏引用表明新令牌的開始。 您可以添加%unquote()來刪除宏引用。

proc sort data=%unquote(dir12&value) out=%unquote(dir12sorted&value) nodupkey;

或者只是不添加宏引用開頭。

%let value=%scan(&values,&i,%str(,));

如果將宏設計為使用空格分隔的值而不是逗號分隔,則使用宏會容易得多。 然后,也無需在調用中添加宏引用。

%macro loops1(values);
%local i value ;
%do i = 1 %to %sysfunc(countw(&values,%str( )));
  %let value=%scan(&values,&i,%str( ));
proc sort data=dir12&value out=dir12sorted&value nodupkey;
  by directorid compid;
run;
%end;
%mend loops1;
%loops1(values=01 02)

宏聲明選項/PARMBUFF用於使自動宏變量SYSPBUFF可用於掃描作為參數傳遞的任意數量的逗號分隔值。

%macro loops1/parmbuff;
  %local index token;
  %do index = 1 %to %length(&syspbuff);
    %let token=%scan(&syspbuff,&index);
    %if %Length(&token) = 0 %then %goto leave1;
    proc sort data=dir12&token out=dir12sorted&token nodupkey;
    by directorid compid;
    run;
  %end;
  %leave1:
%mend;
options mprint nomlogic;
%loops1(01,02)

SYSPBUFF
宏/面包屑

由於您要遍歷日期,因此從長遠來看,我認為像這樣的事情可能會更有幫助:

%macro date_loop(start, end);
    %let start=%sysfunc(inputn(&start, anydtdte9.));
    %let end=%sysfunc(inputn(&end, anydtdte9.));
    %let dif=%sysfunc(intck(month, &start, &end));

    %do i=0 %to &dif;
        %let date=%sysfunc(intnx(month, &start, &i, b), yymmdd4.);
        %put &date;
    %end;
%mend date_loop;

%date_loop(01Jan2012, 01Jan2015);

這是SAS文檔(宏附錄,示例11)中的版本略有修改的版本。

http://documentation.sas.com/?docsetId=mcrolref&docsetTarget=n01vuhy8h909xgn16p0x6rddpoj9.htm&docsetVersion=9.4&locale=zh-CN

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM