SAS-在宏中使用If-then-do

时间:2018-09-11 20:06:17

标签: sas sas-macro

我正在创建一个宏以从数据库中提取表,然后使用 Workbook workbook = WorkbookFactory.create(new FileInputStream(new File(SAMPLE_XLSX_FILE_PATH))); Sheet sheet = workbook.getSheetAt(0); DataFormatter dataFormatter = new DataFormatter(); for (Row row: sheet) { for(Cell cell: row) { String cellValue = dataFormatter.formatCellValue(cell); XSSFCellStyle cellStyle = (XSSFCellStyle)cell.getCellStyle(); XSSFColor cellColor = cellStyle.getFillForegroundXSSFColor(); if( cellColor != null && cellColor.getARGBHex().equals("FFCCFFCC") ){ System.out.println(cellValue); System.out.println(cell.getAddress()); } } } 导出其中一些表。我通过使用if-then-do语句来检查宏变量是否等于字符串,如果这样做,则导出该表。这是我的代码:

proc export

这将产生一个名为%MACRO query(x); proc sql; connect using conn; create table &x. as select * from connection to conn (select * from db.&x.); disconnect from conn; quit; %if &x. = "AddressCategory" %then %do; proc export data = &x. outfile="C:\path\&x..txt" dbms=dlm replace; delimiter="|"; run; %end; %mend query; %query(AddressCategory); 的数据集,但是导出无效。有任何想法吗?谢谢!

1 个答案:

答案 0 :(得分:0)

  

然后导出其中一些表

在宏中,您可能不想检查要按名称导出的项目。如果这样做,宏将增长为包含大量ifs,以检查哪个参数适合导出。

更好的宏将具有一个附加的export=参数,默认为0,并且被调用方将其设置为1,以便导出数据集

%MACRO fetcher (
  remoteLibref=conn, 
  remoteSchema=db, 
  object=, 
  outlib=work, 
  out=&object, 
  export=0, 
  outpath=C:\exports\&remoteSchema,
  outname=&object
);
  proc sql;
    connect using &remoteLibref;
    create table &outlib..&out as select * from connection to &remoteLibref
    ( select * 
      from &remoteSchema..&object
    );
    disconnect from &remoteLibref;
  quit;

  %if &export %then %do;
      proc export 
        dbms=dlm
        data=&outlib..&out. 
        replace outfile="&outpath.\&object..txt"
      ;
        delimiter="|"; 
      run;
  %end;
%mend fetcher;

为您的各种远程对象调用它,明确指定要导出的对象

libname conn sqlserver … connection string … ;

%fetcher(object=AddressCategory, export=1)
%fetcher(object=AddressBook)
%fetcher(object=PreferenceCategory, export=1)
%fetcher(object=PreferenceItems)
%fetcher(object=PreferenceProperties)
%fetcher(object=Payouts, export=1)
%fetcher(object=Payouts, RemoteSchema=DBX, export=1, outname=hidden-payouts)