The DHS Program User Forum
Discussions regarding The DHS Program data and results
Home » Data » Dataset use in Stata » I need help constructing a variable in Stata
Re: I need help constructing a variable in Stata [message #13091 is a reply to message #13066] Mon, 18 September 2017 10:05 Go to previous message
Bridgette-DHS is currently offline  Bridgette-DHS
Messages: 3190
Registered: February 2013
Senior Member
Here is another response from DHS Stata Expert, Tom Pullum:


The following Stata lines should accomplish what you want, with the births re-sequenced by birth order rather than by recency.

quietly summarize v201
scalar max_ceb=r(max)

keep v011 b3* bord*
rename b*_0* b*_*

* bord_1 is the birth order of the most recent birth
* the first birth will be the one for which bord_1=1 or bord_2=1 or bord3=1, etc
* the second birth will be the one for which bord_1=2 or bord_2=2 or bord3=2, etc

local lj=1
quietly while `lj'<=max_ceb {
gen b3ordered_`lj'=.

  local li=1
  while `li'<=max_ceb {
  replace b3ordered_`lj'=b3_`li' if bord_`li'==`lj'
  local li=`li'+1 
  }

local lj=`lj'+1
}

summarize b3_*
summarize b3ordered_*
 
Read Message
Read Message
Read Message
Read Message
Previous Topic: Calculating median age at first sex and percentage of respondents with sex before the age of 15
Next Topic: Opening DHS data on STATA 13
Goto Forum:
  


Current Time: Sat Nov 9 11:43:06 Coordinated Universal Time 2024