动态排除proc sql宏中的列名

时间:2018-12-13 17:50:15

标签: sas

我在宏函数中有一个proc sql语句,该语句从package first_project; import java.lang.reflect.*; import java.util.Scanner; public class My_Class { public static void main(String[] args)throws Exception { Scanner sc=new Scanner(System.in); //Getting numbers from user System.out.println("Enter First Number:"); int a=sc.nextInt(); System.out.println("Enter Second Number:"); int b=sc.nextInt(); //code for private method Class c=addition.class; Method m = addition.class.getDeclaredMethod("add(a,b)"); m.setAccessible(true); m.invoke(c); } } package first_project; //Class for addition public class addition{ private void add(int a,int b) { int ans= a+ b; System.out.println("addition:"+ans); } } 中选择列名。我想根据传递了参数的多个字符串模式排除列名-见下文

dictionary.columns

理想情况下,%symdel keepnames; %macro test(data=, col=); %global keepnames; %let data_lib = %sysfunc(upcase(%sysfunc(scan("&data", 1, ".")))); %let data_data = %sysfunc(upcase(%sysfunc(scan("&data", 2, ".")))); %put &data_lib; %put &data_data; proc sql noprint; select name into :keepnames separated by " " from dictionary.columns where libname = "&data_lib" and memname = "&data_data" and upcase(name) not like upcase("&col."); quit; %mend test; %test(data=sashelp.cars, col=mpg w) %put &keepnames; 参数将变成col,从而排除名称中带有mpg或w的任何列名称。

我遇到了几个问题。首先,我不太清楚如何在宏处理器中隐藏%mpg%, %w%。我尝试以多种方式使用%,但没有运气。其次,我很难在%str()参数中的单词周围添加%符号。任何帮助表示赞赏!

2 个答案:

答案 0 :(得分:2)

例如,将宏参数名称更改为更好的通知方式

%macro fetch_names (data=, dropPattern=, resultVar=fetchedNames)
   ...
%mend;

考虑传递正则表达式,而不是用空格分隔的值列表,这些值必须进行迭代。

 %let fetchedNames = ;
 %fetch_names (
   data = sashelp.cars
 , dropPattern = mpg|w     /* <------- regular expression pattern */
 , resultVar = fetchedNames
 )

宏的内部相似。


  • into :keepnames更改为
    into :&resultVar

  • upcase(name) not like upcase("&col.")更改为
    not prxmatch("/&dropPattern./i", name)

答案 1 :(得分:0)

以下我使用了contains运算符,并遍历col中的参数为每个排除项生成单独的测试。由于某种原因,我没有汽车,所以我上课:

%symdel keepnames;

%macro test(data=, col=);
    %global keepnames;

    %let data_lib = %sysfunc(upcase(%sysfunc(scan("&data", 1, "."))));
    %let data_data = %sysfunc(upcase(%sysfunc(scan("&data", 2, "."))));
    %put &data_lib;
    %put &data_data;
    %let i = 1;
    %let exclusion = %scan(&col,&i);

    proc sql noprint;
        select name into :keepnames separated by " "
        from    dictionary.columns
        where   libname = "&data_lib" and
        memname = "&data_data"

        %do %while(&exclusion ne );
            and upcase(name) not contains upcase("&exclusion")

            %let i = %eval(&i + 1);
            %let exclusion = %scan(&col,&i);
        %end;
        ;
    quit;
%mend test;

option mprint;
%test(data=sashelp.class, col=ame x)
%put &keepnames;

%test(data=sashelp.class)
%put &keepnames;

%test(data=sashelp.class, col=e)
%put &keepnames;