BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
RPYee
Quartz | Level 8

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.
1 ACCEPTED SOLUTION
13 REPLIES 13
RPYee
Quartz | Level 8
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.
Tom
Super User Tom
Super User
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.
RPYee
Quartz | Level 8

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.
jquon126
Calcite | Level 5

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.

Tom
Super User Tom
Super User

@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.

mkeintz
PROC Star

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

 

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Patrick
Opal | Level 21

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

JSJ
Quartz | Level 8 JSJ
Quartz | Level 8

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!

 

 

TomKari
Onyx | Level 15

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

Tom
Super User Tom
Super User

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
;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

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.

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
  • 13 replies
  • 27960 views
  • 1 like
  • 8 in conversation