DATA Step, Macro, Functions and more

How to print a record with leading zeros by ignoring special characters?

Reply
Regular Contributor
Posts: 168

How to print a record with leading zeros by ignoring special characters?

I've my inputs as below.

9161-642-524

9161-7642525

9167642526

642526

642

I need ouput as follows

09161

09161

91676

64252

00642

It means I need only 5 digits in output. If we've any speacial characters or the input record is less than 5 digits then we need to ignore those records and it should be produced with leading zeros.

I've tried this with substr and length function also with the format Z5. But it doesn't helped me either.

Super Contributor
Posts: 308

Re: How to print a record with leading zeros by ignoring special characters?

Hello,

Here you have one solution:

proc format;

picture frm

low-high='99999';

run;

data have (keep=varwant want) ;

length want 5 ;

input varwant $30.;

dashpos=index(varwant, "-");

if 1 <= dashpos <= 5 then want=substr(varwant,1,dashpos - 1);

else want=substr(varwant,1,5);

format want frm.;

datalines;

9161-642-524

9161-7642525

9167642526

642526

642

;


Respected Advisor
Posts: 4,173

Re: How to print a record with leading zeros by ignoring special characters?

Below is totally over the top in case this is a once-off task. If it is something you need regularly then you might consider the approach.

It is based on Base SAS(R) 9.4 Procedures Guide, Third Edition

proc fcmp outlib=work.functions.smd;

  function print_str(in_str $) $;

    r=put(input(scan(in_str,1,,'kd'),5.),z5.);

    return(r);

  endsub;

run;

options cmplib=(work.functions);

proc format;

  value $print_str (default=5) other=[print_str()];

run;

data have;

  infile datalines truncover;

  input mystring $10.;

  datalines;

9161-642-524

9161-7642525

9167642526

642526

642

;

run;

proc print data=have;

  format mystring $print_str.;

run;

Super Contributor
Posts: 340

Re: How to print a record with leading zeros by ignoring special characters?

Data A;

  Input N $;

  N5_Text=Put(Input(Substr(Scan(Compress(N),1,'-'),1,5),5.),Z5.);

  N5_Num=Input(Substr(Scan(Compress(N),1,'-'),1,5),5.); Format N5_Num Z5.;

  Datalines;

9161-642-524

9161-7642525

9167642526

642526

642

;

Run;

Respected Advisor
Posts: 4,173

Re: How to print a record with leading zeros by ignoring special characters?

Posted in reply to user24feb

I believe you could simplify your nested functions as below:

N5_Text=put(input(scan(N,1,,'kd'),5.),z5.);

N5_Text=input(scan(N,1,,'kd'),5.);

Super User
Posts: 10,048

Re: How to print a record with leading zeros by ignoring special characters?

data have;
  infile datalines truncover;
  input mystring $20.;
  datalines;
9161-642-524
9161-7642525
9167642526
642526
642
;
run;
data want;
 set have;
 length temp $ 5;
 temp=mystring;
 want=input(compress(temp,,'kd'),best.);
 format want z5.;
run;

Xia Keshan

Respected Advisor
Posts: 4,173

Re: How to print a record with leading zeros by ignoring special characters?

Just to be "picky": What about a source string like "916-7642525"?

Super User
Posts: 10,048

Re: How to print a record with leading zeros by ignoring special characters?

Not sure. Maybe I misunderstood OP's meaning .  Let OP judge it anyway .  Patrick.Matter  !

Ask a Question
Discussion stats
  • 7 replies
  • 300 views
  • 0 likes
  • 5 in conversation