SAS:检查它们是否存在后堆栈文件

时间:2015-05-06 21:00:38

标签: sas partition datastep

我正在查看一堆按日历年和其他变量分区的文件。我想要做的是按财政年度对它们进行分区(基于记录中存在的日期变量)。例如,要创建FY2010文件,我需要将CY2009和CY2010文件(让我们将这些文件调用file_2009和file_2010)和子集堆叠到FY2010记录中。但是,其中一个或两个文件可能不存在。我想要做的是,如果其中一个文件不存在,那么只需使用另一个。如果两者都退出则使用两者。否则,什么也不做。我提出的方法有点不那么紧凑,我想要。有关最佳方法的任何想法吗?谢谢。

1 个答案:

答案 0 :(得分:1)

我将把分裂逻辑留给你 - 正如其他人所评论的那样,这在SAS中很少是一个好主意,因为通过分组处理实现相同的结果几乎总是更简单。

这是我能想到的最简单的组合文件的方法。

/*First, generate some dummy data*/

data cy2002 cy2004 cy2005;
      do year = 2002,2004, 2005;
            do _n_ = 1 to 10;
                  date =mdy(ceil(ranuni(1)*12),1,year);
                  format date yymmdd10.;
                  if year = 2002 then output cy2002;
                  if year = 2004 then output cy2004;
                  if year = 2005 then output cy2005;
            end;
      end;
run;

/*
  Generate a listing of all sas datasets in the appropriate library.
  For simplicity, assume these are sequentially named and are the only datasets in the library.
*/

ods listing close;
ods output members = members;
proc datasets lib = work memtype=data;
run;
quit;
ods listing;

/*Use the listing dataset to create a view that pieces together all of the calendar year datasets*/ 

data _null_;
      set members end = eof;
      if _n_ = 1 then call execute('data combined /view = combined; set ');
      call execute(name);
      if eof then call execute('; run;');
run;

然后,您可以使用该视图根据日期计算财务年度,并将其用于所需的进一步处理。