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

Hi,

I Have a character variable with comes with leading zeroes and my desired result should be numeric with removal of leading zeros

and substr the values from 3rd position  BID= Substr(AID,3,3)

AID
 00000

 000001

 000011

000100

I20800


my desired result:
BID
 0
 1

11

100

800

 

Can anyone pls help

1 ACCEPTED SOLUTION
6 REPLIES 6
Reeza
Super User

Input ignoring the leading zeroes issue. SAS will remove them automatically. 

chennupriya
Quartz | Level 8

But what function should i need to use to get BID from AID

Tom
Super User Tom
Super User

The INPUT() function can convert a character string to a new value by applying the INFORMAT that you choose.

But you will need to explain why you want to convert the last string into the number 800. Why did you ignore the letter 'I' at the beginning of the string?  Why not 20,800 instead of 800?

 

data have ;
  input AID $char12. ;
cards;
 00000
 000001
 000011
000100
I20800
;

data want ;
 set have ;
 bid = input(aid,12.);
run;

 

PGStats
Opal | Level 21

With regular expression processing:

 

length BID $8;
BID = prxchange("s/..0*(\S+)/\1/o", 1, AID);
PG
Kurt_Bremser
Super User

You should really use the {i} symbol to post data, as this uses a fixed-width font and preserves correct spacing. As it is, your example data does not match your stated requirements.

Per your requirements,

data want;
set have;
bid = input(substr(aid,4,3),3.);
run;

should do the trick.

(getting the last 3 characters from a 6-character string requires starting at position 4)

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 11328 views
  • 0 likes
  • 6 in conversation