DATA Step, Macro, Functions and more

How to remove some characters from a character string

Reply
Super Contributor
Posts: 272

How to remove some characters from a character string

Dear,

 

In my data, I need remove some characters from a variable (test) values. With my code I am not getting output I need for ONE observation.

Please look at OBS 4 (test=EXCL08C). i AM NOT GETTING THE OUTPUT I NEED FOR THIS.

Please help.

 

data

test

EXCL01

EXCL07B

EXCL08

EXCL08C

EXCL10

 

MY CODE;

if test =:'EXCL0' THEN TEST1=COMPRESS(TEST,'XCL0','');
ELSE IF test ^=:'EXCL0' THEN TEST1=COMPRESS(TEST,'XCL','');

 

RESULT;

E1

E7B

E8

E8

E10

 

OUTPUT NEED;

E1

E7B

E8

E8C

E10

 

 

Super User
Posts: 19,875

Re: How to remove some characters from a character string

Posted in reply to knveraraju91

You may be better off using a SUBSTR() function?

 

Looks like you're taking first character and then everything after the 4th?

 

 

Super User
Posts: 11,343

Re: How to remove some characters from a character string

Posted in reply to knveraraju91

I'm not sure this will easily extend if needed but works for the example data;

data example;
   informat test $10.;
   input test;
   result = tranwrd(test,'EXCL0','E');
   result = tranwrd(Result,'EXCL','E');
datalines;
EXCL01
EXCL07B
EXCL08
EXCL08C
EXCL10
;
Super User
Posts: 10,046

Re: How to remove some characters from a character string

Posted in reply to knveraraju91

data example;
   informat test $10.;
   input test;
   result = prxchange('s/XCL0*//i',-1,test);
datalines;
EXCL01
EXCL07B
EXCL08
EXCL08C
EXCL10
;
run;
Ask a Question
Discussion stats
  • 3 replies
  • 280 views
  • 4 likes
  • 4 in conversation