BookmarkSubscribeRSS Feed
jlwatts
Calcite | Level 5

I have a table (email_list) with two values:

  • ORG_ID
  • email_address

This tables has multiple records with the same ORG_ID and unique email addresses.  I want to build a table so that I have a single row for each ORG_ID with all of the individual emails concatenated into a single value with the emails separated by a comma so that I can use that field to email the users.

 

I have the count of the records:

proc sql;

   select count(*) into :rec_count

   from work.email_list;

quit;

 

Then I want to build the list:

%do j=1 to &rec_count;

    if first record:

        org = org_id;

        email = email_address

    else

       if org = org_id then email = email || "', '" || email_address

       else

           org = org_id,

           email = email_address;

 

4 REPLIES 4
Kurt_Bremser
Super User

You don't need the record count.

proc sort data=have;
by org_id;
run;

data want (compress=yes);
set have;
by org_id;
length email $32767; /* maximum possible length */
if first.org_id
then email = email_address;
else email = catx(",",email,email_address);
if last.org_id;
drop email_address;
run;
ballardw
Super User

Did we perhaps miss a Retain?

 

 

data want (compress=yes);
set have;
by org_id;
length email $32767; /* maximum possible length */
retain email;
if first.org_id
then email = email_address;
else email = catx(",",email,email_address);
if last.org_id;
drop email_address;
run;

 

Tom
Super User Tom
Super User

Put the SET statement inside the DO loop.

data want;
  do until(last.org_id);
    set have;
    by org_id;
    length email $1000;
    email=catx(',',email,email_address);
  end;
  drop email_address;
run;

 

PS Don't try to use macro code to generate SAS code until you know what SAS code you need to generate.  For this problem there is no need to generate any code, so macro code is not needed.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 4 replies
  • 459 views
  • 0 likes
  • 4 in conversation