Econometrics blog

Nov 4, 2014

leading zero in string variable

A typical example might be to convert the following numbers 

> to a standard five-digit number with leading zeros:
> 2 -->  00002
> 33 --> 00033
> 473 --> 00473
> 59724 --> 59724

Display with leading zeros is a matter of format:
di %05.0f 2 

A more fundamental change is: 
gen str5 newvar = string(oldvar,"%05.0f")