I am trying to import .txt files (I have attached the files below). Here is the code I wrote:
proc import datafile='/home/u58841625/Cert/class.txt' dbms=tab out=class
replace;
delimiter='09'x;
run;
proc import datafile="/home/u58841625/Cert/state_data.txt" out=work.state
dbms=tab replace;
delimiter='09'x;
run;
The output tables are expected to have 3 and 4 columns respectively but there is only a single column in both tables. (All the columns are merged into one column. Images attached below). Can anyone please explain to me what is the mistake in my code?
Thanks in advance.
Open both text files with a good text editor (e.g. Notepad++) which can display special characters and/or has a hex display mode, before uploading, to verify which delimiter is used.
Thank you for your reoly. I have opened both files in notepad++ and there were no special characters.
Thank you for your reply. I have tried this code before. It is partially working. Please look at the images below I am getting so many unwanted columns with missing values. Is there a solution to get rid of those unwanted columns??
Try Tom's code a.k.a import txt file via data step!
There is no need to "import" a text file. Just READ the text file.
It looks like your files do NOT have any tabs in them.
The CLASS file seems to be using multiple spaces so the columns will align when printed/viewed with a fixed width font.
Name Gender Age Joyce F 11 Thomas M 11 Jane F 12 Louise F 12
So just read them "normally" and see if that works.
data class ;
infile '/home/u58841625/Cert/class.txt' firstobs=2 truncover ;
input name :$20. gender :$1. age ;
run;
The other file
Region State Capital Bird South Georgia Atlanta 'Brown Thrasher' South 'North Carolina' Raleigh Cardinal North Connecticut Hartford Robin West Washington Olympia 'American Goldfinch' Midwest Illinois Springfield Cardinal
appears to have a single space between the values and quotes around values that contain spaces. So use the DSD option with space as the delimiter.
data state;
infile '/home/u58841625/Cert/state_data.txt' dsd dlm=' ' firstobs=2 truncover;
input Region :$10. State :$30. Capital :$30. Bird :$40. ;
run;
Thank you. Your solution worked. But I am still trying to find out how to import those files.
@Tom gave you working code for both files. Where is the issue?
I am a student and learning SAS by SAS Certified Specialist preparation guide. I am practicing all the examples given in the guide and only these two import procedures failed. (Reference: SAS Certified Specialist preparation guide, pg: 41, Importing a Tab-Delimited File).
Example code which they gave is:
proc import datafile='C:\Users\Student1\cert\class.txt'
dbms=tab
out=class
replace;
delimiter='09'x;
run;
I am not able to understand why the code is not working.
Whenever code gives you issues, please post the complete log of a failing step by copy/pasting it directly from your SAS interface (SAS Studio, Enterprise Guide) into a window opened with this button:
As already mentioned, your files as posted do not use tabs, but white space. Maybe the original tabs were replaced by blanks during the whole transport operation.
Next, I suspect that you do not have SAS installed on your desktop or on any Windows system, instead using SAS On Demand. ODA runs on a UNIX platform, you have to upload the files first and then use the UNIX pathname to address them. Retrieve the correct name by right-clicking on the file after the upload and copying it from the Properties.
Thank you. Yes, I am using the SAS On-Demand (SAS studio not installed on my computer). I am using windows not UNIX.
I have checked my log there are no errors but some notes are as follows:
You ARE using UNIX, SAS On Demand runs solely on Linux, which is a UNIX variant.
And you still try to use tabs as delimiters; we have told you several times by now that your files do not contain tabs.
Run @Tom's code.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
Ready to level-up your skills? Choose your own adventure.