SAS: creating multiples files from multiple data s

2019-08-09 16:22发布

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.

2条回答
We Are One
2楼-- · 2019-08-09 16:26
  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;
查看更多
啃猪蹄的小仙女
3楼-- · 2019-08-09 16:35

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;
查看更多
登录 后发表回答