Help using Base SAS procedures

Incorrect output as result of a user-defined format

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 13
Accepted Solution

Incorrect output as result of a user-defined format

[ Edited ]

Hi experts,

 

I would like to know why I am not getting the results I expected using the following proc fcmp and proc format:

 

/**create function**/
proc fcmp outlib = work.functions.myfuncs;
function addZeros(x $) $;

sizeZ = lengthm(x)-length(x)-1; res = catt(repeat('0', sizeZ,x); return (res); endsub; run; /**create new format***/ proc format; value $addFormatTest other=[addZeros()]; run; data sample; input x $15. ; y=x; len = length(x); lenm = lengthm(x); nrZeros = lengthm(x) - length(x); format y $addFormatTest.; z=put(x, $addFormatTest.); datalines; anothertest test tes ; run;​

 

This format was supposed to add leading zeros but it is writing more zeros than it should.

 

Any Ideas?

 

Thanks a lot


Accepted Solutions
Solution
‎11-22-2016 07:14 AM
Respected Advisor
Posts: 4,644

Re: Incorrect output as result of a user-defined format

[ Edited ]

Got it to work with:

 

/**create function**/
proc fcmp outlib = work.functions.myfuncs;
function addZeros(x $) $32;
sizeZ = lengthm(x)-length(x)-1;
res = catt(repeat('0', sizeZ), x);
return (res);
endsub;
run;

options cmplib=work.functions;

/**create new format***/
proc format;
value $addFormatTest (default=32) other=[addZeros()];
run;

data sample;
 
  input x $15. ;
 
 
  y=x;
  len = length(x);
  lenm = lengthm(x);
  nrZeros = lengthm(x) - length(x);
 
  format y $addFormatTest.;
  z=put(x, $addFormatTest.);
 
datalines;
anothertest
test
tes
;

Added missing parenthesis on REPEAT function call, cmplib option, and (default=) specification in value statement of proc format. 

PG

View solution in original post


All Replies
Super User
Posts: 5,255

Re: Incorrect output as result of a user-defined format

Yeah, use z. instead.
Data never sleeps
Occasional Contributor
Posts: 13

Re: Incorrect output as result of a user-defined format

[ Edited ]

Definition found in https://support.sas.com/documentation/cdl/en/leforinforref/63324/HTML/default/viewer.htm#p09lpr3kmbh...

 

Zw.d Format

Writes standard numeric data with leading 0s.

Category:	Numeric

Alignment:	right

I want to add zeros to a character variable and I will use it as a format in SAS Information Map and in SAS Marketing Automation export definitions. 

 

Tks,

Solution
‎11-22-2016 07:14 AM
Respected Advisor
Posts: 4,644

Re: Incorrect output as result of a user-defined format

[ Edited ]

Got it to work with:

 

/**create function**/
proc fcmp outlib = work.functions.myfuncs;
function addZeros(x $) $32;
sizeZ = lengthm(x)-length(x)-1;
res = catt(repeat('0', sizeZ), x);
return (res);
endsub;
run;

options cmplib=work.functions;

/**create new format***/
proc format;
value $addFormatTest (default=32) other=[addZeros()];
run;

data sample;
 
  input x $15. ;
 
 
  y=x;
  len = length(x);
  lenm = lengthm(x);
  nrZeros = lengthm(x) - length(x);
 
  format y $addFormatTest.;
  z=put(x, $addFormatTest.);
 
datalines;
anothertest
test
tes
;

Added missing parenthesis on REPEAT function call, cmplib option, and (default=) specification in value statement of proc format. 

PG
☑ This topic is SOLVED.

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

Discussion stats
  • 3 replies
  • 272 views
  • 1 like
  • 3 in conversation