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

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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