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
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;
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;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.