03-09-2018 01:15 PM
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!
03-12-2018 07:07 PM
03-09-2018 01:29 PM
03-12-2018 03:44 PM
03-12-2018 07:07 PM
03-12-2018 11:25 PM
04-10-2018 10:53 AM
First solution I tried and it worked like a charm. Thanks!!!!
03-09-2018 01:36 PM - edited 03-09-2018 08:45 PM
If you know that zeroes never occur except as left-hand leading characters, then the translate function works:
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);
03-09-2018 05:29 PM
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:
If emp_id can contain alphanumeric characters then code like below should do the trick:
03-10-2018 10:20 AM
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:
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.
03-10-2018 10:39 AM
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 ;
Need further help from the community? Please ask a new question.