DATA Step, Macro, Functions and more

How to remove some characters from a character string

Reply
Super Contributor
Posts: 270

How to remove some characters from a character string

Dear,

 

I need help in my code. In my data, a variable (test) contain some characters that I need to remove. Please help.

 

test

 

BMI_RAW

DIABP1_RAW

DIABP2_RAW

SYSBP2_RAW

SYSBP1_RAW

SYSBP3_RAW

 

OUTPUT NEED;

TEST1

BMI

DIABP

DIABP

SYSBP

SYSBP

SYSBP

 

MY CODE;

TEST1=strip(tranwrd(TEST, "_RAW", ""));

 

I AM GETTING OUTPUT;

BMI

DIABP1

DIABP2

SYSBP1

SYSBP2

SYSBP3

 

Please help. Thanks

 

 

 

 

Trusted Advisor
Posts: 1,128

Re: How to remove some characters from a character string

Please try compress
TEST1=compress(strip(tranwrd(TEST, "_RAW", "")),'ka');
Thanks,
Jag
Trusted Advisor
Posts: 1,128

Re: How to remove some characters from a character string

alternatively by perl regular expressions

 

data have;
input text:$100.;
if prxmatch('/[1-9]|\_\RAW/',text) then new=prxchange('s/[1-9]|\_\RAW//',-1,text);
cards;
BMI_RAW
DIABP1_RAW
DIABP2_RAW
SYSBP2_RAW
SYSBP1_RAW
SYSBP3_RAW
;
Thanks,
Jag
Regular Contributor
Posts: 239

Re: How to remove some characters from a character string

[ Edited ]

I have used Pearl regular expression function

d?_RAW   means it would check string having zero or one digit (d? ) then followed by _RAW.

 

 

data have;
input test $16.;
datalines;
BMI_RAW
DIABP1_RAW
DIABP2_RAW
SYSBP2_RAW
SYSBP1_RAW
SYSBP3_RAW
;

data want(drop=RegularExpressionId);
set have;
   RegularExpressionId = prxparse('s/\d?_RAW/ /');
   call prxchange(RegularExpressionId, -1, test );
   put test ;
run;

 

Super User
Posts: 9,662

Re: How to remove some characters from a character string

TEST1=scan(TEST, 1,'_' , 'd');
Ask a Question
Discussion stats
  • 4 replies
  • 283 views
  • 3 likes
  • 4 in conversation