BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
bzubrick
Obsidian | Level 7

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;
1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

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

9 REPLIES 9
novinosrin
Tourmaline | Level 20

should work:

 

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

format datefield mmddyy10.;
;
run;

bzubrick
Obsidian | Level 7

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

bzubrick
Obsidian | Level 7

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

novinosrin
Tourmaline | Level 20

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

 

bzubrick
Obsidian | Level 7

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;

novinosrin
Tourmaline | Level 20

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;

bzubrick
Obsidian | Level 7
Thank you for all the follow up info!
ballardw
Super User

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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