BookmarkSubscribeRSS Feed
jc3992
Pyrite | Level 9
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!

4 REPLIES 4
Reeza
Super User

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;
jc3992
Pyrite | Level 9

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:)

Reeza
Super User

Yeah, I just looked at your text file. Your code as DLM=',' but there's no comma's in the file

Kurt_Bremser
Super User

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.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

SAS Enterprise Guide vs. SAS Studio

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 959 views
  • 0 likes
  • 3 in conversation