Hello,
I am new to SAS and I have an issue:
I have a directory (C:/mydir/myfiles) where I have many csv files (the number is not constant).
I have to import these files and concatenate them (in no particular order), to get a final csv file. Note that the csv files have identical columns, data types etc.
Example:
myfile_V1.csv
myfile_V2.csv
myfile_V3.csv
myfile_V4.csv
myfile_V5.csv
....
The final concatenated file should be --> myfile_complete.csv
Any idea how do I proceed with this please in a way that I won't have to manually put the file names every time?
Please have a look at https://stackoverflow.com/questions/60244/is-there-replacement-for-cat-on-windows
Or do you need the data in sas? If this is the case, please find of the numerous threads dealing with reading multiple files.Hint the infile-statement allows something like C:/mydir/myfiles/*.csv ...
I am using Unix, the directory name was just an example. I guess it caused some confusions.
My issues is, I don't know how to concatenate all these files into one single file without having to manually input the name of each file every time. (maybe a loop?)
For a first step, I have tried to read all the files in the directory and list them:
filename myfiles '/sasapp/Liv_Marketing/delsquad/bloc/in'; data filenames (keep=filename); dir= dopen('myfiles'); do file = 1 to dnum(dir); filename = dread(dir,file); output; end; rc= dclose(dir); run;
This gives me a list of all the files present in the directory.
Now I need to import all of them and concatenate them into one output file.
Thanks for your help
Please pay attention to the hint from @andreas_lds , you don't need to specifically name each .csv file in your code. You don't even have to read the list of names in the folder.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.