Hi everyone,
I have the following dataset:
Product_ID | Name |
ID1 | NAME1 |
ID1 | NAME2 |
ID1 | NAME3 |
ID2 | NAME4 |
ID3 | NAME5 |
ID3 | NAME6 |
ID4 | NAME7 |
ID5 | NAME8 |
ID6 | NAME9 |
ID7 | NAME10 |
I'm trying to find a way to output/list the grouped Names for each Product ID in a dataset, like the table below:
Product_ID | New Column |
ID1 | • NAME1 • NAME2 • NAME3 |
ID2 | • NAME4 |
ID3 | • NAME5 • NAME6 |
ID4 | • NAME7 |
ID5 | • NAME8 |
ID6 | • NAME9 |
ID7 | • NAME10 |
Do you have any suggestions how to achieve this?
Thank you!
Hi,
This works (note you need ods excel which in one of the newer releases - 9.3 onwards perhaps, can't remember):
data x; infile cards dlm="," ; input product_ID : $20. NAME : $60. ; cards; ID1,NAME1 ID1,NAME2 ID1,NAME3 ID2,NAME4 ID3,NAME5 ID3,NAME6 ID4,NAME7 ID5,NAME8 ID6,NAME9 ID7,NAME10 ; run; data have; length temp $ 1024; set x; by product_id; if first.product_id then temp=''; retain temp; temp = catx('0D'x, temp, catx(" ", "^{unicode 25cf}",NAME) ); if last.product_id then output; run; ods noresults; ods escapechar="^"; ods excel file="s:\temp\rob\test.xlsx"; proc report data=have nowd OUT=have2 split="/"; column product_id temp; define product_id / display; define temp / display style(column)=[protectspecialchars=off cellheight=3cm cellwidth=2cm]; run; ods excel close;
Just an overview:
1) Add the bullet to your data (probably need to use the '07'x) e.g.:
data have;
set have;
name=cats('07'x,name);
run;
2) Use proc report to output your data, setting product_id as order variable and applying spanrows option:
proc report data=have spanrows;
define product_id / order style(column)=[vjust=middle];
...
Hi RW9,
Thank you for help!
I've adjusted some of the code that give me the correct output:
data x;
infile cards dlm="," ;
input product_ID : $20. NAME : $60. ;
cards;
ID1,NAME1
ID1,NAME2
ID1,NAME3
ID2,NAME4
ID3,NAME5
ID3,NAME6
ID4,NAME7
ID5,NAME8
ID6,NAME9
ID7,NAME10
;
run;
data have;
length temp $ 1024;
set x;
by product_id;
if first.product_id then temp='';
retain temp;
temp = catx("^{newline}", temp, catx(" ", "^{unicode 25cf}",NAME) );
if last.product_id then output;
run;
ods escapechar="^";
proc report data=have nowd OUT=have2 split="/";
column product_id temp;
define product_id / display;
define temp / display;
run;
But now I'm struggling to export the ODS output to Excel, since the bullet points are now exported into different rows and not into one row, is there any way around this?
Thank you!
Hi,
This works (note you need ods excel which in one of the newer releases - 9.3 onwards perhaps, can't remember):
data x; infile cards dlm="," ; input product_ID : $20. NAME : $60. ; cards; ID1,NAME1 ID1,NAME2 ID1,NAME3 ID2,NAME4 ID3,NAME5 ID3,NAME6 ID4,NAME7 ID5,NAME8 ID6,NAME9 ID7,NAME10 ; run; data have; length temp $ 1024; set x; by product_id; if first.product_id then temp=''; retain temp; temp = catx('0D'x, temp, catx(" ", "^{unicode 25cf}",NAME) ); if last.product_id then output; run; ods noresults; ods escapechar="^"; ods excel file="s:\temp\rob\test.xlsx"; proc report data=have nowd OUT=have2 split="/"; column product_id temp; define product_id / display; define temp / display style(column)=[protectspecialchars=off cellheight=3cm cellwidth=2cm]; run; ods excel close;
Thank you @RW9 - working perfectly!
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.