BookmarkSubscribeRSS Feed
Hhh111
Calcite | Level 5

 

Hi All,

 

I am having difficulties on leading zeros using Z format. Here is the scenario

 

Table A

amount = 100;

 

So what i did is :

 

amount = amount * 100;

amount_new = put(amount, z7.);

 

the output turn out to be expected, eg. 0010000...but, the format is numeric. I know i can convert it to character, but my assumption is by using Z format, the output is in character already. 

 

Please advise. 

 

 

6 REPLIES 6
PeterClemmensen
Tourmaline | Level 20

apologies for my confusion: Do you want the new variable to be numeric? Also, are you sure you want a new variable and not just format the old one?

Hhh111
Calcite | Level 5

I want the final output to be in character. meaning, from normal numeric, use Z format to add leading zeros and the output change to character. 

 

The reason why i create the new variable is because to see the changes. But in real production, i will use the same name 🙂

PeterClemmensen
Tourmaline | Level 20

What is the logic behind multiplying the amount by 100?

 

Also, when you use the PUT Function, the returned value is always character. 

 

So, doing this

 

data test;
    amount = 100;
    newAmount=put(Amount, z7.);
run;

makes NewAmount character. Not numeric.

Hhh111
Calcite | Level 5

the actual amount is having decimal

so for this case, the value is 100.00 (thats the reason why i * with 100). And the original format for this amount is 19.2.

 

after use PUT function, it does not have leading zeros and the format still numeric. so i guess, somehow the put function and z format is not functioning here. Must be i missed something. 

 

 

 

Kurt_Bremser
Super User

@Hhh111 wrote:

I want the final output to be in character. meaning, from normal numeric, use Z format to add leading zeros and the output change to character. 

 

The reason why i create the new variable is because to see the changes. But in real production, i will use the same name 🙂


See this:

data have;
amount = 100;
run;

data want;
set have;
_amount = put(amount*100,z7.);
drop amount;
rename _amount=amount;
run;

proc print data=want noobs;
run;

Result:

amount

0010000
Tom
Super User Tom
Super User

You use a FORMAT to convert values to character.  SAS will use the format attached to a variable when PRINTING the value so that humans can read it. In general if no format is explicitly attached then the BEST format will be used. 

 

If you want to create a character variable with the formatted value of a variable you use the PUT() or PUTN() function.  Or if the original variable is character and you are using a character format (format name starts with a $) then you could use the PUT() or PUTC() function.  You could also use the VVALUE() or VVALUEX() function to get the formatted value of a variable.

 

How are you LOOKING at the values where the leading zeros are disappearing?  Are you using something like Excel?  Excel has a very nasty habit of transforming data when it reads it in from non-Excel binary formats. Like from CSV files.

SAS Innovate 2025: Call for Content

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!

Submit your idea!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 6 replies
  • 15557 views
  • 0 likes
  • 4 in conversation