data cars93subtab; infile '/folders/myfolders/cars93subtab.txt' dlm=',' termstr=CRLF firstobs=2; input Manufacturer $ Type $ Price MPG_city MPG_highway Horsepower Origin $; run; proc print data=cars93subtab; run;
My output was only two rows and mainly missing data.
The log showed that "invalid data for ___ in line ___
and provided "n=option"
Can anybody tell me how to set up the length of variables?
I guess the reason was the setting of length of variables.
Thank you!
You specify the length ahead of the INPUT statement. The default length is 8.
data cars93subtab; infile '/folders/myfolders/cars93subtab.txt' dlm=',' termstr=CRLF firstobs=2;
length manufacturer $30 Type $20; input Manufacturer $ Type $ Price MPG_city MPG_highway Horsepower Origin $;
A quick way to cheat to import your data is to use PROC IMPORT and then borrow the code from the log and adjust it. Or at least use it as a reference to determine how to set up your Formats/Informats and Lengths.
file cars '/folders/myfolders/cars93subtab.txt' termstr=crlf;
proc import out=cars93subtab datafile=cars dbms=csv replace;
getnames=yes;
guessingrows=max;
run;
wow, there are many ways to write a code for the same question.
data cars93subtab; infile '/folders/myfolders/cars93subtab.txt' termstr=CRLF firstobs=2; length Manufacturer $10; input Manufacturer $ Type $ Price MPG_city MPG_highway Horsepower Origin $; run; proc print data=cars93subtab; run;
when I checked the data in a text editor, I found there are many spaces between each variables,
I checked the text book, in this way, list input is not appropriate.
I used the above code to run it again, still did not work.
I shall use your code but I have to review my text book again.
Thank you:)
Yeah, I just looked at your text file. Your code as DLM=',' but there's no comma's in the file
One of the essential tools for importing text data is an editor that can display hex codes (ie notepad++). Using this, it is easy to see that your text file is tab-delimited, so you should approach it like that:
data cars93subtab;
infile '/folders/myfolders/cars93subtab.txt' dlm='09'x termstr=CRLF firstobs=2;
input Manufacturer :$10. Type :$10. Price MPG_city MPG_highway Horsepower Origin $;
run;
proc print data=cars93subtab;
run;
Adjust lengths as needed.
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.
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.