DATA Step, Macro, Functions and more

Trying to read in a two variable file using INFILE but can't get it to work...

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 16
Accepted Solution

Trying to read in a two variable file using INFILE but can't get it to work...

Hello: Very new to SAS here. Tried searching the support community already but I'm on information overload. I'm trying to read in the attached text tab delimited file as a data set in SAS. The first field should be read in as a date, second as a number with one decimal place. Sample file attached. The code I'm running is not working. This is the code I'm trying to use...

 

data SAData;
	infile 'D:\qsilver\Sas Data\NSAPur.txt' delimiter='	';
	input
	datefield mmddyy10.
	purindex
	;
run;

Accepted Solutions
Solution
2 weeks ago
PROC Star
Posts: 172

Re: Trying to read in a two variable file using INFILE but can't get it to work...

data SAData;
infile 'C:\Users\NSRINIV2\Desktop\NSAPurSample.txt' expandtabs truncover;
input datefield : mmddyy10. purindex ;

format datefield mmddyy10.;

run;

 

Hello mate!, Your file data is tab delimited in the first place. Kindly check and run the above

 

here is my log:


NOTE: The infile 'C:\Users\NSRINIV2\Desktop\NSAPurSample.txt' is:
Filename=C:\Users\NSRINIV2\Desktop\NSAPurSample.txt,
RECFM=V,LRECL=32767,File Size (bytes)=110,
Last Modified=08Aug2017:12:35:10,
Create Time=08Aug2017:12:35:10

NOTE: 7 records were read from the infile 'C:\Users\NSRINIV2\Desktop\NSAPurSample.txt'.
The minimum record length was 13.
The maximum record length was 15.
NOTE: The data set WORK.SADATA has 7 observations and 2 variables.
NOTE: DATA statement used (Total process time):
real time 0.01 seconds
cpu time 0.01 seconds

 

View solution in original post


All Replies
PROC Star
Posts: 172

Re: Trying to read in a two variable file using INFILE but can't get it to work...

[ Edited ]

should work:

 

data SAData;
infile 'D:\qsilver\Sas Data\NSAPur.txt' delimiter=' ';
input
datefield :mmddyy10.
purindex

format datefield mmddyy10.;
;
run;

Occasional Contributor
Posts: 16

Re: Trying to read in a two variable file using INFILE but can't get it to work...

I tried your code but it's not reading in properly... I've attached a screenshot of the log output and the output dataset for you to see..data.pnglog.png

Occasional Contributor
Posts: 16

Re: Trying to read in a two variable file using INFILE but can't get it to work...

Wait you made an edit to your original post and i'm getting a different error message and output format....still not working tho

Solution
2 weeks ago
PROC Star
Posts: 172

Re: Trying to read in a two variable file using INFILE but can't get it to work...

data SAData;
infile 'C:\Users\NSRINIV2\Desktop\NSAPurSample.txt' expandtabs truncover;
input datefield : mmddyy10. purindex ;

format datefield mmddyy10.;

run;

 

Hello mate!, Your file data is tab delimited in the first place. Kindly check and run the above

 

here is my log:


NOTE: The infile 'C:\Users\NSRINIV2\Desktop\NSAPurSample.txt' is:
Filename=C:\Users\NSRINIV2\Desktop\NSAPurSample.txt,
RECFM=V,LRECL=32767,File Size (bytes)=110,
Last Modified=08Aug2017:12:35:10,
Create Time=08Aug2017:12:35:10

NOTE: 7 records were read from the infile 'C:\Users\NSRINIV2\Desktop\NSAPurSample.txt'.
The minimum record length was 13.
The maximum record length was 15.
NOTE: The data set WORK.SADATA has 7 observations and 2 variables.
NOTE: DATA statement used (Total process time):
real time 0.01 seconds
cpu time 0.01 seconds

 

Occasional Contributor
Posts: 16

Re: Trying to read in a two variable file using INFILE but can't get it to work...

Ok this code is working for me. Thank you! Few follow up questions -

 

1) Why do you need the format statement... (below in bold/underline)? Shouldn't defining the data type as mmddyy10. in the input statement take care of the format? 

 

2) Why did you use expandtabs instead of delimiter='   '?

 

3) What is truncover?

 

data SAData;
infile 'D:\qsilver\Sas Data\NSAPur.txt' expandtabs truncover;
input datefield : mmddyy10. purindex ;
format datefield mmddyy10.;
run;

PROC Star
Posts: 172

Re: Trying to read in a two variable file using INFILE but can't get it to work...

Ok this code is working for me. Thank you! Few follow up questions -

 

1) Why do you need the format statement... (below in bold/underline)? Shouldn't defining the data type as mmddyy10. in the input statement take care of the format? 

The format in the input statement is actually an informat that reads and converts non standard data like your character date to a SAS date value which is merely a number. This number has to be rerepresented as a character date for which you need a format statement.

Always remember: Informat is to read and format is to write. They look the same yet they are different

 

2) Why did you use expandtabs instead of delimiter='   '?

I was lazy at first to have noticed your data delimited to blank at face value. But in reality when i actually moved my cursor I learned that it was actually tab delimited perhaps a copy paste from excel. If it's tab demilited you can't quite use dlm=' ' instead you need expandtabs or something dlm='2008'x it's blank and tab delimited--> @art297 the genius taught me this a couple of years ago

 

3) What is truncover?

Basically reads variable length records and doesn't jump to the next record and start reading  new values. Chill, in your data i don't see missing values, so this shouldn't affect the execution

 

HTH,

Naveen Srinivasan

 

data SAData;
infile 'D:\qsilver\Sas Data\NSAPur.txt' expandtabs truncover;
input datefield : mmddyy10. purindex ;
format datefield mmddyy10.;
run;

Occasional Contributor
Posts: 16

Re: Trying to read in a two variable file using INFILE but can't get it to work...

Thank you for all the follow up info!
Super User
Posts: 10,500

Re: Trying to read in a two variable file using INFILE but can't get it to work...

A minor addition about the DLM= on infile. DLM uses single characters. You may have data where your values are separated by spaces or commas and you could use DLM=' ,' . to read

123 John,Smith,"Some other text"

 

If you have a longer string of characters as a delimiter that is always present you would use the DLMSTR option instead. If you data were separated by *x* then DLMSTR='*x*' would be used to read data that looks like

123*x*John*x*Smith*x*Some other text

Occasional Contributor
Posts: 16

Re: Trying to read in a two variable file using INFILE but can't get it to work...

Helpful, thanks!
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 9 replies
  • 113 views
  • 2 likes
  • 3 in conversation