BookmarkSubscribeRSS Feed
Yvegunta
Obsidian | Level 7

Hi All,

 

I am trying to combine the names of the people staying at the same location having the same account_ID, as shown in the table below:

 

NameAddressAccount_ID
ArnoldPitt Street123
StallonePitt Street123
WillisPitt Street123
SmithGeorge Street123

 

What i ideally am looking to get is as below:

 

NameAddressAccount_ID
Arnold and Stallone and WillisPitt Street123
SmithGeorge Street123

 

Thanks in advance.

Yatindra

3 REPLIES 3
Astounding
PROC Star

Should be workable:

 

data want;

length newname $ 50;

set have;

by account_ID address;

if first.address then newname = name;

else newname = trim(newname) || ' and ' || name;

if last.address;

retain newname;

drop name;

rename newname = name;

run;

 

*********** EDITED to add the correction.

DR_Majeti
Quartz | Level 8

Hi Yatindar, 

 

data have;
infile datalines dsd ;
length Name $200 Address $200;
input Name $ Address $ Account_ID ;

datalines;
Arnold , Pitt Street, 123
Stallone, Pitt Street, 123
Willis, Pitt Street, 123
Smith, George Street, 123
;
run;

proc sort data=have;
by address account_id;
run;

data required;
set have;
by address account_id;
length new_name $200;
retain new_name ' ';
if first.address then new_name=strip(name);
else new_name=strip(new_name)||' and '||strip(name);
name=new_name;
drop new_name;
if last.address;
run;

 

Please find the solution

PGStats
Opal | Level 21

Use a do until() loop for this:

 

data have;
input Name :$12. Address &:$24. Account_ID ;
datalines;
Arnold Pitt Street  123 
Stallone Pitt Street  123 
Willis Pitt Street  123 
Smith George Street  123
; 

proc sort data=have; by account_id address name; run;

data want;
length allNames $64;
do until(last.address);
    set have; by account_id address;
    allNames = catx(" and ", allNames, name);
    end;
drop name;
rename allNames = name;
run;

proc print data=want noobs; run;
PG

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
  • 3 replies
  • 750 views
  • 0 likes
  • 4 in conversation