Announcement

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

  • Flagging negative values within an id

    Hi. My dataset has an id variable and a total amount variable. There are multiple observations per id and within a single id there are sometimes negative numbers of the same positive number in another observation. How do I flag these entries that are merely negatives of each other? For example, in the given table below, I want to flag the first two entries for id 1 and the first and third entry for id 3.
    Id Total Amount
    1 34
    1 -34
    1 25
    2 13
    2 14
    3 456
    3 43
    3 -456
    3 765

  • #2
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte id int totalamount
    1   34
    1  -34
    1   25
    2   13
    2   14
    3  456
    3   43
    3 -456
    3  765
    end
    
    gen abs_amt = abs(totalamount)
    gen sign_amt = sign(totalamount)
    
    by id abs_amt (sign_amt), sort: gen byte flag = sign_amt[1] != sign_amt[_N]
    The tableau you created to show your example data does not come from a Stata data set: it couldn't because variable names cannot contain embedded blanks. If you have not yet imported your data into Stata, it is premature to ask for help with code. If you have brought your data into Stata, then the helpful thing to do is to give an example from the Stata data set, and use the -dataex- command for that purpose. Not only will it save a great deal of time for whoever chooses to try to help you, it will also be quicker and easier for you! If you are running version 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    Comment


    • #3
      Thank you, Clyde. That really helped. The reason I did not use dataex is because I'm working off a remote server and can't copy anything off a platform running on it.

      I do have a follow on question. I want to check if for each negative value in the Total Amount variable, there is a corresponding positive vale. Basically, how can I check if in the table above if id #1 has both a -34 and a 34? I understand that there is a flag==1 created for both the entries, but confirming that appears to be a manual task currently and my dataset had over a 100,000 observations
      Last edited by Karishma DSouza; 25 Sep 2020, 12:56.

      Comment


      • #4
        No. The only way that code will produce a flag is on a pair (or larger group) of observations that have the same value but different signs. It does not just flag all negative values--a negative value with no positive paired with it will not get flagged by that code.

        Comment


        • #5
          Thank you, Clyde. I greatly appreciate your help.

          Comment

          Working...
          X