HI Experts Good Morning
Here I have dataset unable to read the data
data trians_time;
input train_name $ 1-22 arrival 23-26 dept 27-32 total_hours 34-39;
informat arrival dept total_hours time5.;
format arrival dept total_hours time5.;
datalines;
VSKP GARIB RATH(12740) 20:30 04:06 07:36
GODAVARI EXP(12728) 7:40 01:39 07:59
FALAKNUMA EXP(12704) 15:55 00:02 08:07
;
run;
data trians_time;
input;
call scan(_infile_,-3,p,l,' ');
train_name=substr(_infile_,1,p-1);
arrival=input(scan(_infile_,-3,' '),time5.);
dept=input(scan(_infile_,-2,' '),time5.);
total_hours=input(scan(_infile_,-1,' '),time5.);
drop p l;
format arrival dept total_hours time5.;
datalines;
VSKP GARIB RATH(12740) 20:30 04:06 07:36
GODAVARI EXP(12728) 7:40 01:39 07:59
FALAKNUMA EXP(12704) 15:55 00:02 08:07
;
run;
The challenge with your current data as you posted it are:
- You are using LIST input but the data doesn't start on the positions as defined in your INPUT statement
- The data as posted doesn't have a fixed column position so you couldn't use LIST input
- You want to read a string into variable "train_name" that has blanks in it. But then blanks are also the delimiter between columns.
So... before trying to provide you some code we need to be sure how your actual data looks like. Is it positional like your Input statement indicates or isn't it. Is the column delimiter really a blank or did something get lost when copying data around?
Could you change the delimiter/your data? An other delimiter would make things much simpler to read. Looking at the code you've posted I assume you're a beginner so ideally we don't "start" reading data where we have to do specialty coding for which you're not really ready yet.
Here how the code could look like when changing the column delimiter to a pipe character.
data trains_time;
infile datalines dlm='|' dsd truncover;
input train_name :$22. (arrival dept total_hours) (:time5.);
format arrival dept total_hours time5.;
datalines;
VSKP GARIB RATH(12740)|20:30|04:06|07:36
GODAVARI EXP(12728)|7:40|01:39|07:59
FALAKNUMA EXP(12704)|15:55|00:02|08:07
;
run;
Thank you sir
Actually the original poster was using COLUMN input mode.
Your code is using LIST input mode.
The problem is that there is no way to combine COLUMN input mode and use of the TIME informat. What is normally done is to use a mixed mode INPUT statement that has some COLUMN mode and some FORMATTED mode.
input train_name $ 1-22 @24 arrival time5.;
or some COLUMN mode and some LIST mode. Either by attaching an INFORMAT to the variable.
input train_name $ 1-22 arrival;
informat arrival time.;
or adding an in-line informat with the colon modifier to use LIST mode instead of FORMATTED mode.
input train_name $ 1-22 arrival :time.;
Note that when using LIST mode you do not need to specify a WIDTH on the INFORMAT specification. SAS will ignore the width and adjust to use the width of actual next set of characters in the input buffer.
data trians_time;
input;
call scan(_infile_,-3,p,l,' ');
train_name=substr(_infile_,1,p-1);
arrival=input(scan(_infile_,-3,' '),time5.);
dept=input(scan(_infile_,-2,' '),time5.);
total_hours=input(scan(_infile_,-1,' '),time5.);
drop p l;
format arrival dept total_hours time5.;
datalines;
VSKP GARIB RATH(12740) 20:30 04:06 07:36
GODAVARI EXP(12728) 7:40 01:39 07:59
FALAKNUMA EXP(12704) 15:55 00:02 08:07
;
run;
Hi Sharp
Thank You for your brilliant coding skill
could you please explain call scan(_infile_,p,l,' ')
this is the first time logical syntax
Regards,
Anand
The documentation for the call scan() routine is found here.
@BrahmanandaRao wrote:
Hi Sharp
Thank You for your brilliant coding skill
could you please explain call scan(_infile_,p,l,' ')
this is the first time logical syntax
Regards,
Anand
_INFILE_ is an automatic SAS variable that has the contents of the input buffer when executing an INPUT statement.
So when needed you can examine or parse the entire input line using functions as needed.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.