Desktop productivity for business analysts and programmers

length issue

Reply
Super Contributor
Super Contributor
Posts: 318

length issue

Hi all

please help me to define length for this CHAR variable.

I have char column name "reason_code" which has different length on it and i want to make it 10 for all values.

Have

reason_code

4567898723 /*not changing*/

078654367890 /*removing zeros from beginning and from end*/

09567894563212 /*removing initial 4 digit to make it 10 */

want

reason_code

4567898723

7865436789

7894563212

Thank you

Grand Advisor
Posts: 17,355

Re: length issue

Are those all the situations you can have?  What 0's in the middle?

Respected Advisor
Posts: 4,976

Re: length issue

What would you like to do, when given a choice of removing leading or trailing 0's?  For example:

0000123456780000

Which characters get removed and which stay?

Esteemed Advisor
Esteemed Advisor
Posts: 7,217

Re: length issue

Just a thought, would you not be better/safer padding up rather than truncated down?  I.e. choose a rule, if length < 20 then repeat('0',20-length)||value.

Super Contributor
Super Contributor
Posts: 318

Re: length issue

I am trying to make unique length that is 10. i have these three unique situation where i need to make changes.

First case: where length is 10 and no need to change anything

4567898723

Second case: Need to remove leading and trailing zeros to make it 10 digit/length

078654367890   to   7865436789

Third case: Need to remove leading 4 digit to make it 10 digit/length

09567894563212    to   7894563212

Thanks all for your response

Respected Advisor
Posts: 4,976

Re: length issue

OK, here's one approach:

data want;

   set have;

   do while (length(reason_code) > 10 and substr(reason_code, length(reason_code), 1)='0');

      substr(reason_code, length(reason_code), 1) = ' ';

   end;

   if length(reason_code) <= 10 then return;

   do until (length(reason_code)=10);

      reason_code = substr(reason_code, 2);

   end;

run;

The bottom loop doesn't have to check for zeroes.  It removes any character it finds until the string becomes short enough.

Good luck.

Super Contributor
Posts: 336

Re: length issue

Maybe:

Data Have;
  Input reason_code $20.;
Datalines;
4567898723
078654367890
09567894563212
;
Run;

Data Want;
  Set Have;
  If Length (reason_code) gt 10 Then Do;
    reason_code=PRXChange("s/^^0*//",1,Reverse(PRXChange("s/^^0*//",1,Compress(Reverse(reason_code)))));
    If Length (reason_code) gt 10 Then reason_code=Reverse(Substr(Reverse(Compress(reason_code)),1,10));
  End;
Run;

Respected Advisor
Posts: 3,124

Re: length issue

data have;

infile cards truncover;

input var $ 100.;

new_var=prxchange('s/(\d*)([1-9]\d{8}[1-9])(0*)$/$2/oi', -1, strip(var));

cards;

4567898723

078654367890

09567894563212

;

Grand Advisor
Posts: 9,578

Re: length issue

data have;
infile cards truncover;
input var $ 100.;
temp=prxchange('s/0+$//o', -1, strip(var));
new_var=substr(temp,length(temp)-9) ;
cards;
4567898723
078654367890
09567894563212
;
run;

Xia Keshan

Ask a Question
Discussion stats
  • 8 replies
  • 696 views
  • 1 like
  • 7 in conversation