Hi experts!
I've been searching but I did not found anything about creating a sas format to add leading zeros in a character variable using PROC FORMAT.
The objective is to use this format whenever I want, for example, using it in SAS Marketing Automation export definitions etc.
I've tried links similar to this: http://www2.sas.com/proceedings/sugi31/243-31.pdf.
Does anyone know?
Thanks a lot!
BS
Full solution put together:
1. Define function
2. Create format
3. Apply format using format statement
4. Convert to new variable using PUT() statement
/*1*/
proc fcmp outlib=work.functions.myfunc;
function leadZ(x $) $;
xx = catt(repeat('0',6-length(x)),x);
return (xx);
endsub;
run;
/*2*/
options cmplib=work.functions;
proc format;
value $leadZ other=[leadZ()];
run;
data sample;
input x $6. ;
/*3 - for demonstration purpose*/
y=x;
format y $leadz.;
z=put(x, $leadz.); /*4*/
datalines;
test
tes
;
run;
proc print data=sample noobs label;
title 'Sample output';
label x='Raw data (x)'
y='Underlying data is raw data, format applied (y)'
z='Converted to new char variable (z)';
run;
Why do you wan to solve this with a format? 🙂 Could you not just concatenate woth a leading zero?
The objetive is to use this format whenever I want, for example, using it in SAS Marketing Automation export definitions etc.
It would be necessary to create it using PROC FORMAT so I can save the format to the catalog.
Or like this
data have;
length CharacterVar $ 5 WithZeros $ 5;
input CharacterVar;
WithZeros = put(input(CharacterVar,best5.),z5.);
datalines;
73345
6378
286
26
4
9644
94
;
run;
I don't what the range of your char var is. this will work for 2 bytes.
data have;
input id var1 $2.;
datalines;
1 8
2 9
3 10
4 99
;
run;
proc print;
run;
data want;
set have;
newvar=put(input(var1,3.),z3.);
run;
Guys,
The objetive is to use this format whenever I want, for example, using it in SAS Marketing Automation export definitions etc.
I should create it using PROC FORMAT.
Tks,
You can create a function via PROC FCMP that does the conversion and implement this via a FORMAT.
The SAS documentation for PROC FORMAT has an example of this type of implementation but converting Celsius to Fahrenheit I believe.
So the answer to your question is, yes, it's possible.
And since a character can't always be read as a number I suggest using this function instead:
Assuming length of field required is 6.
xx = cats(repeat('0',6-length(x)-1), x);
http://www.listendata.com/2016/07/sas-add-leading-zeros-to-variable.html
You do need SAS 9.3+ for this to work.
Hi Reeza,
I'm having some troubles. When I run the following program I created:
proc fcmp outlib=work.functions.myfunc;
function leadZ(x $);
xx = cats(repeat('0',6-length(x)-1),x);
return (xx);
endsub;
run;
options cmplib=work.functions;
proc format;
value $leadZ other=[leadZ()];
run;
data x;
infile datalines;
input x $leadZ.;
datalines;
test
tes
;
run;
I get the following error:
2 The SAS System 13:38 Wednesday, November 9, 2016 36 37 data x; 38 infile datalines; 39 input x $leadZ.; _______ 485 NOTE 485-185: Informat $LEADZ was not found or could not be loaded.
Do you have any idea how to fix it?
PS: It's my first time creating a function 🙂
Tks,
In proc format you create a format not an informat.
Either apply the format instead of using it as an informat or create an informat.
Im assuming you also tried FILL with a picture format.
Hi,
You're correct. I'm trying to fill with a picture format.
Now:
proc fcmp outlib=work.functions.myfunc;
function leadZ(x $);
xx = cats(repeat('0',6-length(x)-1),x);
return (xx);
endsub;
run;
options cmplib=work.functions;
proc sql;
create table x (
a VARCHAR(6),
b VARCHAR(6)
);
insert into x values("123","123");
quit;
proc print data=x;
format a $leadZ.;
run;
Now I don't get any errors, but when I check in the result generated by proc print, there's no difference when it comes to the format being applied.
Any ideas?
Tks for the help so far!
You created a FORMAT, but then you tried to use an INFORMAT that you did NOT create. Try running this code instead.
data x;
input x $6 ;
put x= $leadZ. ;
datalines;
test
tes
;
Also you forgot to include a $ in your function defintion so that SAS knows that it returns a character string.
proc fcmp outlib=work.functions.myfunc;
function leadZ(x $) $;
xx = cats(repeat('0',6-length(x)-1),x);
return (xx);
endsub;
run;
Full solution put together:
1. Define function
2. Create format
3. Apply format using format statement
4. Convert to new variable using PUT() statement
/*1*/
proc fcmp outlib=work.functions.myfunc;
function leadZ(x $) $;
xx = catt(repeat('0',6-length(x)),x);
return (xx);
endsub;
run;
/*2*/
options cmplib=work.functions;
proc format;
value $leadZ other=[leadZ()];
run;
data sample;
input x $6. ;
/*3 - for demonstration purpose*/
y=x;
format y $leadz.;
z=put(x, $leadz.); /*4*/
datalines;
test
tes
;
run;
proc print data=sample noobs label;
title 'Sample output';
label x='Raw data (x)'
y='Underlying data is raw data, format applied (y)'
z='Converted to new char variable (z)';
run;
It worked just fine!
I'll try to find a way to change the part when I explicitly define the variable length is 6.
In any case, thanks a lot!
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: