Announcement

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

  • Change the order of values created in a variable using a loop

    When I try to create a variable in Stata with a loop, I want the resulting variable's values to exist in the order in which the loop was run, but instead, the results are in numeric ascencinding order. How can I fix this?

    For example, I want to create a variable that shows the average number of children a woman had over 5 survey waves by race (shown here as `k'). Below I have a loop for Black women [race_eth ==3].

    forvalues k = 1/5 {
    summarize numkids [w=weightvar] if race_eth == 3 & survey == `k', detail
    replace wtmn_numkids_black = r(mean) if race_eth == 1 & survey == `k'
    }

    I want the resulting variable to show me the values in order of survey year (which is the order in which the loop occurred), so the first value should be survey 1, second should be survey 2, etc. But instead, Stata orders the values from lowest to highest, creating a variable that looks like this:


    wtmn_numkids_black -- Svy weighted mean of number of children - black
    ----------------------
    Freq.
    2.487754
    2.547431
    2.686026
    2.797447
    2.999271


    Any help is much appreciated!


  • #2
    In the example you show you have an inconsistency.
    Code:
    forvalues k = 1/5 {
    summarize numkids [w=weightvar] if race_eth == 3 & survey == `k', detail 
    replace wtmn_numkids_black = r(mean) if race_eth == 1 & survey == `k'
    }
    But that is not your problem.

    Assuming you have longitudinal panel data, so the same women appear in successive waves, then for any individual woman in the survey, it seems to me that the number of births she has had will be the same or more in each succeeding wave, so I would expect the average across all the women to increase from wave to wave. Even if this average number is something adjusted for children in the household, or children under 18, or whatever, so that an individual woman can see a decrease between two waves, I would still expect those decreases to be more than offset by births to other women.

    Comment

    Working...
    X