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!
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and save with the early bird rate—just $795!
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.
Ready to level-up your skills? Choose your own adventure.