Help using Base SAS procedures

Group by a variable and concatenate another variable

Accepted Solution Solved
Reply
Regular Contributor
Posts: 186
Accepted Solution

Group by a variable and concatenate another variable

Hi,

Lets say I have the following table :

NameDateSource
Bob2012-01-01test1
Bob2012-01-02test1
Bob2012-01-03test2
Alex2012-02-02test5
Alex2012-02-03test5

What is the SAS procedure to obtain the following table :

NameSource
Bobtest1 - 2012-01-01, test1 - 2012-01-02, test2 - 2012-01-03
Alextest5 - 2012-02-02, test5 2012-02-03

To be more specific, I want to concatenate source & " - " & date and for each concatenated sources and date, I want to separate them with ", ".

Is this something that can be done in SAS?

Thank you for your help and time.


Accepted Solutions
Solution
‎02-28-2013 10:53 AM
PROC Star
Posts: 7,357

Re: Group by a variable and concatenate another variable

As long as your data are already grouped by name, I think that the following does what you want:

data have;

  input Name $ Date yymmdd10. Source $;

  cards;

Bob 2012-01-01 test1

Bob 2012-01-02 test1

Bob 2012-01-03 test2

Alex 2012-02-02 test5

Alex 2012-02-03 test5

;

data want (drop=source_in);

  set have (rename=(source=source_in));

  retain source;

  by name notsorted;

  if first.name then source=cat(strip(source_in),' - ',put(date,yymmdd10.));

  else source=cat(strip(source),', ',strip(source_in),' - ',put(date,yymmdd10.));

  if last.name then output;

run;

View solution in original post


All Replies
Solution
‎02-28-2013 10:53 AM
PROC Star
Posts: 7,357

Re: Group by a variable and concatenate another variable

As long as your data are already grouped by name, I think that the following does what you want:

data have;

  input Name $ Date yymmdd10. Source $;

  cards;

Bob 2012-01-01 test1

Bob 2012-01-02 test1

Bob 2012-01-03 test2

Alex 2012-02-02 test5

Alex 2012-02-03 test5

;

data want (drop=source_in);

  set have (rename=(source=source_in));

  retain source;

  by name notsorted;

  if first.name then source=cat(strip(source_in),' - ',put(date,yymmdd10.));

  else source=cat(strip(source),', ',strip(source_in),' - ',put(date,yymmdd10.));

  if last.name then output;

run;

Regular Contributor
Posts: 186

Re: Group by a variable and concatenate another variable

Thank you very much for your quick reply.

This works perfectly.

Just to clarify, when you say as long as my data is already grouped by name did you mean sorted by name?

Also, I think the date in my real table is not formated properly because I get the following error in the put statement :

Format $YYMMDD was not found or could not be loaded.

How do I fix this error?

Thank you for your help and time.

PROC Star
Posts: 7,357

Re: Group by a variable and concatenate another variable

Your example data wasn't sorted by name, but the records for each name were next to each other and already in the order that you indicated you wanted to end up with.  The 'by name notsorted' statement, in the code I suggested, would work if you're actual data were similarly ordered.

You are getting the error because your date field is apparently a character field rather than a SAS date.

If that is the case, in the two lines where I used "put(date,yymmdd10.)", simply change them to "strip(date)". i.e.,

data want (drop=source_in);

  set have (rename=(source=source_in));

  retain source;

  by name notsorted;

  if first.name then source=cat(strip(source_in),' - ',strip(date));

  else source=cat(strip(source),', ',strip(source_in),' - ',strip(date));

  if last.name then output;

run;

Regular Contributor
Posts: 186

Re: Group by a variable and concatenate another variable

Thank you very much for the explanation.

This works perfectly.

Regular Contributor
Posts: 186

Re: Group by a variable and concatenate another variable

One last thing, how do I add the numbre of elements in variable source?

Thank you for your help.

PROC Star
Posts: 7,357

Re: Group by a variable and concatenate another variable

You could use something like:

data have;

  input Name $ Date $10. Source $;

  cards;

Bob 2012-01-01 test1

Bob 2012-01-02 test1

Bob 2012-01-03 test2

Alex 2012-02-02 test5

Alex 2012-02-03 test5

;

data want (drop=date source_in);

  set have (rename=(source=source_in));

  retain source;

  by name notsorted;

  if first.name then do;

    source=cat(strip(source_in),' - ',strip(date));

    elements=1;

  end;

  else do;

    source=cat(strip(source),', ',strip(source_in),' - ',strip(date));

    elements+1;

  end;

  if last.name then output;

run;

Regular Contributor
Posts: 186

Re: Group by a variable and concatenate another variable

Thank you very much, this works perfectly.

A last detail, how do I make "elements" column appear as the second column instead of the third column?

PROC Star
Posts: 7,357

Re: Group by a variable and concatenate another variable

Just decare it before you declare source.  e.g.,

data want (drop=date source_in);

  set have (rename=(source=source_in));

  retain elements source;

  by name notsorted;

  if first.name then do;

    source=cat(strip(source_in),' - ',strip(date));

    elements=1;

  end;

  else do;

    source=cat(strip(source),', ',strip(source_in),' - ',strip(date));

    elements+1;

  end;

  if last.name then output;

run;

Regular Contributor
Posts: 186

Re: Group by a variable and concatenate another variable

Thank you very much for your help with this!

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 9 replies
  • 265 views
  • 6 likes
  • 2 in conversation