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!
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;
Are all your values prefixed by "S"?
Do you want that list in saved in a dataset-variable? Can you describe what the next steps are?
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;
Yess! This will do. Thanks so much for your help!
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.