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
BID= input(substr(AID,3,3) ,32.);
Input ignoring the leading zeroes issue. SAS will remove them automatically.
But what function should i need to use to get BID from AID
BID= input(substr(AID,3,3) ,32.);
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;
With regular expression processing:
length BID $8;
BID = prxchange("s/..0*(\S+)/\1/o", 1, AID);
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)
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.
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.