SAS: creating multiples files from multiple data sets

落花浮王杯 提交于 2020-01-16 00:54:08

问题


I have 24 datasets that are structured in the same way. By that I mean the same column headers (time, date, price, stock symbol), data set structure etc. I don't wish to append all 24 files since one data set is to big to handle. I named all my data sets with the name "file1 file2 file3 file4....up to file24".

What I want to do is the following:

  1. For example change the date format in all of my 24 files at once;

  2. Be able to extract from each file# a specific stock symbol like 'Dell' and append all the extracted 'Dell' data;

  3. and finally, how can I create a loop that that allows me to change the stock symbol from 'Dell' to another stock symbol in my list like 'Goog'? I would like that loop to do (2) for all of my stock symbols.


回答1:


This is a prototype of solution. I don't know whether you need to do many symbol changes. Will modify the code upon request. Haven't tested, it should work though.

%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;



回答2:


  1. To change the date format in a dataset, it might be a bad idea to loop through all the observations. The standard syntax is -

    proc datasets library = your_libname nolist; modify dataset_name; format variable_name format_name; quit;

Given that the modify statement does not take multiple SAS files, you will have to wrap it in a macro for all 24 files

%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. To extract and append all 'Dell' related data, it is best to use views.

For example, you first define a view (note that there is no physical dataset called 'all_files' created here) -

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

and you can then write -

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


来源:https://stackoverflow.com/questions/10959678/sas-creating-multiples-files-from-multiple-data-sets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!