I have a load of CSV files in with the same layout with the exception of the termstr. Some have CR, the others CRLF.
Is there an easy way to import all of these? This is my current code:
filename tsvs "/Path/To/Files/*.txt";
data test;
infile tsvs filename=tsvfile dlm=',' dsd missover TERMSTR=CR;
input code:$5. @;
if code='P10' then
do;
fName=scan(scan(tsvfile,-1,"/"),1,".");
input @7 a b c$ d$ e$ f$ g;
output test;
end;
run;
This only works for the CR ones. I can then have another code that has CRLF as the termstr and append them together, but is there a way to do it together? Or get SAS to clean the CSVs first?
thanks
Run an intermediate step first:
data _null_;
infile "your_filepath.txt" recfm=n;
file "your_filepath.crlf" recfm=n;
input inchar $char1.;
if inchar ne '0a'x
then do;
put inchar %char1.;
if inchar = '0d'x then put '0a'x;
end;
run;
So all your files have CRLF termination strings; put this into a macro and run it on all your files with CALL EXECUTE from a data step that reads the directory. Then run your import data step from the intermediate files.
Run an intermediate step first:
data _null_;
infile "your_filepath.txt" recfm=n;
file "your_filepath.crlf" recfm=n;
input inchar $char1.;
if inchar ne '0a'x
then do;
put inchar %char1.;
if inchar = '0d'x then put '0a'x;
end;
run;
So all your files have CRLF termination strings; put this into a macro and run it on all your files with CALL EXECUTE from a data step that reads the directory. Then run your import data step from the intermediate files.
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.