在 %EVAL 函数或 %IF 条件中找到字符操作数
A character operand was found in the %EVAL function or %IF condition
/* 这是有问题的代码*/
%macro numstats(var = ,file=, format=);
%let dsid=open(&file.,i);
%if %LENGTH(&var.) > 8 AND %VARTYPE(&dsid.,%VARNUM(&dsid.,&var.))='N' %then %do;
Proc SQL;
SQL code
quit;
end;
%mend numstats;
我在 运行 此代码时遇到以下错误:在 %EVAL 函数或 %IF 条件中找到了一个字符操作数,其中需要一个数字操作数。
我已经广泛使用此代码来尝试不同的在线解决方案,但无法弄清楚为什么我仍然收到此问题。请帮忙!
- 旁注:我必须添加此
IF
语句的原因是试图找出该变量是否为日期变量。我所有的日期变量都是 Date9.
所以如果它是数字并且长度大于 8,那么我想添加我不会添加到常规数字变量的日期格式。如果有人能想到更简单的方法来做到这一点,那么我也对此持开放态度,但请帮我找出这个错误!
如果您想在宏代码中调用数据步骤函数,则需要使用 %SYSFUNC() 宏函数调用它们。这是一个宏函数的示例,可以为您执行此操作。
%macro varinfo
/*----------------------------------------------------------------------
Retrieve attribute of a specified variable.
----------------------------------------------------------------------*/
(ds /* Data set name */
,var /* Variable name */
,info /* information attribute to return - Default is NUM */
);
/*----------------------------------------------------------------------
Example values for INFO parameter:
NUM = variable number
LEN = length of variable
FMT = format of variable
INFMT = informat of variable
LABEL = label of variable
TYPE = type of variable (N for numeric, C for character)
------------------------------------------------------------------------
Usage Examples:
%if %varinfo(&data,NAME)
%then %put input data set contains variable NAME;
%put Variable &column in &data has type %varinfo(&data,&column,type);
------------------------------------------------------------------------
Notes:
The macro call resolves to 0 when either the data set does not exist
or the variable is not in the specified data set.
Invalid values for the INFO parameter generate a SAS ERROR message.
----------------------------------------------------------------------*/
%local dsid rc varnum;
%let dsid = %sysfunc(open(&ds));
%if (&dsid) %then %do;
%let varnum = %sysfunc(varnum(&dsid,&var));
%if (&varnum) & %length(&info) %then
%sysfunc(var&info(&dsid,&varnum))
;
%else
&varnum
;
%let rc = %sysfunc(close(&dsid));
%end;
%else 0;
%mend varinfo;
使用这个你的宏可能会变成这样:
%macro numstats(var = ,file=, format=);
%if %varinfo(&file,&var,type)=N and
DATE = %sysfunc(substrn(%varinfo(&file,&var,fmt),1,4))
%then %do;
* do something ;
%end;
%mend numstats;
/* 这是有问题的代码*/
%macro numstats(var = ,file=, format=);
%let dsid=open(&file.,i);
%if %LENGTH(&var.) > 8 AND %VARTYPE(&dsid.,%VARNUM(&dsid.,&var.))='N' %then %do;
Proc SQL;
SQL code
quit;
end;
%mend numstats;
我在 运行 此代码时遇到以下错误:在 %EVAL 函数或 %IF 条件中找到了一个字符操作数,其中需要一个数字操作数。
我已经广泛使用此代码来尝试不同的在线解决方案,但无法弄清楚为什么我仍然收到此问题。请帮忙!
- 旁注:我必须添加此
IF
语句的原因是试图找出该变量是否为日期变量。我所有的日期变量都是Date9.
所以如果它是数字并且长度大于 8,那么我想添加我不会添加到常规数字变量的日期格式。如果有人能想到更简单的方法来做到这一点,那么我也对此持开放态度,但请帮我找出这个错误!
如果您想在宏代码中调用数据步骤函数,则需要使用 %SYSFUNC() 宏函数调用它们。这是一个宏函数的示例,可以为您执行此操作。
%macro varinfo
/*----------------------------------------------------------------------
Retrieve attribute of a specified variable.
----------------------------------------------------------------------*/
(ds /* Data set name */
,var /* Variable name */
,info /* information attribute to return - Default is NUM */
);
/*----------------------------------------------------------------------
Example values for INFO parameter:
NUM = variable number
LEN = length of variable
FMT = format of variable
INFMT = informat of variable
LABEL = label of variable
TYPE = type of variable (N for numeric, C for character)
------------------------------------------------------------------------
Usage Examples:
%if %varinfo(&data,NAME)
%then %put input data set contains variable NAME;
%put Variable &column in &data has type %varinfo(&data,&column,type);
------------------------------------------------------------------------
Notes:
The macro call resolves to 0 when either the data set does not exist
or the variable is not in the specified data set.
Invalid values for the INFO parameter generate a SAS ERROR message.
----------------------------------------------------------------------*/
%local dsid rc varnum;
%let dsid = %sysfunc(open(&ds));
%if (&dsid) %then %do;
%let varnum = %sysfunc(varnum(&dsid,&var));
%if (&varnum) & %length(&info) %then
%sysfunc(var&info(&dsid,&varnum))
;
%else
&varnum
;
%let rc = %sysfunc(close(&dsid));
%end;
%else 0;
%mend varinfo;
使用这个你的宏可能会变成这样:
%macro numstats(var = ,file=, format=);
%if %varinfo(&file,&var,type)=N and
DATE = %sysfunc(substrn(%varinfo(&file,&var,fmt),1,4))
%then %do;
* do something ;
%end;
%mend numstats;