Hi,
Lets say I have the following table :
Name | Date | Source |
---|---|---|
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 |
What is the SAS procedure to obtain the following table :
Name | Source |
---|---|
Bob | test1 - 2012-01-01, test1 - 2012-01-02, test2 - 2012-01-03 |
Alex | test5 - 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.
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;
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;
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.
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;
Thank you very much for the explanation.
This works perfectly.
One last thing, how do I add the numbre of elements in variable source?
Thank you for your help.
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;
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?
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;
Thank you very much for your help with this!
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.
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.