BookmarkSubscribeRSS Feed
vishalrajpoot3
Obsidian | Level 7

Hello Team,

 

How can I concatenate the below data on the basis of unique dept with all the area in output?

 


data abc;
input dept $ area $;
datalines;
D1 a
d1 b
d1 c
d2 s
d2 d
d2 f
d3 y
d4 r
d4 f
d4 t
;
run;

8 REPLIES 8
PaigeMiller
Diamond | Level 26

Please show us the desired result.

--
Paige Miller
vishalrajpoot3
Obsidian | Level 7

@PaigeMiller

 

please see the below desired output.

 

dept    area
D1     a, b, c
d2     s, d, f
d3     y
d4     r, f, t

PaigeMiller
Diamond | Level 26

Thank you for showing us the desired output.

 

If you are creating a report and you need the results as shown, then I guess @novinosrin has given you the answer. However, if you are going to do additional analysis in SAS, this is a very poor way to structure the data for analysis, so I would advise against doing this in this case.

--
Paige Miller
vishalrajpoot3
Obsidian | Level 7
so what would you like to suggest doing in this case?
PaigeMiller
Diamond | Level 26

It depends on what your next steps are.

--
Paige Miller
Kurt_Bremser
Super User

@vishalrajpoot3 wrote:
so what would you like to suggest doing in this case?

Very simple. Don't do it. Longitudinal datasets are always better for analysis. Wide formats are only needed for presentations to PHBs.

novinosrin
Tourmaline | Level 20

something like this?

 

data abc;
input dept $ area $;
datalines;
D1 a
d1 b
d1 c
d2 s
d2 d
d2 f
d3 y
d4 r
d4 f
d4 t
;
run;

data want;
set abc;
by dept;
length new_var $30;
retain new_var;
if first.dept then call missing(new_var);
new_var=catx('-',new_var,area);
if last.dept;
run;
novinosrin
Tourmaline | Level 20

Hi @vishalrajpoot3  Modified my code to your requirement

 

data temp;
set abc;
_dept=upcase(dept);
run;
data want;
set temp;
by _dept;
length new_var $30;
retain new_var;
if first._dept then call missing(new_var);
new_var=catx(',',new_var,area);
if last._dept;
run;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 1828 views
  • 0 likes
  • 4 in conversation