DATA Step, Macro, Functions and more

[SAS Formats] Is it possible to create a format to add leading zeros in a character variable?

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 13
Accepted Solution

[SAS Formats] Is it possible to create a format to add leading zeros in a character variable?

[ Edited ]

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


Accepted Solutions
Solution
‎11-10-2016 06:43 AM
Super User
Posts: 17,835

Re: [SAS Formats] Is it possible to create a format to add leading zeros in a character variable?

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;

View solution in original post


All Replies
PROC Star
Posts: 551

Re: [SAS Formats] Is it possible to create a format to add leading zeros in a character variable?

Why do you wan to solve this with a format? Smiley Happy Could you not just concatenate woth a leading zero?

Occasional Contributor
Posts: 13

Re: [SAS Formats] Is it possible to create a format to add leading zeros in a character variable?

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.

PROC Star
Posts: 551

Re: [SAS Formats] Is it possible to create a format to add leading zeros in a character variable?

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;
Super Contributor
Posts: 268

Re: [SAS Formats] Is it possible to create a format to add leading zeros in a character variable?

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;

Occasional Contributor
Posts: 13

Re: [SAS Formats] Is it possible to create a format to add leading zeros in a character variable?

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,

Super User
Posts: 17,835

Re: [SAS Formats] Is it possible to create a format to add leading zeros in a character variable?

[ Edited ]

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. 

Super User
Posts: 17,835

Re: [SAS Formats] Is it possible to create a format to add leading zeros in a character variable?

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. 

 

Occasional Contributor
Posts: 13

Re: [SAS Formats] Is it possible to create a format to add leading zeros in a character variable?

[ Edited ]

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 Smiley Happy

 

Tks,

 

Super User
Posts: 17,835

Re: [SAS Formats] Is it possible to create a format to add leading zeros in a character variable?

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. 

Occasional Contributor
Posts: 13

Re: [SAS Formats] Is it possible to create a format to add leading zeros in a character variable?

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!

Super User
Super User
Posts: 6,500

Re: [SAS Formats] Is it possible to create a format to add leading zeros in a character variable?

[ Edited ]

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;

 

Solution
‎11-10-2016 06:43 AM
Super User
Posts: 17,835

Re: [SAS Formats] Is it possible to create a format to add leading zeros in a character variable?

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;
Occasional Contributor
Posts: 13

Re: [SAS Formats] Is it possible to create a format to add leading zeros in a character variable?

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!

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 13 replies
  • 450 views
  • 5 likes
  • 5 in conversation