EZ Study

Actuarial Biology Chemistry Economics Calculators Confucius Engineer
Physics
C.S.


SAS programming Skills
SAS Tip: Read Multiple Delimited Files by FILENAME

Question: How can I read multiple delimited files into one single combined SAS dataset? Assuming all the files have the similar layout.

Answer: We can use FILENAME statement to read multiple delimited files ;
so in case if you have to read more than 100 raw files with the same layout, then you can use one IMPORT or infile statement to read them all, no need to use 100 IMPORT statements.

Example: Here is an SAS code example to read 100 tab delimited files, you can simply copy and paste the code, remember to change your file directory to run, it's been tested on SAS 9.1.3 and SAS 9.2:

        /***************************************************/
        /**** Apply FILENAME to define a selection of multiple files ****/
        /***************************************************/
We can use
        FILENAME myfiles
        (  "directory\filename1.tab",
            "directory\filename2.tab",
            ... ,
            "directory\filename100.tab"  );

You can use excel to help generate the above codes automatically.

        /****************************************************/
        /*** Read the files by import statement, notice the datafile=... ***/
        /****************************************************/
        proc import datafile=myfiles
               out=dataout
               dbms=dlm
               replace;
               delimiter='09'x;  /*use hexidecimal value '09'x for tab delimitor */
               datarow=2;         /* get data from 2nd row, 1st row for variable */
               run;

Another way of loading so many files is to use macro to read the file one by one, and append each other thereafter.

Related links:

Continue to next: How can I read external files from FTP into SAS? Symputx?   SAS tutorial home
Back to: How to read different kinds of files(csv,excel,) into SAS?   Statistics tutorial home