Announcement

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

  • How to combine two variables into one based on two criteria

    Hi I am a beginner in Stata.

    I have one panel dataset, and I want to do the following:

    To look up a value of index, let's call it "index" within my dataset based on two criteria, the countrycode does not equal to 3 and the year is equal to the year in the current row.
    To illustrate, I exectued the code
    Code:
     separate china_penetration_ind, by(countrycode)
    And my data for china_penetration_ind has been split. But I want them to be in the same row.
    I wonder how can I do the following:
    if countrycode==DE, replace china_penetration_indUK==china_penetration_indUK (for the values in those row whichcountrycode!=DE by matching year and nace), My dataset looks like the following. In summary, I want chinapeindDK to be in the same row with chinapeindDE by matching "nace" and "year"
    Click image for larger version

Name:	_20190206084757.png
Views:	1
Size:	26.5 KB
ID:	1482218

    Thanks in advance

  • #2
    Hi Xiaoke
    This is a little difficult to understand still.
    First, people will be most easily able to help you if you provide an example from your data with dataex. See the FAQ on how and why: https://www.statalist.org/forums/help#stata
    In very short, dataex gives people on the forum an exact copy of your data, so that any code they use will work for you.
    Be sure to provide an example with a selection of the variables and observations that illustrates your problem. Note that the variables nace and year are not in your screenshot, making it difficult to understand your data structure.

    Second, your question is confusing. This part "if countrycode==DE, replace china_penetration_indUK==china_penetration_indUK (for the values in those row whichcountrycode!=DE": so look for observations where country id DE or where it is not DE? For the part: "by matching year and nace" we cannot know how to match because we dont see how year and nace are included in your dataset.

    Comment


    • #3
      One more thought: I am wondering if you needed separate at all. It is quite possible that, starting from your original data, you could have used reshape in order to get to your desired data structure.
      Perhaps best to post a dataex example of your original data as well.

      Comment

      Working...
      X