Skip to content

Instantly share code, notes, and snippets.

@statgeek
Created October 19, 2017 02:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save statgeek/d583cfa992bf56da51d435165b07e96a to your computer and use it in GitHub Desktop.
Save statgeek/d583cfa992bf56da51d435165b07e96a to your computer and use it in GitHub Desktop.
SAS - combine rows into one record with a delimiter
*create sample data for demonstration;
data have;
infile cards dlm='09'x;
input OrgID Product $ States $;
cards;
1 football DC
1 football VA
1 football MD
2 football CA
3 football NV
3 football CA
;
run;
*Sort - required for both options;
proc sort data=have;
by orgID;
run;
**********************************************************************;
*Use RETAIN and BY group processing to combine the information;
**********************************************************************;
data want_option1;
set have;
by orgID;
length combined $100.;
retain combined;
if first.orgID then
combined=states;
else
combined=catx(', ', combined, states);
if last.orgID then
output;
run;
**********************************************************************;
*Transpose it to a wide format and then combine into a single field;
**********************************************************************;
proc transpose data=have out=wide prefix=state_;
by orgID;
var states;
run;
data want_option2;
set wide;
length combined $100.;
combined=catx(', ', of state_:);
run;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment