BookmarkSubscribeRSS Feed
tysonsri
Calcite | Level 5

Hello All, I am using SAS 9.4.

 

I have a 101 CSV files in the directory. I want to combine all csv files into one single CSV file /one dataset .

 

Everyday one csv file will add into the same directory and I need to combine all csv files into one single csv file again.

 

Can you please help me with the sas code

4 REPLIES 4
ballardw
Super User

Do want a SAS data set or a CSV file?

A data step can read multiple CSV files into a single SAS data set IF the CSV files all have the same structure.

tysonsri
Calcite | Level 5
I want a single sas dataset.
All 101 csv files are in same structure.

How do we do that in a sas?
comibe all 101 .csv files to one data set
Reeza
Super User

Do you have code to read a single CSV file? As a data step?

When you combine all the data do you need to add any information about which file it came from?

 

Here's a rough idea on how to read all files into a single SAS data set in one step:

https://communities.sas.com/t5/SAS-Communities-Library/How-do-I-write-a-macro-to-import-multiple-tex...

Tom
Super User Tom
Super User

If all of the CSV files have the same structure and are either all of the CSV files in that folder or all that follow a simple pattern then just use a single data step with a wildcard, * , in the filename.

 

So something like this:

data want;
    length fname $256;
    infile "\\my directory\*.csv" dsd truncover filename=fname;
    input @;
    if fname ne lag(fname) then delete;
    length firstvar 8 secondvar $40 lastvar 8;
    informat lastvar date.;
    format lastvar date9.;
    input firstvar -- lastvar;
run;

 

If the files are really large you might need to work harder by keeping track of which files you already read so you know which ones are new,  but if the files are not that big then it is probably just easier to read the all each time.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

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
  • 4 replies
  • 1030 views
  • 1 like
  • 4 in conversation