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

Hello, all:

 

I would like to remove the last number from the ID column.  I have 300 ID names which need to do this repeatly.  Please let me know how, thanks.

 

data state;

input ID $;

cards;

TN1

TN2

TN3

UCH1

UCH2

UCH3

UCH4

UCH5

ICFG1

ICFG2

ICFG3

ICFG4

ICFG5

ICFG6

;

run;

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

find a digit, searching from right to left. And, as I think about it, the code I suggested should actually have been:

data want;
  set state;
  if findc(id,,'db') eq length(id) then id=substr(id,1,findc(id,,'db')-1);
run;

i.e., only remove the last character .. if it is a digit.

 

Art, CEO, AnalystFinder.com

 

View solution in original post

11 REPLIES 11
Shmuel
Garnet | Level 18

There are several ways to remove the numbers:

data state;
input ID $;
/*1*/ id = translate(id,'','0123456789');
/*2*/ id = compress(id,'0123456789');
/*3*/ id = compress(id,' ','D'); cards; ... your data ... run;
RW9
Diamond | Level 26 RW9
Diamond | Level 26

You could also do:

id = compress(id,,'ka');

K=Keep, A=Alpha.

http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000212246.htm

 

Might be a bit shorted if you had other things as there are a few options in compress.

Jagadishkatam
Amethyst | Level 16
data state;
input ID $;
new=prxchange('s/\d$/ /',-1,strip(id));
cards;
TN1
TN2
TN3
UCH1
UCH2
UCH3
UCH4
UCH5
ICFG1
ICFG2
ICFG3
ICFG4
ICFG5
ICFG6
;
run;
Thanks,
Jag
art297
Opal | Level 21

If you only want to remove one digit if it exists at the end of an id you could use:

data want;
  set state;
  if findc(id,,'db') then id=substr(id,1,findc(id,,'db')-1);
run;

Art, CEO, AnalystFinder.com

 

ybz12003
Rhodochrosite | Level 12

What is the 'db' refering?

art297
Opal | Level 21

find a digit, searching from right to left. And, as I think about it, the code I suggested should actually have been:

data want;
  set state;
  if findc(id,,'db') eq length(id) then id=substr(id,1,findc(id,,'db')-1);
run;

i.e., only remove the last character .. if it is a digit.

 

Art, CEO, AnalystFinder.com

 

lalohg
Quartz | Level 8

HI Art,

could you please let me know how to remove entries with at least one number (any number) of a character variable?

lalohg
Quartz | Level 8

HI Art,

could you please let me know how to remove entries with at least one number (any number) of a character variable?

 

Thanks

Lalo

art297
Opal | Level 21

Just use the anydigit function. i.e.,

if anydigit(varname) then call missing(varname);

Art, CEO, AnalystFinder.com

 

lalohg
Quartz | Level 8

Thank you very much

 

Have a good day.

 

Lalo.

ybz12003
Rhodochrosite | Level 12

Thanks for your kind help.

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
  • 11 replies
  • 25383 views
  • 7 likes
  • 6 in conversation