BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
BrahmanandaRao
Lapis Lazuli | Level 10

In sashelp.class 

I want to cancatinate name those who have same age group 

 

 

i want output as like below

 

NameSexAgeHeightWeightSame age_group _names 
JoyceF1151.350.5 
ThomasM1157.585Joyce Thomas
JamesM1257.383 
JaneF1259.884.5 
JohnM125999.5 
LouiseF1256.377 
RobertM1264.8128James Jane John Louise  Robert
AliceF1356.584 
BarbaraF1365.398 
JeffreyM1362.584Alice Barbara  Jeffrey
AlfredM1469112.5 
CarolF1462.8102.5 
HenryM1463.5102.5 
JudyF1464.390Alfred Carol Henry 
JanetF1562.5112.5 
MaryF1566.5112 
RonaldM1567133 
WilliamM1566.5112Janet Mary Ronald William
PhilipM1672150Philip
1 ACCEPTED SOLUTION

Accepted Solutions
Arjen
SAS Employee

First you need to make sure the data set is sorted by Age and then use a Data step with a by clause. Example code with comments below:

* sort the input dataset by age and name (the latter is optional);
proc sort data=sashelp.class out=sortedbyage;
	by Age Name;
run;

* create a new dataset with concatenated names;
data concatenatednames;
	* use sorted dataset;
	set sortedbyage;
	* group by Age;
	by Age;
	* Same_age is the temporary variable for holding the concatenated names. Make it sufficiently long;
	length Same_age $ 80;
	* Same_age_group_names will hold the final value at the last record for a certain age;
	length Same_age_group_names $ 80;

	* Hold on to the value in the Same_age variable across observations;
	retain Same_age;
	if first.Age then
     do;
		* First record of a certain age, initialize the variable with the name of the student;
		Same_age = Name;
	 end;
	else 
	 do;
		* Not the first record, append the name to the Same_age variable;
		Same_age = CATX(" ", Same_age, Name);
	 end;
	if last.Age then
	 do;
	 	* Last record of a certain age. Save the concatenated string in the Same_age_group_names variable;
		Same_age_group_names = Same_age;
	 end;
	* We don't want Same_age to end up in our data, so drop it;
	drop Same_age;
run;

View solution in original post

11 REPLIES 11
novinosrin
Tourmaline | Level 20

proc sort data=sashelp.class out=have;
by age;
run;

data want;
do until(last.age);
set have;
by age;
length temp Same_age_group_names $100;
temp=catx(' ',temp,name);
if last.age then Same_age_group_names=temp;
output;
end;
drop temp;
run;
BrahmanandaRao
Lapis Lazuli | Level 10
Thank you very much
Arjen
SAS Employee

Beat me to it 🙂

novinosrin
Tourmaline | Level 20

HI @Arjen  Appreciate your traditional approach and nice comments. That's a virtue.

 

 

Arjen
SAS Employee

Hi @novinosrin
Thanks. Like the do-until approach as well. Would never have thought of that. 

Arjen
SAS Employee

First you need to make sure the data set is sorted by Age and then use a Data step with a by clause. Example code with comments below:

* sort the input dataset by age and name (the latter is optional);
proc sort data=sashelp.class out=sortedbyage;
	by Age Name;
run;

* create a new dataset with concatenated names;
data concatenatednames;
	* use sorted dataset;
	set sortedbyage;
	* group by Age;
	by Age;
	* Same_age is the temporary variable for holding the concatenated names. Make it sufficiently long;
	length Same_age $ 80;
	* Same_age_group_names will hold the final value at the last record for a certain age;
	length Same_age_group_names $ 80;

	* Hold on to the value in the Same_age variable across observations;
	retain Same_age;
	if first.Age then
     do;
		* First record of a certain age, initialize the variable with the name of the student;
		Same_age = Name;
	 end;
	else 
	 do;
		* Not the first record, append the name to the Same_age variable;
		Same_age = CATX(" ", Same_age, Name);
	 end;
	if last.Age then
	 do;
	 	* Last record of a certain age. Save the concatenated string in the Same_age_group_names variable;
		Same_age_group_names = Same_age;
	 end;
	* We don't want Same_age to end up in our data, so drop it;
	drop Same_age;
run;
novinosrin
Tourmaline | Level 20

More fun before my coffee break

 

proc sort data=sashelp.class out=have;
by age;
run;
proc transpose data=have(keep=age name) out=_have(drop=_:);
by age;
var name;
run;
data want;
merge have _have;
by age;
length  Same_age_group_names $100;
if last.age then Same_age_group_names=catx(' ',of col:);
drop col:;
run;
Arjen
SAS Employee

Brilliant Smiley Happy

 

novinosrin
Tourmaline | Level 20

Park in temp array and concatenate all at once

 

proc sort data=sashelp.class out=have;
by age;
run;
data want;
array temp(999)$20 _temporary_;
call missing( of temp(*));
do _n_=1 by 1 until(last.age);
set have;
by age;
length  Same_age_group_names $100;
temp(_n_)=name;
if last.age then Same_age_group_names=catx(' ',of temp(*));
output;
end;
run;

Edited to add fogotten call missing

Arjen
SAS Employee

Anyone would be crazy not to love the versatility of the SAS programming language and the data step. 

novinosrin
Tourmaline | Level 20

Indeed sir @Arjen   extremely addictive  🙂

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 11 replies
  • 1411 views
  • 3 likes
  • 3 in conversation