BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
bsas94
Obsidian | Level 7

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

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

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

3 REPLIES 3
LinusH
Tourmaline | Level 20
Yeah, use z. instead.
Data never sleeps
bsas94
Obsidian | Level 7

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,

PGStats
Opal | Level 21

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

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 3 replies
  • 1583 views
  • 1 like
  • 3 in conversation