Desktop productivity for business analysts and programmers

EG code, removing the "DR", MISS" FROM A SAS VARIABLE

Accepted Solution Solved
Reply
Contributor
Posts: 62
Accepted Solution

EG code, removing the "DR", MISS" FROM A SAS VARIABLE

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 


Accepted Solutions
Solution
‎05-28-2017 01:08 AM
Contributor
Posts: 62

Re: EG code, removing the "DR", MISS" FROM A SAS VARIABLE

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


All Replies
Super User
Posts: 1,109

Re: EG code, removing the "DR", MISS" FROM A SAS VARIABLE

[ Edited ]

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
Contributor
Posts: 62

Re: EG code, removing the "DR", MISS" FROM A SAS VARIABLE

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.

 

Super User
Posts: 1,109

Re: EG code, removing the "DR", MISS" FROM A SAS VARIABLE

[ Edited ]

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
Contributor
Posts: 62

Re: EG code, removing the "DR", MISS" FROM A SAS VARIABLE

hi jag,

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

Super User
Posts: 1,109

Re: EG code, removing the "DR", MISS" FROM A SAS VARIABLE

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
Contributor
Posts: 62

Re: EG code, removing the "DR", MISS" FROM A SAS VARIABLE

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. 

Occasional Contributor
Posts: 13

Re: EG code, removing the "DR", MISS" FROM A SAS VARIABLE

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;

Solution
‎05-28-2017 01:08 AM
Contributor
Posts: 62

Re: EG code, removing the "DR", MISS" FROM A SAS VARIABLE

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 ..

Occasional Contributor
Posts: 13

Re: EG code, removing the "DR", MISS" FROM A SAS VARIABLE

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;

Contributor
Posts: 62

Re: EG code, removing the "DR", MISS" FROM A SAS VARIABLE

hi thanks for the detailed code...

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

some how the previous solution better ..

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 10 replies
  • 189 views
  • 1 like
  • 3 in conversation