BookmarkSubscribeRSS Feed
MILKYLOVE
Calcite | Level 5

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?

 

 

3 REPLIES 3
andreas_lds
Jade | Level 19

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 ...

MILKYLOVE
Calcite | Level 5

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

 

 

 

PaigeMiller
Diamond | Level 26

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.

--
Paige Miller

sas-innovate-white.png

Missed SAS Innovate in Orlando?

Catch the best of SAS Innovate 2025 — anytime, anywhere. Stream powerful keynotes, real-world demos, and game-changing insights from the world’s leading data and AI minds.

 

Register now

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1114 views
  • 0 likes
  • 3 in conversation