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

HI,

I have a variable name which contains

DR JAMES KINDY

MISS JUME ALFINE

MA JOSEPH ABRAM LINKCON

HAROID KINDERSON GEORGE

 

From the abvoe line i want to remove only DR,MISS , MA and create a new Variable with Full name except the designations. 

like below:

JAMES KINDY

JUME ALFINE

JOSEPH ABRAM LINKCON

HAROID KINDERSON GEORGE

 

If I apply tranwrd fuction it will remove only last value called "MA"  not other two values, also in the same value where ever MA is it is removing from String value.

 

any best solution 

1 ACCEPTED SOLUTION

Accepted Solutions
vallsas
Pyrite | Level 9

hi kanivan,

great to see the solution , this code is working.

only thing is if two times repeats the same string DR in the begging and middile DR , need to remove the DR ony in the begging .

any idea. thank you very much ..

View solution in original post

10 REPLIES 10
Jagadishkatam
Amethyst | Level 16

Please try perl regular expression

 

data have;
infile cards missover;
input Name &$100.;
newname=strip(prxchange('s/\bDR\b|\bMISS\b|MA\b//',-1,name));
cards;
DR JAMES KINDY
MISS JUME ALFINE
MA JOSEPH ABRAM LINKCON
HAROID KINDERSON GEORGE
;
Thanks,
Jag
vallsas
Pyrite | Level 9

hi Jag,

this is excellent ..brillient..great its works.

 

I have one small concern on the same, the data also contains middle name with DR. dont want to remove this. only in the begging of the string only need to remove any clue.

 

Jagadishkatam
Amethyst | Level 16

Please try the below code

 

newname=strip(prxchange('s/\bDR\s\b|\bMISS\s\b|MA\s\b//',-1,name));

 

Also, please mark the answer which was helpful to you. I see you have marked your response as answer.

Thanks,
Jag
vallsas
Pyrite | Level 9

hi jag,

this is scripit is not working , as it was removing everying ..

Jagadishkatam
Amethyst | Level 16

I tested this script and its working fine. Please find the output I got.

 

data have;
infile cards missover;
input Name &$100.;
newname=strip(prxchange('s/\bDR\s\b|\bMISS\s\b|MA\s\b//',-1,name));
cards;
DR JAMES DR. KINDY
MISS JUME ALFINE
MA JOSEPH ABRAM LINKCON
HAROID KINDERSON GEORGE
;


OUTPUT:

Capture1.JPG

Thanks,
Jag
vallsas
Pyrite | Level 9

hi jag,

the only problem on the below code is it will not supress the DR. in the middile of the name,

where as the first record we have two DR , the begging DR removed but middile DR also should be removed.

 

as per the code we are searchign in the starting of the string any of the special characters(dr,mr,miss) and removing and some times they also apper in the middile of the name. 

kanivan51
Obsidian | Level 7

I hope it will help you

 

DATA HAVE;
INPUT NAME $20.;
DATALINES;
DR JOHN DAS
MISS ANNA RAFT
MA MIC JAG
JAGA TOOMBA YUOMBA
;
RUN;

DATA WANT;
SET HAVE;

DO;
NV=TRANWRD(NAME,'DR ','');
NV=TRANWRD(NV,'MISS ','');
NV=TRANWRD(NV,'MA ','');
IF FIND(NV,'')=1 THEN NV=SUBSTR(NV,2,LENGTH(NV));
OUTPUT;
END;

RUN;

vallsas
Pyrite | Level 9

hi kanivan,

great to see the solution , this code is working.

only thing is if two times repeats the same string DR in the begging and middile DR , need to remove the DR ony in the begging .

any idea. thank you very much ..

kanivan51
Obsidian | Level 7

Hi, vallsas!

 

DATA HAVE;
INPUT NAME $20.;
DATALINES;
DR JOHN DAS
DR JOHNDR DAS
DR JOHN DRDAS
DR JOHN DR DRDAS
MISS ANNAMISS RAFT
MISS ANNA MISSRAFT
MISS ANNA MISS RAFT
MA MIC JAG
MA MICMA JAG
MA MIC MAJAG
MA MIC MA JAG
JAGA TOOMBA YUOMBA
;
RUN;
DATA WANT;
SET HAVE;
DO;
NV=TRANWRD(NAME,'DR ','');
NV=TRANWRD(NV,'MISS ','');
NV=TRANWRD(NV,'MA ','');
IF FIND(NV,'')=1 THEN NV=SUBSTR(NV,2,LENGTH(NV));
IF SUBSTR(NAME,1,3)='DR ' THEN NV_SUBSTR=SUBSTR(NAME,4,LENGTH(NAME));
ELSE IF SUBSTR(NAME,1,5)='MISS ' THEN NV_SUBSTR=SUBSTR(NAME,6,LENGTH(NAME));
ELSE IF SUBSTR(NAME,1,3)='MA ' THEN NV_SUBSTR=SUBSTR(NAME,4,LENGTH(NAME));
ELSE NV_SUBSTR=NAME;
OUTPUT;
END;
RUN;

vallsas
Pyrite | Level 9

hi thanks for the detailed code...

by using this techiique not gettign the right soultion stil..

some how the previous solution better ..

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 10 replies
  • 1506 views
  • 1 like
  • 3 in conversation