next up previous
Next: Looping Up: SAS Previous: Exporting a SAS dataset

Reading data into SAS from an arbitrary set of files

Suppose you have a set of data files that you need to read into SAS, but there is no systematic pattern to the names. Here I describe two ways to do this. The second is some text I put together as bits and pieces. The first is a more elegant solution I found somewhere, but have yet to try.

  1. Use the following SAS code, where the 'SAS statements' part somehow opens each file and reads in the contents. I believe you would want to make use of the dread function as part of that.

    do i=1 to dnum(dirid); 
    ...SAS statements... 

  2. First create a text file with the file names, one per row. For example, the Unix command 'ls >filenames.txt' would do the trick if all the files (and no others) were in the current directory. Then use SAS code such as follows. Here I just read in the data in the files and concatenate it all into the dataset 'full'.

    FILENAME fileref 'filenames.txt'; /* 'fileref' must be 8 chars or less */ 
    data filenames;  
      length var1 $200;  
      infile fileref;  
      input var1;  
    data _null_; 
      set filenames nobs=nobs;  
      call symput('out'||left(_n_), var1);  
    %macro doit;  
    %do i=1 %to 248; /* 248 needs to be the number of filenames from filenames.txt that you want to read in */ 
    %let docname=&&out&i; 
    filename mydata pipe cat &docname | tr -d '\r' ;  
    data subfile;  
      infile mydata lrecl=500 truncover dsd;  
      informat x y;  
      input x y; 
    run ; 
    proc append base=full data=subfile2 force; 
    %PUT &docname; 
    %end; %mend doit; 
    %doit; run;

next up previous
Next: Looping Up: SAS Previous: Exporting a SAS dataset
Chris Paciorek 2012-01-21