Add trailing zeros to character data

Reply
Occasional Contributor
Posts: 12

Add trailing zeros to character data

I had such a wonderful response to add leading zeros to data that I am now asking for help in adding training zeros.

Any suggestions would be appreciated.

Super Contributor
Posts: 282

Re: Add trailing zeros to character data

Hi,

It would help to provide examples of what you have and what you'd like it to change to.

Is this character or numeric data?

Regards,

Amir.

Occasional Contributor
Posts: 12

Re: Add trailing zeros to character data

It originally was numeric and I converted the data to character data. I was able to add a leading zero by doing this but I also need to add a trailing zero as well.

example:  '3' to '030'

Super Contributor
Posts: 282

Re: Add trailing zeros to character data

Hi,

Consider either multiplying the numeric by 10 before converting it to character, or when it is character consider using the cats() function, e.g.:

var2=cats(var1,'0');

Regards,

Amir.

Super User
Posts: 5,083

Re: Add trailing zeros to character data

You might want to consider adding both the leading and trailing zero in one step:

charvar = put(numvar * 10, z3.);

Frequent Contributor
Posts: 102

Re: Add trailing zeros to character data

I have a similar problem.  When I merge number with decimals, the decimals that are zero get cut off.  For example, merging 2.500 and 6.050 would produce 2.5-6.05.  How do I get the program to stop cutting off the zeroes and output 2.500-6.050?

Super User
Posts: 10,500

Re: Add trailing zeros to character data

The values aren't cut off it is a result of internal numeric storage.

You don't show how you have attempted to create your 2.500 - 6.050 but the likeliest answer is going to involve a format that displays at a given precision.

For example a value of 2.5 will display at 2.500 when using something like F6.3 which forces every appearance to show 3 values to the right of a decimal point.
One way to create a string of your example

data _null_;

     x= 2.5;

     y= 6.05;

     result = catx(' - ',put(x,7.3),put (y,7.3));

     put result;

run;

Ask a Question
Discussion stats
  • 6 replies
  • 6422 views
  • 0 likes
  • 5 in conversation