BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Aexor
Lapis Lazuli | Level 10

Hello All,

 

I have below requirement

 

In a external file I have data like 

Sam,sam11@abc.com ,abc 

22/09/1992,Math

Ram,ram5@abc.com ,xyz

22/09/1993, English

 

I want to read data from this file and get output like  

Sam,sam11@abc.com ,abc 22/09/1992,Math

Ram,ram5@abc.com ,xyz22/09/1993, English

 

Please let me know the how can i solve this.

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
andreas_lds
Jade | Level 19

Or one input-statement with the / (means: start reading next line)

data want;
infile datalines dlm=",";
length
  name $10
  email $50
  whatever $3
  date 4
  course $15
;
format date ddmmyy10.;
input name email whatever / date:ddmmyy10. course;
datalines;
Sam,sam11@abc.com ,abc 
22/09/1992,Math
Ram,ram5@abc.com ,xyz
22/09/1993, English
;

View solution in original post

7 REPLIES 7
Aexor
Lapis Lazuli | Level 10
Yes.

Sam,sam11@abc.com ,abc
22/09/1992,Math
Ram,ram5@abc.com ,xyz
22/09/1993, English
Kurt_Bremser
Super User

Then you just need two INPUTs per data step iteration:

data want;
infile datalines dlm=",";
length
  name $10
  email $50
  whatever $3
  date 4
  course $15
;
format date ddmmyy10.;
input name email whatever;
input date:ddmmyy10. course;
datalines;
Sam,sam11@abc.com ,abc 
22/09/1992,Math
Ram,ram5@abc.com ,xyz
22/09/1993, English
;
andreas_lds
Jade | Level 19

Or one input-statement with the / (means: start reading next line)

data want;
infile datalines dlm=",";
length
  name $10
  email $50
  whatever $3
  date 4
  course $15
;
format date ddmmyy10.;
input name email whatever / date:ddmmyy10. course;
datalines;
Sam,sam11@abc.com ,abc 
22/09/1992,Math
Ram,ram5@abc.com ,xyz
22/09/1993, English
;
FreelanceReinh
Jade | Level 19

You could even omit the slash because the default behavior of the INPUT statement is to read the remaining values, if any, from the next line (see Reading Past the End of a Line) -- at the cost of an additional note in the log:

NOTE: SAS went to a new line when INPUT statement reached past the end of a line.
Tom
Super User Tom
Super User

Let's put your example lines into a file:

options parmcards=old;
filename old temp;

parmcards4;
Sam,sam11@abc.com ,abc 
22/09/1992,Math
Ram,ram5@abc.com ,xyz
22/09/1993, English
;;;;

Do you want to generate a new text file without the extra line break?

filename new temp;
data _null_;
  infile old;
  file new;
  input ;
  put _infile_ ',' @;
  input;
  put _infile_;
run;

Results:

Sam,sam11@abc.com ,abc,22/09/1992,Math
Ram,ram5@abc.com ,xyz,22/09/1993, English

Or did you want to read the file into a SAS dataset?

We can just read it and SAS will automatically jump to the next line when it runs out of values on the first line.

data want;
  infile old dsd ;
  input name :$20. email :$200. other :$20. date :ddmmyy. department :$20. ;
  format date yymmdd10.;
run;

image.png

Or we could tell it when to move to a new line.  In that case you might want to add the TRUNCOVER option to prevent if from moving too soon.

data want;
  infile old dsd truncover ;
  input name :$20. email :$200. other :$20. / date :ddmmyy. department :$20. ;
  format date yymmdd10.;
run;

or

data want;
  infile old dsd truncover ;
  input #1 name :$20. email :$200. other :$20. #2 date :ddmmyy. department :$20. ;
  format date yymmdd10.;
run;

or even

data want;
  infile old dsd truncover ;
  input name :$20. email :$200. other :$20.;
  input date :ddmmyy. department :$20. ;
  format date yymmdd10.;
run;

 

Aexor
Lapis Lazuli | Level 10
This is awesome.
Thanks for explaining 🙂
My requirement was only to generate a new text file without the extra line break but I got to know many things. Thanks.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 7 replies
  • 1689 views
  • 7 likes
  • 5 in conversation