DATA Step, Macro, Functions and more

Record in multiple lines

Reply
New Contributor
Posts: 3

Record in multiple lines

I hav a sas data set with my data for one record getting spilled as say 5 lines (this may vary for different recordss)however the 6th line would be a blank .so i know its an end of record..how do i read these 5 records as one record ?

Example

38b
Elgin road
Kolkata-30

These appear as separate records but is actually one address

Need help to deaign a macro to read this as a single record
Super User
Posts: 3,105

Re: Record in multiple lines

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.
         ;  
Respected Advisor
Posts: 4,646

Re: Record in multiple lines

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;
PG
Valued Guide
Posts: 765

Re: Record in multiple lines

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;

Super User
Posts: 5,081

Re: Record in multiple lines

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.

Valued Guide
Posts: 858

Re: Record in multiple lines

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;

New Contributor
Posts: 3

Re: Record in multiple lines

Thank u so much
Ask a Question
Discussion stats
  • 6 replies
  • 311 views
  • 5 likes
  • 6 in conversation