Hello All,
I have below requirement
In a external file I have data like
Sam,[email protected] ,abc
22/09/1992,Math
Ram,[email protected] ,xyz
22/09/1993, English
I want to read data from this file and get output like
Sam,[email protected] ,abc 22/09/1992,Math
Ram,[email protected] ,xyz22/09/1993, English
Please let me know the how can i solve this.
Thanks!
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,[email protected] ,abc
22/09/1992,Math
Ram,[email protected] ,xyz
22/09/1993, English
;
Can you be sure there's always two lines for a single person, in immediate succession?
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,[email protected] ,abc
22/09/1992,Math
Ram,[email protected] ,xyz
22/09/1993, English
;
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,[email protected] ,abc
22/09/1992,Math
Ram,[email protected] ,xyz
22/09/1993, English
;
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.
Let's put your example lines into a file:
options parmcards=old;
filename old temp;
parmcards4;
Sam,[email protected] ,abc
22/09/1992,Math
Ram,[email protected] ,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,[email protected] ,abc,22/09/1992,Math Ram,[email protected] ,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;
CFC_SAS_Communities_400x225.jpg
It's your turn to help shape SAS Innovate 2027. Share your expertise and inspire the SAS community.
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.
Ready to level-up your skills? Choose your own adventure.