Announcement

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

  • Subtracting Variables Stata

    Dear Stata experts,

    I have a question regarding a subtracting. My data looks like this:

    A . B . C
    x | x
    x | x
    x | x
    x | x
    x | x
    x | 4
    x | x
    5 | x
    x | x
    x | 2
    x | x
    3 | x
    x | 1
    x | x
    2 | x

    I need to get C = A-B but the 'x" are empty cells.
    I need: 5-4 = 1, 3-2=1 & 2-1=1 in the C column

    Thanks in advance,

    Jeff

  • #2
    Hello Jeffrey,

    Welcome to the Stata Forum.

    Please present the data under CODE delimiters or use the SSC - dataex - , as recommended in the FaQ.

    That said, I did not get your point in terms of presenting the "rule" so as Stata performs the estimations.

    Best,

    Marcos
    Best regards,

    Marcos

    Comment


    • #3
      Please do read FAQ Advice #12 on presenting data examples that can be read directly into Stata. This one requires some engineering before it can be handled. Please respect requests made on behalf of those willing to give some time to help you. http://www.statalist.org/forums/help#stata

      If a student presented such a dataset I would have to ask how it got this way and what else might be messed up and how you are confident that values are paired like that. Or (more positively) is there some other information that makes this seem natural?

      You ask for A - B but all your examples are B - A.

      All serious questions, but a literal answer to your example is

      Code:
      clear
      input A B
       . .
       . .
       . .
       . .
       . .
       . 4
       . .
      5 .
       . .
       . 2
       . .
      3 .
       . 1
       . .
      2 .
      end
      
      gen long obsno = _n
      gen OK = (A < .) | (B < .)
      bysort OK (obsno) : gen pair = ceil(_n/2)
      
      bysort OK pair (obsno) : gen C = A[2] - B[1]
      list, sepby(pair)
      
           +-------------------------------+
           | A   B   obsno   OK   pair   C |
           |-------------------------------|
        1. | .   .       1    0      1   . |
        2. | .   .       2    0      1   . |
           |-------------------------------|
        3. | .   .       3    0      2   . |
        4. | .   .       4    0      2   . |
           |-------------------------------|
        5. | .   .       5    0      3   . |
        6. | .   .       7    0      3   . |
           |-------------------------------|
        7. | .   .       9    0      4   . |
        8. | .   .      11    0      4   . |
           |-------------------------------|
        9. | .   .      14    0      5   . |
           |-------------------------------|
       10. | .   4       6    1      1   1 |
       11. | 5   .       8    1      1   1 |
           |-------------------------------|
       12. | .   2      10    1      2   1 |
       13. | 3   .      12    1      2   1 |
           |-------------------------------|
       14. | .   1      13    1      3   1 |
       15. | 2   .      15    1      3   1 |
           +-------------------------------+
      Last edited by Nick Cox; 24 Nov 2016, 07:49. Reason: Fixed output.

      Comment


      • #4
        Thanks a lot for the help!

        Comment


        • #5
          Correction to #3:

          You ask for A - B but all your examples are B - A.
          Quite wrong!

          Comment

          Working...
          X