1

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

我想要做的是以下内容:

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

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

  3. 最后,如何创建一个循环,使我可以将股票代码从“戴尔”更改为列表中的另一个股票代码,例如“Goog”?我希望该循环为我的所有股票代码执行 (2)。

4

2 回答 2

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

    proc datasets library = your_libname nolist; 修改数据集名称;格式变量名格式名;退出;

鉴于 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. 要提取和附加所有“戴尔”相关数据,最好使用视图。

例如,您首先定义一个视图(注意这里没有创建名为“all_files”的物理数据集)-

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

然后你可以写 -

data dell;
  set all_files;
  where ticker = 'DELL';
run;
于 2012-06-09T14:49:34.913 回答
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;
于 2012-06-09T12:10:09.170 回答