02-18-2013 11:37 PM
I am new to SAS and I need to import several .csv files simultaneously into sas. I started the procedure using proc import but I have too many files to import then one by one. Here is what I did:
PROC IMPORT OUT= WORK.INSTOWN2000Q1
DATAFILE= "F:\SAS Institutional Ownership\46_q1_2000.csv"
PROC IMPORT OUT= WORK.INSTOWN2000Q2
DATAFILE= "F:\SAS Institutional Ownership\45_q2_2000.csv"
PROC IMPORT OUT= WORK.INSTOWN2000Q4
DATAFILE= "F:\SAS Institutional Ownership\43_q4_2000.csv"
PROC IMPORT OUT= WORK.INSTOWN2000Q3
DATAFILE= "F:\SAS Institutional Ownership\44_q3_2000.csv"
set WORK.INSTOWN2000Q1 WORK.INSTOWN2000Q2 WORK.INSTOWN2000Q3 WORK.INSTOWN2000Q4;
if shrout3=0 then delete;
if stkcdesc= 'COM'or stkcdesc='CMA' or stkcdesc='CMB' or stkcdesc='CMC' or stkcdesc='' ;
I have many more files in my "F:\SAS Institutional Ownership/ folder.
Thank you for your support
02-19-2013 02:29 AM
As I know Till Now SAS didn't develop a technique to import several CSV files at once. but, you can use macro program to do that i.e.
%Macro ReadCSV (infile , outfile );
PROC IMPORT OUT= &ofile
%ReadCSV("F:\SAS Institutional Ownership\46_q1_2000.csv", work.ofile1);
%ReadCSV( "F:\SAS Institutional Ownership\45_q2_2000.csv", work.ofile);
and so on for other files
also there is other advanced technique for reading all files from one folder at once - using macros also.
05-17-2015 12:13 PM
Since my indexing loop skills are sketchy at best, I like this method. I just build my macro variables with a few keystrokes in a spreadsheet, copy and paste them into the macro you kindly provided, and (BAM!), 40, 80, 200 or so flat files imported in seconds. No errors.
Thanks so much!
05-18-2015 08:27 AM
By indexing/looping, I referenced other macros I had worked with and/or tried to build, not this macro. In my comment I was trying to be emphatic that the reason why I liked the macro supplied by MS_Egyptian was the lack of such a feature.
02-19-2013 06:57 AM
If the files all have the same fields in the same order you can used the INPUT statement generated with a PROC IMPORT and use a wild card in the FILENAME. You will also want to learn about the INFILE statement options EOV and FILENAME.
length filename csvname $256;
infile "F:\SAS Institutional Ownership\*.csv" eov=eov filename=filename lrecl=?;
if _n_ eq 1 or eov then do;
csvname = filename;
other statements possibly
02-19-2013 10:51 AM
All things considered, if all of your input files whereabout information is already in a table or a flat file or can be put into those files conveniently using OS commands, "Filevar=" in "Infile" statement will be another option.
05-17-2015 05:04 PM
You do not want to be using PROC IMPORT for this project. It will need to guess at what the columns represent. You are better off writing a data step to read the CSV files. You could recall the program the PROC IMPORT generated as a starting point if you want.