BookmarkSubscribeRSS Feed
mnew
Calcite | Level 5
Experts:

Another beginner question. How do you use the values in different observations as arguments for a function? I'm trying to concatenate string values from multiple observations. I'll use the sasuser.pilots as an example. Each observation has a person's name etc. What if you need to generate name lists so you can assign these people to teams of five?

Here's very clumsy code I managed to come up with. I'm sure it is not the way to go because if the dataset has hundreds of people, I would be in real misery :). Maybe my trouble is also not knowing how to use a do loop here.

Thank you for your time in advance.

data work.NameList;
set sasuser.pilots(keep=lastname)End=last ;
Length GroupList $ 500;
retain GroupList;
if _n_ < 11 then do;
Counter+1;
Grouplist=Catx(';',Grouplist,LastName);
if _n_=10 then do;
output;
GroupList='';
Counter=0;
end;
end;
if 11 <=_n_<=15 then do;
Counter+1;
Grouplist=Catx(';',Grouplist,LastName);
if _n_=15 then do;
output;
GroupList='';
Counter=0;
end;
end;
if _n_>15 then do;
Counter+1;
Grouplist=Catx(';',Grouplist,LastName);
if last=1 then output;
end;
run;
proc print data=work.namelist;
run;
2 REPLIES 2
SPR
Quartz | Level 8 SPR
Quartz | Level 8
Hello Mnew,

This is a possible solution. It could be much easier if you need only team number.
[pre]
proc SQL;
create table n as
select distinct name as name from sashelp.class
;quit;
data g;
length team $100;
retain team;
set n;
if _n_=1 then team="";
if MOD(_n_-1,5) = 0 then do; g+1; team=""; end;
team=TRIM(team)||TRIM(name)||";";
run;
proc SQL;
create table g1 as
select a.name,a.g,b.team
from g as a left join (select g, max(team) as team from g group by g) as b
on a.g=b.g
;quit;
proc SQL;
create table r as
select a.*, b.g, b.team
from sashelp.class as a left join g1 as b on a.name=b.name
;quit;
[/pre]
Sincerely,
SPR
mnew
Calcite | Level 5
Thank you so much!
I went back and revised my code. It's less clumsy now. You introduced the MOD function to me :).
I've signed up for the SAS SQL training so I will review your code after that study...

data work.NameList;
set sasuser.pilots(keep=lastname)End=last ;
Length GroupList $ 500;
retain GroupList;
Grouplist=Catx(';',Grouplist,LastName);
if mod(_n_,5)=0 then do;
output;
GroupList='';
end;
else if last=1 then output;
drop lastname;
run;
proc print data=work.namelist;
run;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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