BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
darrenhoggard
Calcite | Level 5

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

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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.

View solution in original post

1 REPLY 1
Kurt_Bremser
Super User

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.

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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
  • 1 reply
  • 480 views
  • 2 likes
  • 2 in conversation