Your example ony shows 3 lines of data. To read across multiple lines of data in an INPUT statement use the carriage control character: /.
input @1 address_number $8.
/ @1 address_street $30.
/ @1 address_city $30.
;
A macro?
data want;
infile datalines truncover;
length line line1-line5 $32;
array l{5} line1-line5;
i = 1;
do until(missing(line));
input line &;
if i <= 5 then l{i} = line;
i + 1;
end;
id + 1;
drop i line;
datalines;
One line
Two
lines
Three
short
lines
Four
lines
is more than
enough
Five
lines
is way
too
much
;
proc print data=want noobs; run;
Hi, just one added idea ... you could increment "i" in the loop rather than initializing and incrementing in separate statements ...
do i=1 by 1 until(missing(line));
input line &;
if i <= 5 then l{i} = line;
end;
I like the approach ... just picking a small nit. The incoming file might not contain a blank line at the end. You might want to add some handling for that to make sure that the last address is part of the output.
Here is another solutions:
data have;
infile cards;
input address $25.;
cards;
38b
Elgin road
Kolkata-30
25
Main St
Pittsburgh
PA
;
run;
data count;
set have;
count+1;
if missing(address) then count = 0;
run;
data prep;
set count;
by address notsorted;
%macro lag;
%do i = 1 %to 5;
lag_address_&i = lag&i(address);
%end;
%mend;
%lag;
run;
data prep2;
set prep;
%macro deletes;
%do i = 1 %to 5;
%let j = %eval(&i+1);
if missing(lag_address_&i) then call missing(lag_address_&j);
%end;
%mend;
%deletes;
run;
data want;
set prep;
if count = 0 then do;
address2 = catx(' ',of lag_address_5-lag_address_1);
end;
run;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.