问题
I have the following problem:
I would like to insert a leading zero in the values of a variable holding different
customer identification numbers. I have already tried to use the generate
command to add the zero, but this did not work.
How can i do that in Stata?
My variable looks as follows:
567
523
598
679
回答1:
You do not specify if your variable is of string or numeric type.
If it is the former, you simply need to insert the zero as follows:
clear
input str3 customer_id
"567"
"523"
"598"
"679"
end
generate new_customer_id = "0" + customer_id
list
+---------------------+
| custom~d new_cu~d |
|---------------------|
1. | 567 0567 |
2. | 523 0523 |
3. | 598 0598 |
4. | 679 0679 |
+---------------------+
If it is the latter, you just need to format the variable accordingly:
clear
input customer_id
567
523
598
679
end
format customer_id %04.0f
list
+----------+
| custom~d |
|----------|
1. | 0567 |
2. | 0523 |
3. | 0598 |
4. | 0679 |
+----------+
来源:https://stackoverflow.com/questions/50836959/insert-leading-zero-in-a-variable