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.
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!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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: