我创建了一个宏来将数据集拆分为训练和验证集。我注意到以下%if语句未正确评估,并且最近了解到这是因为宏处理器does not make a distinction between character and numeric values as the rest of SAS does(链接到SAS文档)。我通过使用%eval函数确认了这一点,如下所示:
%else %if %eval(&split) <= 0 or %eval(&split) >= 1 %then %do;
%put ERROR: The specified split of the data must be within the following range of real numbers (0,1].;
%end;
如何解决这个问题,以便它将宏变量“split”的输入正确读取为十进制?
可以使用以下示例代码:
data test (drop=i);
do i=1 to 1000;
a=round(uniform(1)*4,.01);
b=round(uniform(1)*10,.01);
c=round(uniform(1)*7.5,.01);
if b<2 then d=1;
else d=0;
if i<500 then y=1;
else y=0;
output;
end;
stop;
run;
%macro train_valid(split=);
%if &split = %then %put ERROR: Missing an input. Check to make sure the macro variable has an input.;
%else %if &split <= 0 or &split >= 1 %then %do;
%put ERROR: The specified split of the data must be within the following range of real numbers (0,1].;
%end;
%else %do;
proc surveyselect data=test samprate=&split seed=1000 out=Sample outall
method=srs noprint;
run;
data test_train;
set Sample;
where selected = 1;
run;
data test_valid;
set Sample;
where selected = 0;
run;
%end;
%mend;
%train_valid(split=.75);
答案 0 :(得分:1)
我认为您的问题是,默认情况下,SAS将使用%EVAL()
函数评估%IF,%WHILE等条件,该函数只能处理整数比较。如果要使用浮点值或日期文字,则需要明确使用%SYSEVALF()
来测试条件。
%if %sysevalf(&split <= 0 or &split >= 1) %then %do;
答案 1 :(得分:0)
尝试嵌套%eval函数,如下所示:
a*b + c