BookmarkSubscribeRSS Feed
Pj1989
Calcite | Level 5
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
6 REPLIES 6
SASKiwi
PROC Star

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.
         ;  
PGStats
Opal | Level 21

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
MikeZdeb
Rhodochrosite | Level 12

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;

Astounding
PROC Star

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.

Steelers_In_DC
Barite | Level 11

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;

Pj1989
Calcite | Level 5
Thank u so much

sas-innovate-2024.png

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.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 1266 views
  • 5 likes
  • 6 in conversation