BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
nicnad
Fluorite | Level 6

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.

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

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

9 REPLIES 9
art297
Opal | Level 21

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;

nicnad
Fluorite | Level 6

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.

art297
Opal | Level 21

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;

nicnad
Fluorite | Level 6

Thank you very much for the explanation.

This works perfectly.

nicnad
Fluorite | Level 6

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

Thank you for your help.

art297
Opal | Level 21

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;

nicnad
Fluorite | Level 6

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?

art297
Opal | Level 21

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;

nicnad
Fluorite | Level 6

Thank you very much for your help with this!

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 9 replies
  • 1002 views
  • 6 likes
  • 2 in conversation