varnum

时间:2016-12-26 05:32:21

标签: sas

我正在尝试的是 - >如果& vars的varnum小于0意味着如果varnum大于0则DS中的变量不存在则失败。但是错误在varnum语句中出现了多个参数..代码如何在单个语句中检查& vars宏变量,其中包含这些变量BRTHDTC DMDTC RFENDTC RFSTDTC是否存在于ds中??

%macro drpchk (lib,dsn);
%local dsid result;
proc sql noprint;
    select distinct catx(".",libname,memname), name 
    into :DS separated by " ", :vars separated by " "
    from dictionary.columns
    where libname = upcase("&lib") and format=('YYMMDD10.');
quit;

data &dsn(keep=&vars);
set &DS;
run;
    %let dsid=%SYSFUNC(open(&dsn));
    %IF %SYSFUNC(varnum(&dsid,&vars)) < 0 %THEN %DO;
result=pass;
else result=fail;
end;
run;
%mend;
%drp_chk(dtelib,dte);

2 个答案:

答案 0 :(得分:0)

生成包含变量的数据集列表有很长的路要走。该程序查看所有变量,将其限制为选择列表,通过注释查看要修改的代码行。

该方法类似但没有宏。

/*Generate fake data to work with*/
data class1;
    set sashelp.class;
    drop age sex;
run;

data class2;
    set sashelp.class;
    drop weight height;
run;

data class3;
    set sashelp.class;
    Order=1;
run;

data class4;
    set sashelp.class;
    keep name;
run;

data class5;
    set sashelp.class;
run;

/*Extract metadata from dictionary tables*/
proc sql noprint;
    create table column_list as select memname, libname, name, type, 1 as count 
        from dictionary.columns where libname='WORK' and memname like 'CLASS%'
        /*ADD THE VARIABLE LIST HERE*/
        order by memname, name;
quit;

/*Transpose results to a more user friendly format*/
proc transpose data=column_list out=flipped;
    by memname;
    id name;
    idlabel name;
    var count;
run;

/*Format output*/
data want;
    retain memname;
    set flipped;
    array class(*) _NUMERIC_;

    do i=1 to dim(class);

        if class(i)=. then
            class(i)=0;
    end;
    All_Variables=sum(of _numeric_)-I;
    DROP I _NAME_;
run;

答案 1 :(得分:0)

我不确定这个过程的目标是什么,但也许这有助于简化宏的工作。让我们创建一个宏,它将获取输入数据集和变量名称列表,并返回它在该数据集中找到的那些变量的列表。

%macro drpchk(dsn,varlist,mvar=droplist);
%local lib qlist ;
%if not %symexist(&mvar) %then %global &mvar ;
%if %index(&dsn,.) %then %let lib=%upcase(%scan(&dsn,1,.));
%else %let lib=WORK ;
%let dsn=%upcase(%scan(&dsn,-1,.));
%let qlist="%sysfunc(tranwrd(%sysfunc(compbl(%upcase(&varlist))),%str( )," "))";
%let &mvar=;
proc sql noprint;
  select name
    into :&mvar separated by ' '
  from dictionary.columns
  where libname = "&lib"
    and memname= "&dsn"
    and upcase(name) in (&qlist)
  ;
quit;
%mend;

然后,您可以使用该列表生成drop语句。

data test;
  length SUBJID $10 BRTHDTC DMDTC RFENDTC RFSTDTC 8;
run;

%drpchk(test,BRTHDTC DMDTC RFENDTC RFSTDTC);
data want ;
  set test;
  drop &droplist;
run;