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: Call for Content

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 16. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 1028 views
  • 3 likes
  • 4 in conversation