BookmarkSubscribeRSS Feed
woo
Lapis Lazuli | Level 10 woo
Lapis Lazuli | Level 10

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

8 REPLIES 8
Reeza
Super User

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

Astounding
PROC Star

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?

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

woo
Lapis Lazuli | Level 10 woo
Lapis Lazuli | Level 10

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

Astounding
PROC Star

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.

user24feb
Barite | Level 11

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;

Haikuo
Onyx | Level 15

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

;

Ksharp
Super User
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

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