I have an employee number field that can be anywhere from 5 to 8 alpha-characters. The field is 10 characters wide and the employee number is prefaced with leading zeros to fill the field. There may or may not be zeros in the actual employee number. I use only the EG interface to build queries and design summary reports. How can I add a computed field to trim the employee number of any leading zeros?
Thanks, in advance for your assistance!
Rita Yee
Project Engineer
FedEx Express
Then do
empnum = strip(put(input(empnum,best.),best.));
Are the employee numbers actually numbers, or can they contain any non-digit characters?
Then do
empnum = strip(put(input(empnum,best.),best.));
First solution I tried and it worked like a charm. Thanks!!!!
This thread has been helpful. I'm looking for something similar but my values contain some non digit characters. How can I modify your solution to fit that criteria?
Thanks in advance.
Please supply some examples for your character values, and what you expect as results.
@jquon126 wrote:
This thread has been helpful. I'm looking for something similar but my values contain some non digit characters. How can I modify your solution to fit that criteria?
Thanks in advance.
Then use one of the other answers that can work on strings that are not representations of numbers.
If you know that zeroes never occur except as left-hand leading characters, then the translate function works:
x=translate(x,' ','0');
Note the 2nd and 3rd arguments of translate may not be in the intutitively expected order. TRANSLATE converts the 3rd character (0) to the 2nd character (' ').
Added note. If you can have non-leading zeroes that you want to preserve:
do while (x=:'0');
x=substr(x,2);
end;
x=right(x);
If your source variable (let's call it emp_id) is numeric then just change the format on it - i.e. assign format BEST32.
If emp_id is character and you're sure it only contains digits then use an informat to convert the string to a number:
emp_id_want=input(emp_id,best32.);
If emp_id can contain alphanumeric characters then code like below should do the trick:
emp_id_want=prxchange('s/^\s*0*//oi',1,emp_id);
In SAS EG, create a calculated field--
For Char to numeric
INPUT(t1.'Ent Id Number'n,Z10.)
Convert numeric Entity ID(for example, imported from a spreadsheet) without leading zeroes to 10 Char with leading zeroes.
From Numeric to Char
PUT(t1.'Entity ID'n,Z10.)
Best of luck!
Hi, Rita
Glad to see you're persevering with SAS!
To put the advice in the above posts into an EG context, create a new computed column, and make it an advanced expression.
You can use all of the functions described above in the advanced expression. Here's another way, assuming that your fields can only contain digits. I'm using EmpID as the field name; just change as needed, and drop this into the "Enter an expression" box:
right(put(input(EmpID, best10.),best10.))
It's first using the input function to convert your character field to a number, then using the put function to convert the number back to character (the best10. format will put in leading blanks.) Then the right function shifts the non-blank characters to the right of the field.
Tom
You can use the VERIFY() function to find the location of the first non-zero character. You can then use that information in the SUBSTR() function.
data test;
input str $20. ;
new_str = substr(str,verify(str,'0'));
put (_all_) (= $quote.);
cards;
123
00ABC
;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.