Announcement

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

  • Missing values after generate command

    Hi, I am trying to make a new variable called "change" which generates the differences for variable emplystat between my observations that have values of 1,2,3. However when I use the command "gen change = d.emplystat" I keep getting blank values. How do i create this change variable so that it will only generate values if there is a difference (say > 0). I have tried using an "if" command but i can't get it still. Thanks in advance, would appreciate any help.

  • #2
    Julia:
    welcome to ths forum.
    Why not going back to basics instead of using time-series operators?
    Code:
    . set obs 3
    Number of observations (_N) was 0, now 3.
    
    . g var1=_n
    
    . g var2=0
    
    . g wanted= var1- var2
    
    . list
    
         +----------------------+
         | var1   var2   wanted |
         |----------------------|
      1. |    1      0        1 |
      2. |    2      0        2 |
      3. |    3      0        3 |
         +----------------------+
    
    .
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      Hi Carlo, thanks for the reply! However, in this case what I'm trying to do is to find the difference for the same variable but different observations, i.e., for var1 but line 2-line 1, and so on.

      Comment


      • #4
        Data example please. https://www.statalist.org/forums/help#stata

        Comment


        • #5
          Here is an example, mergeid is the unique person's identifier and I'm trying to find the difference for emplystat - so that I will know when there is a change in the person's employment status (i.e. from employed to retired, which I have already coded them to be integers).
          Click image for larger version

Name:	Screenshot 2023-02-21 at 5.23.54 PM.png
Views:	1
Size:	141.9 KB
ID:	1702692

          Comment


          • #6
            Please use dataex and not a screenshot. The link in #4 explains why we ask this. In any case isn't there a time variable too? (I don't think we need to see the country variable.)

            Comment


            • #7
              Julia:
              as an aside to Nick's helpful guidance, youmay want to consider something along the folllowing lines (please note that the last within-panel value of -wanted- is always missing because the calculation stops due to -bysort.):
              Code:
              . use "https://www.stata-press.com/data/r17/nlswork.dta"
              (National Longitudinal Survey of Young Women, 14-24 years old in 1968)
              
              . bysort idcode: gen wanted= msp[_n+1] - msp[_n]
              
              . list idcode year msp wanted if idcode==1
              
                     +------------------------------+
                     | idcode   year   msp   wanted |
                     |------------------------------|
                  1. |      1     70     0        1 |
                  2. |      1     71     1        0 |
                  3. |      1     72     1        0 |
                  4. |      1     73     1        0 |
                  5. |      1     75     1       -1 |
                     |------------------------------|
                  6. |      1     77     0        0 |
                  7. |      1     78     0        0 |
                  8. |      1     80     0        0 |
                  9. |      1     83     0        0 |
                 10. |      1     85     0        0 |
                     |------------------------------|
                 11. |      1     87     0        0 |
                 12. |      1     88     0        . |
                     +------------------------------+
              Kind regards,
              Carlo
              (Stata 19.0)

              Comment


              • #8
                Hi Carlo, I think that worked! Thank you so much

                Comment

                Working...
                X