Announcement

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

  • When stata's gen variable doesn't output through the numbers I specify

    Dear STATALIST Community Members,
    Can you please take the time to solve my problem?


    The value of the variable code is a 14-digit value and is the region code plus the year and month. For example, 11230560/2019/06.

    gen double code_w = code*10 + week_day
    format %20.0g code_w

    gen double code_t = code*100 + week_day*10 + de_hr_grp
    format %20.0g code_t

    /*
    The variable week_day has values 1 and 2 for weekday and weekend, respectively.
    The variable de_hr_grp stands for hours (6-12-12-18-18-24) and has values 1,2,3, respectively.
    */

    gen item=1 if newv1==1
    replace item=2 if new3==2
    replace item=3 if new3==7
    replace item=4 if new3==12
    replace item=5 if new3==23

    *The variable item is the item I specified and I assigned the values 1,2,3,4,5.

    *So, the last time I typed the command below, the end numbers of the output should be 1 through 5. However, my output only shows 2 and 6. What is the problem ?

    gen double code_i = code_t*10 + item
    format %20.0g code_i


    *results of command 'tab code_i'


    1.17e+16 | 114 0.00 99.96
    1.17e+16 | 160 0.00 99.96
    1.17e+16 | 42 0.00 99.96
    1.17e+16 | 1 0.00 99.96
    1.17e+16 | 5 0.00 99.96
    1.17e+16 | 717 0.01 99.98
    1.17e+16 | 121 0.00 99.98
    1.17e+16 | 158 0.00 99.98
    1.17e+16 | 796 0.02 100.00
    1.17e+16 | 33 0.00 100.00
    1.17e+16 | 37 0.00 100.00
    ------------+-----------------------------------
    Total | 5,062,296 100.00


    Thank you

  • #2
    Code:
    11230560/2019/06
    could be held in a string variable -- str14 or upwards -- but if you use an expression like that to generate a numeric variable Stata will treat the slashes as division operators. That can be seen from

    Code:
    . di 11230560/2019/06
    927.07281
    Other way round, #1 implies that you have a numeric variable code -- but whatever it's holding is not what you expect it to hold. Possibly you encoded a string to get that, but the results won't allow useful calculations.

    I could make other guesses but that is unlikely to be productive here. We need please a dataex based on

    Code:
    dataex code week_day de_hr_grp
    What's more: you don't explain newv1 or new3 ,

    In sum, you're over-estimating our powers to tell you what is going wrong.

    Please back up and read and act on https://www.statalist.org/forums/help#stata

    Comment

    Working...
    X