2
votes

I'm trying to import a .CSV file into a SAS dataset, and am having some trouble. Here's a line of sample input:

Foo,5,10,3.5
Bar,2,3,1.0

The problem I'm having is that the line-final "3.5" and "1.0" are not being correctly interpreted as variable values (instead SAS complains that they are invalid values, giving me a NOTE: Invalid data for VARIABLE error). However, when I add a comma to the end of the line, like so:

Foo,5,10,3.5,
Bar,2,3,1.0,

Then everything works fine. Is there a way that I can make this import work without modifying the source file?

Currently, my DATA step's INFILE statement has the DSD, DLM=',', and MISSOVER options.

2
If you post your code we can see if there are any other problems you are missing...Jay Corbett

2 Answers

3
votes

With this data in a .csv file in a windows environment

Foo,5,10,1.5
Bar,2,3,2.1
Foo,5,10,3.5
Bar,2,3,4.1

This code works (running SAS locally on a windows machine)

filename f 'D:\Data\SAS\input.csv';
data input;
 infile f delimiter=','; 
 input char1 $ num1 num2 num3;
Run;

As @itzy mentioned, the environment is important..more info will help with the solution

When you are working with data from a different environment, you can use the TERMSTR option on the INFILE statement to tell SAS how the lines of data are terminated.

2
votes

This most likely has to do with the different codes for line endings in Unix and Windows. I'm guessing your data comes from a different operating system than the one you're running SAS on.

The solution is to change the newline codes to the correct operating system. If you're running SAS on a unix system, try the dos2unix command. If you're running Windows, you can edit the CSV file with a text editor like UltraEdit or Notepad++ and save the file in Windows format.