I've three different CSV files of same structure. Appreciate if someone tell me how to merge all those files into one CSV file via SAS program.
If you tweak my code very slightly, you preserve the first header line:
filename have ("$HOME/sas/customer1.csv","$HOME/sas/customer2.csv","$HOME/sas/customer3.csv");
filename want "$HOME/sas/customer.csv";
data _null_;
infile have;
file want;
input;
if (substr(_infile_,1,4)) ne 'Name' or _n_ = 1;
put _infile_;
run;
The answers is within the Programming 1 course.
I assume you mean concatenate,not merge.
You're right, need to concatenate
You can combine multiple external files into one fileref:
filename inpfile ("filename1","filename2","filename3");
Thank you for the reply. However once again I need little help to accomplish this task.When I tried the following code, headers of each file is getting concatenated in the final output (want) file. I just wanted to combine values of each file with one header as headers are same across the file.
filename have ("/sas/customer1.csv", "/sas/customer2.csv", "/sas/customer3.csv");
filename want '/sas/customer.csv';
data _null_;
file want
infile have
input;
put _infile_;
run;
You need to identify (with a proper substring) the header records and omit them with a subsetting if.
I couldn't understand. Appriciate if you could provide some outline in terms of program to achieve this.
Assume I've the output file as follows.
Name | Age |
James | 34 |
Name | Age |
Sachin | 51 |
I'm looking for output like,
Name | Age |
James | 34 |
Sachin | 51 |
Thanks in advance for any inputs you offer me.
How do I write a macro to...impor
The second example explicitly deals with multiple files, that have multiple headers.
Read through it 🙂
You want to skip reading the header row. This can be done in a data step with the EOV option and some additional coding.
A very rough skeleton of the program would look like this:
data want;
infile "C:\Path\*.csv" delimiter=',' DSD lrecl=32767 firstobs=2 eov=skip missover;
/*anything else you may want befor the input such as Informat statements*/
input @; /*this reads in the "current" line and holds. This sets the value
of the EOV (End Of file Variable) */
if Skip then do;
skip = 0;
end;
else do;
input
/* input variable list*/
;
/* any other code you may need*/
output; /* explicitly write to the output set*/
end;
run;
The resetting of the variable Skip, my name nothing magical, is required otherwise you can end up in an infinite loop.
The example requires you to put the variable names and such code.
Just add
if substr(_infile_,1,4) ne 'Name';
before the
put _infile_;
Still I'm not getting the desired output. There is no change in the output file after adding the if clause before put statement. My code is
filename have ("/sas/customer1.csv", "/sas/customer2.csv", "/sas/customer3.csv");
filename want '/sas/customer.csv';
data _null_;
file want
infile have
input;
if substr(_infile_,1,4) ne 'Name';
put _infile_;
run;
You're not inputting to a variable.
I would suggest you work through my example step by step but utimately it's your choice.
The code you've posted will read the 3 .csv from source and write all the records except the ones having "Name" at the beginning of a line to a single output file named customer.csv
Your code as such will work IF you add semicolons after "file want" and "infile have"....
It is still not working after adding the semicolons in file and infile statements
Define "not working".
If you get errors,
SHOW.THE.LOG.
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.