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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

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
  • 17 replies
  • 16772 views
  • 1 like
  • 5 in conversation