Desktop productivity for business analysts and programmers

Drop leading zeros from character variable

Accepted Solution Solved
Reply
Contributor
Posts: 41
Accepted Solution

Drop leading zeros from character variable

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

___________________________________
The fact that I ask for help simply means I am inexperienced and under-educated. Please don't assume I am incompetent and uneducated.

Accepted Solutions
Solution
‎04-10-2018 10:53 AM
Super User
Posts: 10,601

Re: Drop leading zeros from character variable

Then do

empnum = strip(put(input(empnum,best.),best.));
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code

View solution in original post


All Replies
Super User
Posts: 10,601

Re: Drop leading zeros from character variable

Are the employee numbers actually numbers, or can they contain any non-digit characters?

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Contributor
Posts: 41

Re: Drop leading zeros from character variable

Posted in reply to KurtBremser
They are only numbers.


___________________________________
The fact that I ask for help simply means I am inexperienced and under-educated. Please don't assume I am incompetent and uneducated.
Solution
‎04-10-2018 10:53 AM
Super User
Posts: 10,601

Re: Drop leading zeros from character variable

Then do

empnum = strip(put(input(empnum,best.),best.));
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Super User
Posts: 8,287

Re: Drop leading zeros from character variable

Posted in reply to KurtBremser
Better to use 32. as the INFORMAT and 32. as the FORMAT.
The BEST format will default to 12 characters and larger integers will be presented in scientific format. Since the values are by definition integers then just tell SAS that. 32 is the maximum width.
Also for informats there is no real BEST informat. If you use BEST as an informat is just an **alias** for the normal NN. informat. Calling it BEST, as if it did something different, will just confuses novice SAS users into thinking that it is somehow going to do something different than the normal numeric informat.
Contributor
Posts: 41

Re: Drop leading zeros from character variable

Posted in reply to KurtBremser

First solution I tried and it worked like a charm.   Thanks!!!!

___________________________________
The fact that I ask for help simply means I am inexperienced and under-educated. Please don't assume I am incompetent and uneducated.
Trusted Advisor
Posts: 1,399

Re: Drop leading zeros from character variable

[ Edited ]

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);

 

 

Respected Advisor
Posts: 4,797

Re: Drop leading zeros from character variable

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);

PROC Star
Posts: 1,334

Re: Drop leading zeros from character variable

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

Super User
Super User
Posts: 8,287

Re: Drop leading zeros from character variable

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
;
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 9 replies
  • 595 views
  • 0 likes
  • 6 in conversation