Keeping caseid in and keeping missing observations out when using Stata "collapse" [message #5611] |
Tue, 16 June 2015 10:54 |
Lizzynaija
Messages: 12 Registered: February 2015 Location: United States
|
Member |
|
|
Dear DHS Researchers,
I am analyzing contextual determinants of neonatal mortality using the Nigeria 2013 DHS. I am currently trying to aggregate individual-level statistics to create community-level variables using the Stata collapse command.
I would be grateful if I could get some guidance on how to tackle the following challenges:
1)According to the Stata manual,"collapse" will, by default, use all my observations to calculate the summary statistics; if I want to exclude missing observations for variables, I am to specify the "cw" option. However, when I included this option, Stata returned an error message: "no observations" and I am not sure how to get around this.
2) I want to collapse by PSU (v001) so as to get the community means for my variables. However, I am running into problems with keeping my caseid variable in the collapsed dataset. I need to the caseid variable to stay in the dataset so that I can merge the community level means back onto the original IR dataset. However, when I put it in the by() portion of "collapse", it causes the dataset to collapse by the caseid, and not the PSU.
Below is the code I have been working with:
#delimit;
collapse(mean) commresid=wherelives commregion=region meancommeduc=comm_educlvl communemp=unemployed
commpoverty=poverty commpovlevel=povlevel commwealth=v190 commanc=ancvisits commpostnatal=postchk commsba=birthassist
commdelivery=birthplace commfemeduc=femeduc commeneduc=meneduc commfemjob=femjob commenjob=menjob
commworkprev=workprevyr commfirstmarr=agefirstunion commfirstbirth=matagefirstbirth commallkids=parity
commidealkids=idealkidnum commsons=xxsonsalive commgirls=xxgirlsalive commdecision=all_decision
commviolence=violence commcontrol=control, by(v001 caseid) cw;
#delimit cr
Please what are the steps I should take to get this to work properly?
Thank you very much,
Elizabeth
|
|
|