cancel
Showing results for 
Search instead for 
Did you mean: 

Reading data from external file in SAS

SOLVED
Aexor
Lapis Lazuli | Level 10
Solved!

Reading data from external file in SAS

Message contains a hyperlink

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
Solution

Re: Reading data from external file in SAS

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
Kurt_Bremser
Super User

Re: Reading data from external file in SAS

Aexor
Lapis Lazuli | Level 10

Re: Reading data from external file in SAS

Yes.

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

Re: Reading data from external file in SAS

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
Solution

Re: Reading data from external file in SAS

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

Re: Reading data from external file in SAS

Message contains a hyperlink

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
Super User

Re: Reading data from external file in SAS

Message contains an image

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

Re: Reading data from external file in SAS

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.