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

Hi,

Please help to me get "modified name" from "NAme". String length is varies

NameModifiedName
ABCDABCD
GHIJ0000GHIJ
RRJJZL0244RRJJZL
cdds_3cdds
ABSD0122ABSD
NNN0111NNN
ABCD0000ABCD
1 ACCEPTED SOLUTION

Accepted Solutions
shivas
Pyrite | Level 9

Hi Rajesh,

Try this..Hope it helps..

data have;

input name $ 20.;

modified = compress(name,'0123456789_');

cards;

ABCD

GHIJ0000

RRJJZL0244

cdds_3

ABSD0122

NNN0111

ABCD0000

;

run;

Thanks,

Shiva

View solution in original post

6 REPLIES 6
Alpay
Fluorite | Level 6

Can you specify the rules (chars to keep/exclude)? e.g.: Are you trying to keep characters A to Z and a to z only?

RajeshPU
Calcite | Level 5

Hi Alpay,

Yes. thats correct. i'm trying to keep only the characters

Thanks for simplifyng the question

shivas
Pyrite | Level 9

Hi Rajesh,

Try this..Hope it helps..

data have;

input name $ 20.;

modified = compress(name,'0123456789_');

cards;

ABCD

GHIJ0000

RRJJZL0244

cdds_3

ABSD0122

NNN0111

ABCD0000

;

run;

Thanks,

Shiva

Keith
Obsidian | Level 7

You could simplify the compress function to keep just the letters A-Z,a-z, rather than remove non-letters.

modified = compress(name,,'ka');

Alpay
Fluorite | Level 6

You may utilize regular expressions as well.

It will substitute null string for characters not in the range 'a' to 'z' (a-z) and 'A' to 'Z' (A-Z).

data x;

input Name $20.;

NewName = PRXCHANGE('s/[^a-zA-Z]//o',-1,Name);

datalines;

ABCD

GHIJ0000

RRJJZL0244

cdds_3

ABSD0122

NNN0111

ABCD0000

;

run;

RajeshPU
Calcite | Level 5

Thank you Shiva, that works

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 6 replies
  • 642 views
  • 0 likes
  • 4 in conversation