I'm trying to import a .txt file that has over 700 observations.
This is what it looks like: https://imgur.com/a/80EXybk
This is my code: https://imgur.com/a/ZKLnKWs
And this is the log file : https://imgur.com/a/ZKLnKWs
Could someone help identify the problem and help me with a solution?
From the LOG notes it looks like SAS thinks the file is just one line. It also is not seeing the break between the fields.
Try adding these options to the INFILE statement:
expandtabs truncover termstr=cr
Don't post pictures of text, post the text itself in a {i} window, or attach the txt file to your post.
Sorry. Will do for future reference.
From the LOG notes it looks like SAS thinks the file is just one line. It also is not seeing the break between the fields.
Try adding these options to the INFILE statement:
expandtabs truncover termstr=cr
Thank you so much!
I'm a new student to SAS.
How would you even know how to use that code? I understand TRUCOVER, but the other things, i'm not sure how you would use that.
In a real world situation, if you get a text file, would it just be easier to convert it to csv or excel?
In the real world text files are easier to deal with than Excel files. Excel is a spreadsheet program and so users can put anything into any cell, which makes it not very suitable for reading in a system like SAS that wants a dataset. With a text file you can have complete control over how to handle anything stupid that users might put into a field.
A number of editors will replace spaces with tabs to save characters. Some users enter tabs in hopes it will make the columns of data line up so that the file is easier to read for humans. The EXPANDTABS option will replace the tabs with enough spaces so that it lines up to the next tab stop. It assumes tab stops are set at every 8 characters.
You might also consider whether the file is actually intended to be a delimited file with tab as the delimiter. In that case you might want to use the DSD and DLM= options on the INFILE statement.
dsd dlm='09'x
If you have an actual value for every field then it doesn't matter which way you read the file, but if you have missing values that are represented by spaces (or nothing at all between the delimiters) then using the DSD and DLM= option will work better. Although if the tabs were just there to replace multiple spaces and when they are replaced the values appear in fixed locations on the line then you can read the data using fixed length formats or column positions and the missing values will be read properly.
A CSV file is just a delimited file with comma as the delimiter. So not really any easier or harder to read than a file that is using a tab for a delimiter.
The TERMSTR= option was suggested because your data step was treating the file as if it only had one line. SAS will normally automatically read files that use either LF alone (as Unix does) or CRLF (as Windows does). Although it might on Unix treat the extra CR at the end of the line as part of the data of the line. So when it doesn't see the line breaks at all it is normally because some idiot program used CR only as the line break. The biggest idiot these days is Excel on Mac. For some reason they didn't hear that MAC OS is now a Unix platform and they still generate text files with CR only as end of line. You can force it to make proper files by being careful what file format you select when creating the file.
Thank you for taking the time to explain that to me.
Tom, you the man!
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.
Find more tutorials on the SAS Users YouTube channel.