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

Hi all, I'm struggling with carrying data from one row to the next using a method that I'm pretty sure has worked in the past. I simply want to carry the 'name' from one row to the next, within each table subset, and end up with a last.table record which has all of the names in that table concatenated into one string. I also want to make use of the 'retain' function. 

 

This is my current data:

name table rowcat
Riley t1  
Henderson t1  
MacDonald t1  
Cook t1  
DuPage t2  
Samoa t2  

 

This is what I want:

 

name table rowcat
Riley t1 Riley,
Henderson t1 Riley, Henderson,
MacDonald t1 Riley, Henderson, MacDonald,
Cook t1 Riley, Henderson, MacDonald, Cook,
DuPage t2 DuPage,
Samoa t2 DuPage, Samoa,

 

Can anyone help me understand why this code isn't working?

data have;
	length name $ 12 table $ 2 rowcat $ 200;
	input name $ table $;
	datalines;
		Riley t1
		Henderson t1
		MacDonald t1
		Cook t1
		DuPage t2
		Samoa t2
	;
run;

proc sort data=have; by table; run;

data want; 
	set have; 
	by table;

	if first.table then do;
		rowcat = ' ';
	end;

	rowcat = strip(strip(name)||", "||strip(rowcat));

	retain rowcat;
run;

Thanks!!

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

This makes the shown desired output:

data have;
	length name $ 12 table $ 2 ;
	input name $ table $;
	datalines;
Riley t1
Henderson t1
MacDonald t1
Cook t1
DuPage t2
Samoa t2
	;
run;

proc sort data=have; 
   by table; 
run;

data want; 
	set have; 
	by table;
   length rowcat $ 200;
   retain rowcat;
	if first.table then do;
		rowcat = ' ';
	end;

	rowcat = catx(', ',rowcat,name);

run;

You had a moderately subtle technical issue.  If you Retain a variable that is on the input data set it gets reread from the source data set, in your case missing. So nothing would be kept from the previous record.

Note that I removed the ROWCAT from the first data set.

 

Catx, and the other CAT functions are bit cleaner than using strip(variable)|| constantly. The Catx function places the first parameter between values when both are not missing. So unless you actually want multiple commas adjacent if a Name happened to be missing is a more compact code. CATX automatically removes trailing blanks.

 

 

View solution in original post

2 REPLIES 2
ballardw
Super User

This makes the shown desired output:

data have;
	length name $ 12 table $ 2 ;
	input name $ table $;
	datalines;
Riley t1
Henderson t1
MacDonald t1
Cook t1
DuPage t2
Samoa t2
	;
run;

proc sort data=have; 
   by table; 
run;

data want; 
	set have; 
	by table;
   length rowcat $ 200;
   retain rowcat;
	if first.table then do;
		rowcat = ' ';
	end;

	rowcat = catx(', ',rowcat,name);

run;

You had a moderately subtle technical issue.  If you Retain a variable that is on the input data set it gets reread from the source data set, in your case missing. So nothing would be kept from the previous record.

Note that I removed the ROWCAT from the first data set.

 

Catx, and the other CAT functions are bit cleaner than using strip(variable)|| constantly. The Catx function places the first parameter between values when both are not missing. So unless you actually want multiple commas adjacent if a Name happened to be missing is a more compact code. CATX automatically removes trailing blanks.

 

 

techsassy
Obsidian | Level 7

Thanks so much for the solution, as well as the CAT function encouragement!

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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
  • 2 replies
  • 2289 views
  • 0 likes
  • 2 in conversation