Created
October 19, 2017 02:17
-
-
Save statgeek/d583cfa992bf56da51d435165b07e96a to your computer and use it in GitHub Desktop.
SAS - combine rows into one record with a delimiter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
*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