Announcement

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

  • Create new variable from de comparation of the values of two variables

    Hi everyone,

    I am student, recently using stata, having touble with the creation of variables Here is a simplified version of my data:
    row sales_2022 sales_2023
    1 39011 39011
    2 51805 0
    3 11921 11921
    4 0 77823
    5 14283 14283

    I am looking for a way in which Stata could do the followings:

    1.Create a new variable (for example "sales_comparation")
    2. Then replace the variable "sales_comparation" with the value common values between sales_2022 and sales_2023
    3. Replace with missing the variable "sales_comparation" when values are not the samen sales_2022 and sales_2023

    The result should be this
    row sales_2022 sales_2023 sales_comparation
    1 39011 39011 39011
    2 51805 0
    3 11921 11921 11921
    4 0 77823
    5 14283 14283 14283
    thanks for any help!
    regards

    Rob

  • #2
    Code:
    gen wanted = sales_2022 if sales_2022 == sales_2023

    Comment


    • #3
      Seems like you want something like this:

      Code:
      gen sales_comparison = sales_2022 if sales_2022 == sales_2023
      Although there are a few things to note. First, if you want to do this for multiple years, then you should transform this from the wide format to the long format first. In fact, you probably want to do that regardless. See:

      Code:
      help reshape
      And, just as a quick note, I don't know exactly what you want to do here, but it seems like it should be way more straight forward and useful to take the difference (if they are the same the difference is zero) or create an indicator (1 if sales were the same, otherwise 0) to get a variable that will be useful later in the analysis.

      Edit: crossed with #2.

      Comment


      • #4
        Thanks to the both of you Clyde and Daniel, very helpful information!!

        Regards

        Comment

        Working...
        X