BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Satish_Parida
Lapis Lazuli | Level 10

Hi All,

 

I am trying to read a utf-8 encoded csv file.

In Notepad++ the file looks fine.

In notepad the whole file comes as 1 line.

I am using a UNIX OS, where SAS 9.4 is installed.

 

So when SAS reads the file, it is only reading 1 record. As I am using firstobs=2 to avoid reading header, the final dataset contains 0 records.

 

I am using the following code.

 

filename file1 '/u04/dataloader/Satish/App/ZZ/f1.csv' encoding='utf-8';

data xxx.asdf;
infile file1 dsd dlm=',' firstobs=2 encoding='utf-8';
input SiteNumber:$10. LabName:$30. AnalyteName:$16. FromDate:$20. ToDate:$20. 
      LowRange:best12. HighRange:best12. Units:$10. Dictionary:$10. Comments$10.;
run;

I am trying to read the data from line2.

 

Thanks in advance.

1 ACCEPTED SOLUTION

Accepted Solutions
GinaRepole
SAS Employee

Oh, I see! If everything about your SAS session is Unix centralized, you should keep it that way. Just add the TERMSTR=LR option onto the INFILE statement and it should read the existing file correctly (or TERMSTR=CR if you may be missing carriage returns).

View solution in original post

7 REPLIES 7
GinaRepole
SAS Employee
It sounds to me like the CSV file you're working with has Unix based line endings instead of Windows based line endings, so the records are not being properly split.

I was able to resolve this issue by quickly opening the file in WordPad (instead of Notepad) and saving it under a different name. It automatically corrected the line breaks.
Satish_Parida
Lapis Lazuli | Level 10

@GinaRepole 

Can it be done in bash command, or using any utility in base SAS?
I am trying to automate a batch process. I am using a UNIX OS, where SAS 9.4 is installed.
This is one example file.

GinaRepole
SAS Employee

Oh, I see! If everything about your SAS session is Unix centralized, you should keep it that way. Just add the TERMSTR=LR option onto the INFILE statement and it should read the existing file correctly (or TERMSTR=CR if you may be missing carriage returns).

Satish_Parida
Lapis Lazuli | Level 10

@GinaRepole

 

Thank you very much. Robot Happy

 

I was missing the carriage return. 

 

 

filename file1 '/u04/dataloader/Satish/App/ZZ/f1.csv' encoding='utf-8';

data xxx.asdf;
infile file1 dlm=',' dsd firstobs=2 encoding='utf-8' TERMSTR=CR; /*CR Worked as carriage return*/
input SiteNumber:$10. LabName:$30. AnalyteName:$16. FromDate:$20. ToDate:$20. 
      LowRange:best12. HighRange:best12. Units:$10. Dictionary:$10. Comments$10.;
run;

 

 

Thank you very much

 

 

GinaRepole
SAS Employee
Cheers 🙂

I adjusted my prior post to mention carriage returns as well in case anyone comes by to reference this in the future and only reads the solution post.
Tom
Super User Tom
Super User

@Satish_Parida wrote:

@GinaRepole

 

Thank you very much. Robot Happy

 

I was missing the carriage return. 

 

 

filename file1 '/u04/dataloader/Satish/App/ZZ/f1.csv' encoding='utf-8';

data xxx.asdf;
infile file1 dlm=',' dsd firstobs=2 encoding='utf-8' TERMSTR=CR; /*CR Worked as carriage return*/
input SiteNumber:$10. LabName:$30. AnalyteName:$16. FromDate:$20. ToDate:$20. 
      LowRange:best12. HighRange:best12. Units:$10. Dictionary:$10. Comments$10.;
run;

 

 

Thank you very much

 

 


Note: As far as I can tell only Microsoft Excel on Mac is still creating files using CR only as the end of line marks.  But you can pick a different file format when you save the files from Excel so that it will generate files using LF or CR+LF instead.  

Satish_Parida
Lapis Lazuli | Level 10

If the source of the file is different and we can not predict the line breaks

Can we use more than one TERMSTR option in one data step. like LF+CR

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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
  • 7 replies
  • 13090 views
  • 2 likes
  • 3 in conversation