/ *这是有问题的代码* /
%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,那么我想添加我不会添加到常规数字变量的日期格式。如果有人能想到更简单的方法,那么我也对此持开放态度,但请帮我弄清楚这个错误!答案 0 :(得分:0)
如果要在宏代码中调用数据步骤函数,则需要使用%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;