Announcement

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

  • puzzling out for num line of code

    hi guys!

    I recently received a *do file dating back to 2010 and there's a line of code I'm struggling with:

    for num 1/6: by xwaveid: replace hhfxid=hhfxid[X] if hhfxid==""

    where xwaveid is the crosswave ID (7-digit) and hhfxid is the father's xwaveid (text, 7-digit)

    Specifically I'm unable to figure out what the requested change is / how to look for the change made

    grateful for your suggestions


  • #2
    -for- is an obsolete command that was used before -foreach- was created.

    If I remember how its syntax worked, a modern substitue for the code you show would be:

    Code:
    forvalues i = 1/6 {
        by xwaveid: replace hhfxid = hhfxid[`i'] if missing(hhfxid)
    }
    The effect of this code would be to replace missing values of the variable hhfxid with the first non-missing value among the first six observations of hhfxid (this is done separately within each group of observations corresponding to a single value of xwaveid). Does that seem sensible in your context?

    Comment


    • #3
      for in Stata went undocumented in Stata 7, if I recall correctly. It should still work, but it is not documented (not even undocumented).

      Modern equivalent is

      Code:
      forval x = 1/6 { 
          by xwaveid: replace hhfxid=hhfxid[`x'] if hhfxid==""
      }
      In turn, the first hhfxid value in each wave is copied to all values for each identifier are missing.

      If any are still missing, use the second value, and so on.

      It doesn't check for inconsistencies in hhfxid between waves.

      Comment


      • #4
        thank you both for the modern equivalent and explanation. nick you are right: 'for' still works.

        what I still dont get is the following:

        if I keep just one wave of the data, the code generates no changes (no surprise)
        if, however, I keep 2 waves, then for both `x'=1 and `x'=2 I get changes. shouldnt there be changes only for `x'=1? I mean if in wave 2 I have no ID value for the father, then it is copied from wave 1. what am I missing here?

        Comment


        • #5
          I can't see your data, but consider this dataset

          Code:
          . clear
          
          . set obs 6
          obs was 0, now 6
          
          . gen father = "dad" in 2
          (5 missing values generated)
          
          . l, sep(0)
          
               +--------+
               | father |
               |--------|
            1. |        |
            2. |    dad |
            3. |        |
            4. |        |
            5. |        |
            6. |        |
          Looking at the first observation won't change anything, but looking at the second will. After that no changes.

          Comment


          • #6
            the only explanation I can think of is:
            the ID of the father is missing in wave 2 and not in wave 1 `x'=1
            the ID of the father is missing in wave 1 and not in wave 2 `x'=2
            would that make sense?

            Comment


            • #7
              guess I was typing as you were typing, and the answer is the same. thanks nick

              Comment


              • #8
                I was a big fan of this construction and still keep these pages bookmarked:
                http://www.stata.com/support/faqs/pr...-for-commands/
                http://apps.eui.eu/Personal/Franklin...tyle%20for.pdf
                Stata/MP 14.1 (64-bit x86-64)
                Revision 19 May 2016
                Win 8.1

                Comment


                • #9
                  I think you are most of the way there. The code loops over the observations in each group. Run through the code on 6 observations with this pseudocode.

                  for i = 1 to 6
                  replace hhfxid with hhfxid[i] if any values of missing

                  Thus

                  replace hhfxid with hhfxid[1] if any values are missing
                  replace hhfxid with hhfxid[2] if any values are missing
                  replace hhfxid with hhfxid[3] if any values are missing
                  replace hhfxid with hhfxid[4] if any values are missing
                  replace hhfxid with hhfxid[5] if any values are missing
                  replace hhfxid with hhfxid[6] if any values are missing

                  So it finds the first non-missing of hhfxid and uses that to fill in any missings.

                  Comment


                  • #10
                    Carole: No, no, no! (Insert friendly grin.) See also http://www.stata.com/support/faqs/pr...lems-with-for/ written in despair because of the many people who got bitten when they tried to combine this with other constructs. Terrible messes, terrible confusion.

                    Comment


                    • #11
                      will do.
                      I dare 2 more questions:
                      a. how would I got about a before and after list of the data?
                      b. how would I perform a consistency check?

                      Comment


                      • #12
                        No, no worries! That was before I was comfortable with using macros. I keep them bookmarked because I do occasionally find old code that I need to translate. I've reformed my ways and find loops are much more intuitive!
                        Stata/MP 14.1 (64-bit x86-64)
                        Revision 19 May 2016
                        Win 8.1

                        Comment

                        Working...
                        X