检查 SAS-Macro 中的变量类型

Checking for variable type in SAS-Macro

我试图在宏中使用 proc sql 和 proc freq 过程来总结我的变量。

代码如下:

%macro des_freq(input= ,vars= );
  %let n=%sysfunc(countw(&vars));
  %let binary=NO;
  %do i = 1 %to &n;
    %let values = %scan(&vars, &i);
      %if %datatyp(&values)=NUMERIC %then %do;
        proc summary data = &input;
          output out=x min(&values)=minx max(&values)=maxx;
        run;
        data _null_;
          set x;
          if minx = 0 and maxx = 1 then call symputx('binary','YES');
        run;
        %if &binary = YES %then %do;
          proc sql;
            select segment_final,
                  (sum(case when &values = 1 then 1 else 0 end)/ count(*)) * 100      as &values._percent
            from &input
            group by segment_final;
          quit;
        %end;
        %else %do;
          proc freq data =&input;
            tables segment_final*&values/nofreq nopercent nocol;
          run;
        %end;
      %end;

     %else %do;
        proc freq data =&input;
           tables segment_final*&values/nofreq nopercent nocol;
        run;
     %end;
  %end;
%mend;

我的变量可以是数字或字符。如果它是数字,它可以有 2 个不同的值。

我想要二进制变量中 1 的百分比(因此 proc sql)和每个段的所有不同变量的百分比(因此 proc freq)。

我的第一个 if 语句是检查变量是否为数字,然后如果它是数字,接下来的几个步骤是检查它是否是二进制的。如果是二进制文件则执行 proc sql 否则执行 proc freq。

如果变量是字符那么只执行proc freq。

我不知道如何检查我的变量是否为数字。我尝试了 %SYSFUNC(Vartype)、%isnum 和 %DATATYP。 None 其中似乎有效。请帮忙!!

首先你可以查看 sashelp.vcolumn table 来检查变量类型:

data want(keep=libname  memname name type);
    set sashelp.vcolumn( where= (libname='SASHELP' and memname='CLASS'));
run;

如果您不想使用 vcolumn table,您可以使用 vtype() 数据步进函数,正如@Tom 建议的那样:

data _NULL_;
    set  &input (obs=1);
    call symput('binary',ifc(vtype(&values)='N','YES','NO' ));
run;