Looping Over Files in SAS

Get a list of all the files and folders from a single directory into a SAS data set, in one hit. Really useful for iterating over later.

Real quick one to get a directory listing into SAS, if you want to know more about how it works, check out this.

Specify the directory to list using the location macro variable.

The code

%Let location = C:\Program Files; *no need to wrap this in quotes;

FileName myDir Pipe "dir ""&location"" /b"; *gaps in path handled by double-double quote here;

Data listing;
    InFile myDir Length=lineLength;
    Input file $varying512. lineLength;
Run;

Looping over the Directory

Use the dynamic loop from here to loop over your directory contents. If you’ve got a directory of flat files that all need to go through a Proc Import, they can now be named absolutely anything and it won’t matter in your code!