Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

RE: st: concatenate, leading zeros


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   RE: st: concatenate, leading zeros
Date   Tue, 23 Mar 2004 10:07:22 -0000

I am unclear what Daphna is doing here. 

A format like "%02s" can only be applied to 
string variables. In my experiments, it has 
the effect of inserting leading spaces in the 
display, not leading zeros. (Or what I am doing wrong, 
or misunderstanding?) 

However, the display format as set by -format- 
has no effect on the values of a variable. 

Hugh's advice, which we can revise to 

gen str14 newvar = 
	string(var1,"%02.0f") + 
	string(var2,"%05.0f") + 
	string(var3,"%07.0f")

is on the contrary appropriate to _numeric_
variables. 

Note that -egen, concat()- won't work here, 
as it is crucial that different formats are used. 

Nick
n.j.cox@durham.ac.uk 

Hugh Davies replied to Daphna Bassok 

Try:

Gen str14 newvar = string(var1,"%02.0f") + string(var1,"%05.0f") + string(var1,"%07.0f")
 
> I need to concatenate three variables into 1, and all three may have 
> leading zeros.
> Whenever I concatenate, the leading zero's are dropped.
> 
> To be clear...
> 
> If I have:
> 
> var1=01
> var2=04956
> var3=0906730
> 
> I would like to get a variable back that is: 01049560906730
> 
> This is not what I am getting: instead I am getting 14956906730.
> 
> I have formatted all my variables as string with the appropriate 
> formats.  For instance, for my variable 1, I have done format var1 %02s.
> 
> They all look fine, and retain their leading zero's individually, but not 
> once I concatenate.

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index