Announcement

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

  • Subtract variables

    Hello,
    I'm completly new to Stata and I couldn't find a solution for my basic problem, so I would be really thankful if someone can answer this short question:

    I want to subtract to variables, they look like the following:

    var1: var2

    ID Freq. ID Freq.
    1 145 1 435
    2 278 2 234
    3 543 3 643
    4 683 4 123

    And i want to subtract the frequencies to a new variable, so that the result will be:
    var 3:

    ID Freq.
    1 435-145
    2 234-278
    3 ....
    ...

    I tried using gen var3 = var2 - var1, but somehow then it subtracts the IDs and not the frequencies.
    And I tried this:
    gen var3 = .
    replace var3 = 1 if var2 == 1
    replace var3 = 1 if var1 == 1

    But then it just sums up the frequencies, but I can't figure out how to subtract them.

    I would really thankful for an answer!
    Best Regards

  • #2
    Scott:
    welcome to the list.
    You cannot achieve what you're after with your current data layout (mainly, you cannot have two -ID- for the same observations).
    You may want something along the following lines, instead:
    Code:
    . input ID Freq_1 Freq_2
    
                ID     Freq_1     Freq_2
      1. 1 145  435
      2.
    .  2 278  234
      3.
    .  3 543  643
      4.
    .  4 683  123
      5.
    . end
    
    . g var_3= Freq_2- Freq_1
    
    . list
    
         +------------------------------+
         | ID   Freq_1   Freq_2   var_3 |
         |------------------------------|
      1. |  1      145      435     290 |
      2. |  2      278      234     -44 |
      3. |  3      543      643     100 |
      4. |  4      683      123    -560 |
         +------------------------------+
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      Hello Carlo,
      thank you for the fast answer first, but I still don't really get what you did...
      Maybe I wasn't really clear:
      Both variables var1 and var2 are measuring how people are concernd about 2 topics; The ID is coded like that: 1 (Very Concerned) 2(Concerned) ... and the freq. is how many people answered that. And now I just want to subtract the values. When I use the following code, then it adds the values, but isn't it possible to just subtract and not to add them?:

      gen var3 = .
      replace var3 = 1 if var2 == 1
      replace var3 = 1 if var1 == 1

      I'm sorry if my question is stupid, but I try to find a solution for hours and I'm nearly freaking out over it
      Kind regards,
      Scott

      Comment


      • #4
        Scott:
        thanks for providin furthe details.
        I would not use ID for reperesenting variable score, though.
        That said, you would be better off with working with 4 separate datasets and creating from scratch the frequencies of each pair of variables and the -append- those datasets them together in a new .dta file.
        There's also another issue with your dataset: var1 and var2 have different frequencies; hence you probably have to deal with a missing values problem.
        Kind regards,
        Carlo
        (Stata 19.0)

        Comment


        • #5
          I am still quite unclear about what's going on. Presenting a reproducible example with dataex (SSC) and explaining what you want done with reference to that would help immensely. That's no more than we suggest in the FAQ Advice everybody is asked to read before posting.

          https://www.statalist.org/forums/help#stata

          Comment

          Working...
          X