Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • First non zero integer

    Hi,

    I have a sample with 667 variables. I want to replace the observations of variable 3 till variable 666 for the first non zero digit. The sample includes numbers like 0.005 or -45.6.

    Can anyone help me? Trying it for 2 days now already.


    Vincent

  • #2
    Welcome to Statalist.

    This seems to do what you want. The key was to convert the number to a string with a format that (I hope!) ensures that the first character in the string will be nonzero. Taking the absolute value prevents the minus sign, and "e" format ("scientific notation" to Boomer programmers) is intended to have the first significant digit left of the decimal point.
    Code:
    describe, simple
    foreach var of varlist x2-x5 {
        generate byte d_`var' = real(substr(strofreal(abs(`var'),"%-9.3e"),1,1))
    }
    list, clean
    Code:
    . list, clean
    
           x1          x2          x3          x4         x5   d_x2   d_x3   d_x4   d_x5  
      1.    0   -.0700273   -.0508646    6.836784   986932.4      7      5      6      9  
      2.    0   -.0091926   -.4981204    73.49965     180984      9      4      7      1  
      3.    0   -.0448812    .4771252     96.8254   227689.7      4      4      9      2  
      4.    0   -.0867026   -.8328747   -11.71692   744834.1      8      8      1      7  
      5.    0   -.0204538   -.9097059   -63.65746   599702.2      2      9      6      6  
      6.    0    .0100857    .6076029    58.60998    55539.6      1      6      5      5  
      7.    0   -.0465563    .0217684    14.18886   184487.3      4      2      1      1  
      8.    0    -.030125    .3020099   -82.15974   918579.2      3      3      8      9  
      9.    0    .0636145     .337718   -22.57167   791856.8      6      3      2      7  
     10.    0    .0767329   -.0122538   -20.82788   26978.92      7      1      2      2

    Comment


    • #3
      Hi William,

      You are my hero! It works perfect!

      Thanks!!!

      Comment

      Working...
      X