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

Hi all, 

I would like to concatenate multiple subject IDs that looks like this:

 

S002001~S002002, S002006~S002012, S002014~S002016

 

 

so basically, I would like to connect the subjid's that are continuous with "~" and use "," for those that is not continuous..

 

here's the data:


data a;
input subjid $8.;
cards;
S002001
S002002
S002006
S002007
S002008
S002009
S002010
S002011
S002012
S002014
S002015
S002016
;
run;

 

 

Thanks in advance for your help!

1 ACCEPTED SOLUTION

Accepted Solutions
s_lassen
Meteorite | Level 14

I think the easiest way is to start with creating a group variable:

Data grouped;
  set a;
  _number=input(substr(subjid,2),6.);
  if dif(_number) ne 1 then
    group+1;
  drop _number;
run;

Then you can use CATX to create the string:

Data want;
  set grouped end=done;
  by group;
  length Sequences $200;
  retain Sequences;
  if first.group then 
    call catx(', ',Sequences,subjid);
  else if last.group then 
    call catx('~',Sequences,subjid);
  if done;
  keep Sequences;
run;

View solution in original post

4 REPLIES 4
PeterClemmensen
Tourmaline | Level 20

Are all your values prefixed by "S"?

andreas_lds
Jade | Level 19

Do you want that list in saved in a dataset-variable? Can you describe what the next steps are?

s_lassen
Meteorite | Level 14

I think the easiest way is to start with creating a group variable:

Data grouped;
  set a;
  _number=input(substr(subjid,2),6.);
  if dif(_number) ne 1 then
    group+1;
  drop _number;
run;

Then you can use CATX to create the string:

Data want;
  set grouped end=done;
  by group;
  length Sequences $200;
  retain Sequences;
  if first.group then 
    call catx(', ',Sequences,subjid);
  else if last.group then 
    call catx('~',Sequences,subjid);
  if done;
  keep Sequences;
run;
ykcgod
Calcite | Level 5

Yess! This will do. Thanks so much for your help!

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!

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