代码之家  ›  专栏  ›  技术社区  ›  NicChik

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

  •  0
  • NicChik  · 技术社区  · 7 年前

    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);
    
    2 回复  |  直到 7 年前
        1
  •  1
  •   Tom    7 年前

    我认为您的问题是,默认情况下,SAS将使用 %EVAL() 函数,该函数只能处理整数比较。如果要使用浮点值或日期文字,则需要显式使用 %SYSEVALF()

    %if %sysevalf(&split <= 0 or &split >= 1) %then %do;
    
        2
  •  0
  •   user133631    7 年前

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