Hi everyone,
I am new to SAS programming and I am trying to read multiple CSV files into SAS. I have tried using the infile statement with a wildcard to read all the files in a folder, but it doesn't seem to be working. Can someone please guide me on how to read multiple CSV files in SAS?
Here's the code I am using:
filename files "/path/to/folder/*.csv"; data mydata; infile files dlm=',' firstobs=2; input var1 var2 var3; run;
I am not getting any errors, but it seems like only the first file is being read. I have searched online for a solution but haven't been able to find one that works for me. Any help would be greatly appreciated.
Thank you!
Your code will only skip the header line on the FIRST file it finds.
To skip all of the headers use the FILENAME= option of the INFILE statement to create a variable to contain the name of the current file so you can skip the header line of each file.
data mydata;
length filen $256;
infile "/path/to/folder/*.csv" dsd truncover filename=filen;
input @@;
if filen ne lag(filen) then delete;
input var1 var2 var3;
run;
The variable created by the FILENAME= option is not output. So if you want to keep the name of the file that contributed the current observation then add another variable. This code will also add a simple numeric variable to count how many files have been read.
data mydata;
length filen $256;
infile "/path/to/folder/*.csv" dsd truncover filename=filen;
input @@;
if filen ne lag(filen) then do;
fileno+1;
delete;
end;
input var1 var2 var3;
filename=filen;
run;
Your code will only skip the header line on the FIRST file it finds.
To skip all of the headers use the FILENAME= option of the INFILE statement to create a variable to contain the name of the current file so you can skip the header line of each file.
data mydata;
length filen $256;
infile "/path/to/folder/*.csv" dsd truncover filename=filen;
input @@;
if filen ne lag(filen) then delete;
input var1 var2 var3;
run;
The variable created by the FILENAME= option is not output. So if you want to keep the name of the file that contributed the current observation then add another variable. This code will also add a simple numeric variable to count how many files have been read.
data mydata;
length filen $256;
infile "/path/to/folder/*.csv" dsd truncover filename=filen;
input @@;
if filen ne lag(filen) then do;
fileno+1;
delete;
end;
input var1 var2 var3;
filename=filen;
run;
Since a file reference can also be used for output, wildcards don't make sense there.
Do everything in the INFILE:
data mydata;
infile "/path/to/folder/*.csv" dlm=',' firstobs=2;
input var1 var2 var3;
run;
You may need to add code to skip the header line in every new file.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.