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

Hi all!

I have a data set with a bunch of IDs as string variable like eg.below. I want to delete all the characters from "(" and include only the numbers before "(" for each ID. Any help with SAS code is much appreciated.

 

Thanks!

 

ID

48 (500_82)

49 (501_82)

 

Want:

ID_New

48

49

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

Hi @sms1891  Perhaps you could try scan knowing the separator '(' would of course be the first to split the string

 

data have;
input id $20.;
cards;
48 (500_82)
49 (501_82)
40
;

data want;
set have;
want=scan(id,1,'(');
run;

View solution in original post

4 REPLIES 4
PeterClemmensen
Super User
data have;
input ID $20.;
infile datalines dlm='';
datalines;
48 (500_82)
49 (501_82)
;

data want;
   set have;
   ID_New=substr(ID, 1, find(ID, '(')-1);
run;
novinosrin
Tourmaline | Level 20

If your pattern is not so consistent with some records not having the special character like the 3rd in the modified sample, the following should work

 

data have;
input id $20.;
cards;
48 (500_82)
49 (501_82)
40
;

data want;
set have;
if find(id,'(')>0 then want=substr(id,1,find(id,'(')-1);
else want=id;
run;

 

novinosrin
Tourmaline | Level 20

Hi @sms1891  Perhaps you could try scan knowing the separator '(' would of course be the first to split the string

 

data have;
input id $20.;
cards;
48 (500_82)
49 (501_82)
40
;

data want;
set have;
want=scan(id,1,'(');
run;
anamika1_
Calcite | Level 5
data one;
set have;
string1=substr(id,1,2);
run;

SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

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

View all other training opportunities.

Discussion stats
  • 4 replies
  • 53731 views
  • 4 likes
  • 4 in conversation