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

st: gen strN var2=string(var1, "%fmt") does not produce leading zeros


From   Daniel Egan <[email protected]>
To   [email protected]
Subject   st: gen strN var2=string(var1, "%fmt") does not produce leading zeros
Date   Fri, 15 Jul 2005 14:40:38 -0400

Hello all, 

I hope this one gets in under the wire....

I am working with USA National Drug Codes (NDCs). The raw format I
have reads them in as a number between 7 and 12 digits long.

Actual NDC codes are (usually) of the form 12345-1234-12, i.e. 5-4-2
digits, for a total of 13 inclusive of the two dashes.

So any numeric format with less than 11 digits is actually that number
with leading zeros.

I have read the FAQ at
http://www.stata.com/support/faqs/data/leading0.html , and followed
the directions, but for some reason the resulting string does not have
leading zeroes.

my code runs as follows: 
****************************
format ndc %12.0f
gen str12 sndc=string(ndc, "%12.0f")
gen sndclen=length(sndc)
tab sndclen, missing

  ndclength |      Freq.     Percent        Cum.
------------+-----------------------------------
          7 |    559,577       10.42       10.42
          8 |  1,843,485       34.32       44.74
          9 |  1,917,644       35.70       80.44
         10 |        203        0.00       80.44
         11 |  1,047,475       19.50       99.94
         12 |      3,260        0.06      100.00
------------+-----------------------------------
      Total |  5,371,644      100.00
*********************************

The 12-digit figures are actually missings (all 9's), so I recoded
them, reverted via "real", and then re-converted with -string-
again...

*********
replace sndc="" if sndclen==12
gen ndc2=real(sndc)
gen str11 sndc2=string(ndc2, "%11.0f")
***********

This does not return leading zero's either. 

Any suggestions? 

TIA, 

Daniel Egan



****************
Stata/SE 9.0 for Windows
Born 05 Jul 2005
Total physical memory:     2097151 KB
Available physical memory: 1425028 KB

Stata executable
    folder:               C:\Program Files\Stata9\
    name of file:         wsestata.exe
    currently installed:  05 Jul 2005

Ado-file updates
    folder:               C:\Program Files\Stata9\ado\updates\
    names of files:       (various)
    currently installed:  05 Jul 2005
******************

*
*   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–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index