Announcement

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

  • Panel data

    Hi.. anyone.. who could solve my issue.. i have a panel for 2011 and 2016.... the religion should be same but for me it changes... if its 1 in 2011 it should be 1 in 2016; if 2 in 2011 it should be 2 in 2016 .. so i want to clean my data and erase those households which have difference in religion in both years.. i did try some command but it didnt work.. can some help...

    i tried this but this is wrong i am missing something...
    by HHID (year), sort: keep if religion==1 & year==2011 |religion==1 &year==2016 & religion==2 & year==2011 |religion==2 &year== 2016 & religion==3 & year==2011 |religion==3 &year==2016
    Last edited by Olive Bat; 01 Mar 2020, 22:12.

  • #2
    i did try some command but it didnt work.. can some help...
    That's really unhelpful. Most of us have mediocre telepathy skills. How are we to imagine what commands you tried, and in what way they didn't work. When asking for help here, always give specific information: actual commands, actual results (including error messages).

    That said, you are also unclear in your request. When you say you want to "erase" households which have a difference in religion, do you mean you want to set the value of religion to missing, or do you mean you want to delete the observations for those households altogether?

    Code:
    // TO DELETE HOUSEHOLDS WHERE RELIGION IS NOT CONSTANT
    by household (religion), sort: drop if religion[1] != religion[_N]
    
    // TO REPLACE VALUE OF RELIGION WITH MISSING VALUE WHEN RELIGION IS NOT CONSTANT
    by household (religion), sort: replace religion = . if religion[1] != religion[_N]
    This code makes a number of assumptions about your data. Among them, that it is in long layout, and that religion is a numeric variable, named religion, and that households are identified by a variable named household. In the future, to get help that is specifically tailored to your actual data, you should always show example data when asking for help with code. And you should always use the -dataex- command to do that.
    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.

    When asking for help with code, always show example data. When showing example data, always use -dataex-.

    The more information you give about your problem, the greater your chances of getting a solution.

    Comment


    • #3
      Thank you Sir.. for your earnest reply .. i wanted to delete the households so your first command worked for me
      i did try the following command too
      bysort HHID: gen x = religion - religion[_n-1]
      replace x = x[_n+1] if x==.
      keep if x==0
      and i got the same result as yours... once again thank you and i will improve on the way of writing my specific query...

      Comment

      Working...
      X