【问题标题】:SAS, handle Macro outputSAS,处理宏输出
【发布时间】:2014-04-28 18:26:56
【问题描述】:

我从 SAS 网站获取了一个 SAS 宏,以便列出文件夹中的所有文件。 这是完整的参考:http://support.sas.com/kb/25/074.html

这就是代码:

%macro drive(dir,ext);                                                                                                                        
  %let filrf=mydir;                                                                                                                       
  /* Assigns the fileref of mydir to the directory and opens the directory */                                                                    
  %let rc=%sysfunc(filename(filrf,&dir));                                                                                                
  %let did=%sysfunc(dopen(&filrf));                                                                                                       
  /* Returns the number of members in the directory */                                                                   
  %let memcnt=%sysfunc(dnum(&did));                                                                                                     
   /* Loops through entire directory */                                                                                                  
   %do i = 1 %to &memcnt;               
     /* Returns the extension from each file */                                                                                                                                    
     %let name=%qscan(%qsysfunc(dread(&did,&i)),-1,.);                                                                                                 
     /* Checks to see if file contains an extension */                                                                                     
     %if %qupcase(%qsysfunc(dread(&did,&i))) ne %qupcase(&name) %then %do;                                                                     
     /* Checks to see if the extension matches the parameter value */                                                                      
     /* If condition is true prints the full name to the log       */                                                                      
      %if (%superq(ext) ne and %qupcase(&name) = %qupcase(&ext)) or                                                                       
         (%superq(ext) = and %superq(name) ne) %then %do;                                                                                     
         %put %qsysfunc(dread(&did,&i));                  
      %end;                                                                               
     %end;                                                                                                                               
   %end;                                                                                                                                 
  /* Closes the directory */                                                                                                            
  %let rc=%sysfunc(dclose(&did));                                                                                                                                 
%mend drive;                                                                                                                            

/* First parameter is the directory of where your files are stored. */                                                                
/* Second parameter is the extension you are looking for.           */                                                                
/* Leave 2nd paramater blank if you want a list of all the files.   */                                                                
%drive(c:\,sas)

这个宏(显然)工作正常,问题是在日志中返回结果。 我需要将这些结果放入 SAS 数据集中以安排其他操作。 我该怎么做?

提前致谢。

【问题讨论】:

    标签: macros dataset sas output


    【解决方案1】:

    首先,如果你能做一个管道,你可以:

    filename dirlist pipe "dir /b c:\*.sas";
    
    data myfiles;
    infile dirlist lrecl=512 truncover;
    input 
    @1 fullname $512.;
    filename = scan(fullname,-1,'\');
    run;
    

    如果由于系统限制确实需要使用该宏,则不能直接从中获取输出,因为它除了打印到日志之外什么都不做。您需要做以下两件事之一:

    • 使用 PROC PRINTTO 将日志重定向到一个文件,然后您可以对其进行解析
    • 更改实际执行某些操作的代码行。 %put %qsysfunc(dread(&did,&i)); 如果您将其更改为,例如,

      文件名 = 恐惧(&did,&i); 输出;

    然后您从数据步骤内部调用宏并使用结果。实际上,您最好只在数据步骤中运行所有这些而不用担心宏 - 它比仅宏所需的复杂得多;在更短的数据步中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-20
      • 1970-01-01
      • 2021-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-13
      • 1970-01-01
      相关资源
      最近更新 更多