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.
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?
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 🙂
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.
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.
@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
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.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.