In sashelp.class
I want to cancatinate name those who have same age group
i want output as like below
Name | Sex | Age | Height | Weight | |
Joyce | F | 11 | 51.3 | 50.5 | |
Thomas | M | 11 | 57.5 | 85 | Joyce Thomas |
James | M | 12 | 57.3 | 83 | |
Jane | F | 12 | 59.8 | 84.5 | |
John | M | 12 | 59 | 99.5 | |
Louise | F | 12 | 56.3 | 77 | |
Robert | M | 12 | 64.8 | 128 | James Jane John Louise Robert |
Alice | F | 13 | 56.5 | 84 | |
Barbara | F | 13 | 65.3 | 98 | |
Jeffrey | M | 13 | 62.5 | 84 | Alice Barbara Jeffrey |
Alfred | M | 14 | 69 | 112.5 | |
Carol | F | 14 | 62.8 | 102.5 | |
Henry | M | 14 | 63.5 | 102.5 | |
Judy | F | 14 | 64.3 | 90 | Alfred Carol Henry |
Janet | F | 15 | 62.5 | 112.5 | |
Mary | F | 15 | 66.5 | 112 | |
Ronald | M | 15 | 67 | 133 | |
William | M | 15 | 66.5 | 112 | Janet Mary Ronald William |
Philip | M | 16 | 72 | 150 | Philip |
proc sort data=sashelp.class out=have(keep= name age);
by age;
run;
data want;
do until(last.age);
set have;
by age;
length want $100;
want=catx(',',want,name);
end;
run;
So you want all the names of people who are 12 years old to be concatenated, such as this:
James Jane John Louise Robert
Is that correct, yes or no?
I can't imagine why anyone would want to do this, how this helps anything, how this allows you to perform more meaningful operations here, could you explain? After all, there are many ways to do this and many possible output formats, and the real solution depends on what you plan to do with this result.
Here is one way:
proc sql noprint;
select distinct name into :names separated by ' ' from sashelp.class
where age=12;
quit;
%put &=names;
Tnq
Just logical and practice purpose
@BrahmanandaRao wrote:
Tnq
Just logical and practice purpose
Practice perhaps but not so logical.
We get fair number of questions on how to deal with data provided with multiple values in a single variable and the solutions range from headachy to cumbersome or downright fragile.
proc sort data=sashelp.class out=have(keep= name age);
by age;
run;
data want;
do until(last.age);
set have;
by age;
length want $100;
want=catx(',',want,name);
end;
run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.