BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Natalie111
Fluorite | Level 6

Hi, I am trying to reorganise my dataset; the original dataset is in the following format (after sorting first by var1 and then by date):

 

 

data WORK;

input var1 date var2;

datalines;

 

1        d1       a   

1        d1       b   

1        d2       a      

2        d3       c

3        d4       a   

3        d5       a   

3        d5       b      

3        d5       c

;

The output dataset should look like:

 

var1  date    var2* 

1        d1       a,b   

1        d2       a   

2        d3       c

3        d4       a   

3        d5       a,b,c  

 

In a nutshell, I would like to keep only the unique combinations of var1 and date and concatenate information provided by var2 into a single new variable, i.e. var2*. 

 

I have tried different things but nothing seems to work so far as it should be. Could anyone please help?

 

Thanks!!

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

Welcome to the SAS Community 🙂

 

data want;
	set work;
	by var1 date;

	if first.date then _var2=var2;
	else _var2=catx(',', _var2, var2);

	retain _var2;
	if last.date;
run;

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26

PROC TRANSPOSE is a simple way to do this, but it will produce three columns that contain original variable var2, rather than a text string as you have shown like a,b,c. I'd recommend the former, because I don't really see any easy way to make use of text string like a,b,c in the future.

 

data have;
input var1 date $ var2 $;
datalines;
1        d1       a   
1        d1       b   
1        d2       a      
2        d3       c
3        d4       a   
3        d5       a   
3        d5       b      
3        d5       c
;

proc transpose data=have out=want(drop=_name_) prefix=var2_;
	by var1 date;
	var var2;
run;

 

--
Paige Miller
PaigeMiller
Diamond | Level 26

Adding

 

If you really want the text string a,b,c rather than the three columns, after doing PROC TRANSPOSE use:

 

data want2;
	set want;
	var2=catx(',',of var2_:);
	drop var2_:;
run;
--
Paige Miller
PeterClemmensen
Tourmaline | Level 20

Welcome to the SAS Community 🙂

 

data want;
	set work;
	by var1 date;

	if first.date then _var2=var2;
	else _var2=catx(',', _var2, var2);

	retain _var2;
	if last.date;
run;

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

Develop Code with SAS Studio

Get started using SAS Studio to write, run and debug your SAS programs.

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
  • 4 replies
  • 1451 views
  • 3 likes
  • 3 in conversation