Insert leading zero in a variable

狂风中的少年 提交于 2019-12-13 00:34:24

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!