宏正在将数字输入转换为字符。如何将其改回数字?

Macro is converting numeric inputs to character. How to change it back to numeric?

我创建了一个宏来将数据集拆分为训练集和验证集。我注意到以下 %if 语句未正确评估,最近了解到这是因为宏处理器 does not make a distinction between character and numeric values as the rest of SAS does(link 到 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);

尝试像这样嵌套 %eval 函数:

%eval(%eval(&split <= 0) or %eval(&split >= 1))

我认为您的问题是默认情况下 SAS 将使用只能处理整数比较的 %EVAL() 函数评估 %IF、%WHILE 等中的条件。如果要使用浮点值或日期文字,则需要明确使用 %SYSEVALF() 来测试条件。

%if %sysevalf(&split <= 0 or &split >= 1) %then %do;