change CSV to ASCII

Reply
Contributor
Posts: 74

change CSV to ASCII

Is there a way to change  a csv file to read in as ASCII?  I have been having problems all day evaluating text strings and it turns out the problem was the csv file.  When I convert it to ASCII all the logic worked.  So I wanted to know if the csv file can be converted in the infile data step so it reads in correctly.

Thanks,

Super User
Posts: 11,129

Re: change CSV to ASCII

If you read the CSV file with a data step you may be able to address this with an Encoding='encoding value' option on the infile or as a dataset option.

But without more specific information we're kind of shooting in the dark.

Super User
Super User
Posts: 6,845

Re: change CSV to ASCII

What do mean by converting to ASCII.  Most of the CSV files that I read are in ASCII as I haven't used EBCDIC in years.

Contributor
Posts: 74

Re: change CSV to ASCII

More detail:  I am importing data from a csv file stored on a unix server.  I read the data in with an infile statement.  There are 3 variables, location$6. infono$12.  and listname $50.   The file appears to import correctly but when I use my error logic to compare the listname variable which contains variable length character values to a hard coded list I have in my program using a %LET todaylist = ('list1_file','list2_file','file_another');  all fail the compare.  I ftp'ed the csv to my desktop and then back up to unix and using a transfer protocal of ascii.  Read in that csv file then the compares worked.  So SAS is not actually "seeing" the values in the listname field in the csv file.  How do I get SAS to read the file correctly so this does not happen?  I am writing an automated report and cannot download/upload the file daily.  I have to use the csv copy provided on the server.

Thanks,

Super User
Super User
Posts: 6,845

Re: change CSV to ASCII

The only change that could have happened by using FTP to move a file to and from DOS using ASCII setting is that the extra Carriage Return character that DOS uses to mark the end of line would be removed.  Since the variable LISTNAME is the last one on the line it is possible your original program was reading the CR as part of the data. 

Here are some ways to modify your INFILE statement to handle that.

1) If you know for sure that you file is using CR+LF as the end of line markers then add TERMSTR=CRLF option to your INFILE statement.

2) You can change your delimiters from just comma to comma or CR.

3) You can compress the CR character out of the variable LISTNAME.

data want ;

  infile 'myfile.csv' dsd firstobs=2 termstr=crlf truncover ;

  length location $6 infono $12 listname $50 ;

  input location infono listname ;

run;

Super User
Posts: 7,431

Re: change CSV to ASCII

Open the files on Unix with a simple editor (vi). If you see ^M at the end of each line, you have the DOS CRLFs there. Use the TERMSTRING option or make sure that the CR characters are removed during filetransfer.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Ask a Question
Discussion stats
  • 5 replies
  • 247 views
  • 0 likes
  • 4 in conversation