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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 10964 views
  • 0 likes
  • 6 in conversation