SAS:从多个数据集创建多个文件

时间:2012-06-09 08:57:14

标签: database sas dataset extract

我有24个以相同方式构建的数据集。我的意思是相同的列标题(时间,日期,价格,股票代码),数据集结构等。我不希望附加所有24个文件,因为一个数据集要处理大。我将所有数据集命名为“file1 file2 file3 file4 .... up to file24”。

我想做的是以下内容:

  1. 例如,一次更改所有24个文件中的日期格式;

  2. 能够从每个文件中提取#特定的股票代码,如“戴尔”,并附加所有提取的“戴尔”数据;

  3. 最后,我怎样才能创建一个循环,允许我将股票代码从'Dell'更改为我的列表中的另一个股票代码,如'Goog'?我想循环为我的所有股票代码做(2)。

2 个答案:

答案 0 :(得分:3)

  1. 要更改数据集中的日期格式,循环浏览所有观察结果可能不是一个好主意。标准语法是 -

    proc数据集库= your_libname nolist;   修改dataset_name;     format variable_name format_name; 退出;

  2. 鉴于modify语句不包含多个SAS文件,您必须将其包装在一个宏中以存储所有24个文件

    %macro modformats();
    proc datasets library = <your libname> nolist;
      %do i = 1 %to 24;
      modify file&i;
        format <variable name> <format name>;
      %end;
    quit;
    %mend modformats;
    
    1. 要提取和附加所有“戴尔”相关数据,最好使用视图。
    2. 例如,您首先定义一个视图(请注意,此处没有创建名为'all_files'的物理数据集) -

      data all_files / view = all_files;
        set file1 file2... file24;
      run;
      

      然后你可以写 -

      data dell;
        set all_files;
        where ticker = 'DELL';
      run;
      

答案 1 :(得分:2)

这是解决方案的原型。我不知道你是否需要做很多符号修改。将根据请求修改代码。没有测试,它应该工作。

%macro test();
%do i=1 %to 24;
  data file&i;
   set file&i;
    format date [dateformat];  /*replace with the format you want */ 

proc append base=unions data=file&i(where=(stock_symbol='Dell'));

data unions;
 set unions;
  stock_symbol='Goog';

%end;
%mend;

%test(); run;
相关问题