簡體   English   中英

SAS:數據步驟中的宏過程錯誤

[英]SAS: macro procedure error in data step

我找不到該錯誤的解決方案。 我嘗試了成功的%eval,%sysfunc和%sysevalf,但沒有成功。 正確評估宏中的“&set”需要什么?

%macro set_istituzionale(set=, varout=);
  %if &set/100 = 1 %then &varout = 'AP';
%mend set_istituzionale;

data soff2; set soff; %set_istituzionale(set=setcon,varout=set); run;

ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition was:
       &set/100 = 1
ERROR: The macro SET_ISTITUZIONALE will stop executing.

您是宏背后的邏輯是錯誤的。

您正在數據步驟內調用該宏,因此該宏應包含可以在數據步驟內解析的內容(一條語句)。

If-else不會以宏語言編寫,而是以普通的datastep語言編寫。

%macro set_istituzionale(set=, varout=);
  if &set/100 = 1 then &varout = 'AP';
%mend set_istituzionale;

現在,如果您使用呼叫,則可以通過以下方式解決數據步驟:

data soff2; 
set soff;
%set_istituzionale(set=setcon,varout=set);
run;

將成為:

data soff2; 
set soff;
if setcon/100 = 1 then set = 'AP';
run;

在您的代碼上,您使用的是宏代碼,因此您的步驟在內部由宏布爾語句%if和&set / 100解析,而%if setcon / 100則setcon是字符串(此處,我們有條件地使用宏語言,寫為變量將無法捕獲變量的值,因為它與數據步驟完全獨立)。

您應該僅將宏語言視為可以為您編寫代碼的內容,例如,可以使用諸如您的第一次嘗試之類的條件將語句有條件地插入到宏變量的值中,例如:

data try;
set want;

%if &macrovar=A %then %do;
if var1=1 and var2='B' then var3='C';
%end;

%if &macrovar=B %then %do 
if var1=2 and var2='A' then var3='D';
%end;

run;

when the macro variable macrovar will be = A the step will be:

data try;
set want;
if var1=1 and var2='B' then var3='C';
run;

if the macro variable macrovar will be = B the step will be:

data try;
set want;
if var1=2 and var2='A' then var3='D';
run;

但是您也可以在數據步驟之外使用宏代碼,例如對宏變量的值有條件地執行數據步驟或其他操作:

%if &macrovar=A %then %do;

data try; set tr;
some code;
run;

%end;

%else %if &macrovar=B %then %do;

proc sort data=tr out=try nodupkey; by var1; run;

%end;

暫無
暫無

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

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