2017-06-28 64 views
0

我创建了一个宏将一个数据集分成一个训练和验证集。我注意到下面的%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; 

我怎样才能解决这个问题,这样它会读我的输入宏变量“分裂”正确的小数?

下面的示例代码可用于:

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); 

回答

1

我觉得你的问题是,默认情况下,SAS将在%IF评估条件,%WHILE等,使用%EVAL()函数,它只能处理整数比较。如果你想使用浮点值或日期文字,那么你需要明确地使用%SYSEVALF()来测试条件。

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

我不认为我已经理解如何正确使用%eval或%sysevalf函数,所以这为我提供了解决方案并帮助我理解。谢谢! – NicChik

0

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

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