BookmarkSubscribeRSS Feed
sayanapex06
Obsidian | Level 7

i need a format to add leading zeros to alphanumeric character and numeric data and if data is 6 character it has to be same

Patrick
Opal | Level 21

@sayanapex06

If you need a format then that's what @Reeza already provided in the link she posted earlier. Have you looked into it?

 

Here a code sample based on @Reeza's code.

/*1*/
proc fcmp outlib=work.functions.myfunc;
  function leadZ(instring $) $;
    instring=left(instring);
    l=length(instring);
    if l >= 6 then
      do;
        outstring=instring;
      end;
    else 
      do;
        r=5-length(instring);
        outstring = repeat('0',r)||trim(instring);
      end;
    return (outstring);
  endsub;
run;

/*2*/
options cmplib=work.functions;

proc format;
  value $leadZ other=[leadZ()];
run;

data have;
  input a_have $char20.;
  datalines;
91
  81
aaaaaa
bbbbbb
cfg014
456asd
4kj
gh4
 12345678910
;
run;

data want;
  set have;
  a_want1=put(a_have,$leadZ20.);
  a_want2=put(a_have,$leadZ6.);
run;

 

Capture.PNG

 

Ksharp
Super User
data test;
    input A $80.;
datalines;
81
1034
91
A46
6HJ
AAAAAA
F39000DSUG08CSDF85000000
;
run;
data want;
 set test;
 length new want $ 6;
 do i=1 to length(a) by 6;
  new=substr(a,i,6);
  want=translate(right(new),'0',' ');
  output;
 end;
run;

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 17 replies
  • 20278 views
  • 1 like
  • 5 in conversation