Announcement

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

  • 'is not a valid command' error when using forval

    Hi,
    I'm using the following forval command from: How do I create variables summarizing for each individual properties of the other members of a group? Stata FAQ
    Code:
    summarize PERSONID, meanonly
    forval i = 1 / `r(max)' { 
          replace ischild = (RO9 == `i') | (RO10 == `i')
          qui by IDHH (ischild), sort:
          replace ownchild =
          cond(RO10[_N] == `i', mchild[_N], fchild[_N])
          if PERSONID == `i' & ischild[_N] 
    }
    The error I get is 'is not a valid command' on the line 'qui by IDHH (ischild), sort:'
    Does someone know what's wrong? I'm new to Stata and have never dealt with forval before. I am writing this code straight into the state command window and not a do file.

    Thanks in advance
    Last edited by John Ash; 05 May 2020, 11:27.

  • #2
    My guess is
    Code:
    & ischild[_N]
    should be

    Code:
    & ischild == ischild[_N]

    Comment


    • #3
      Code:
       qui by IDHH (ischild), sort: replace ownchild = cond(RO10[_N] == `i', mchild[_N], fchild[_N]) if PERSONID == `i' & ischild[_N]   
      is all one (horrendous) command. The FAQ https://www.stata.com/support/faqs/d...ng-properties/ uses #delimit ; to indicate that the command line you are modifying doesn't end before the semi-colon.

      Comment


      • #4
        You omitted the commands
        Code:
        #delimit ;
        ...
        ;
        from your code. If you don't run your code from a do-file, then the code looks like this
        Code:
        summarize PERSONID, meanonly
        forval i = 1 / `r(max)' { 
              replace ischild = (RO9 == `i') | (RO10 == `i')
              qui by IDHH (ischild), sort: replace ownchild = cond(RO10[_N] == `i', mchild[_N], fchild[_N]) if PERSONID == `i' & ischild[_N] 
        }

        Comment


        • #5
          Thanks for the reply - I tried the suggestion but it still doesn't work.
          Let me post my entire original code:

          Code:
          by IDHH RO9, sort: gen fchild = _N if RO9 < .
          by IDHH RO10, sort: gen mchild = _N if RO10 < .  
           gen byte ownchild = 0  
           gen byte ischild = 0  summarize PERSONID, nomeanonly    
           forval i = 1 / `r(max)' {   
                     replace ischild = (RO9 == `i') | (RO10 == `i')  
                     qui by IDHH (ischild), sort:  
                     replace ownchild =  
                     cond(RO10[_N] == `i', mchild[_N], fchild[_N])  
                     if PERSONID == `i' & ischild[_N]  
           }

          Comment


          • #6
            Originally posted by Sven-Kristjan Bormann View Post
            You omitted the commands
            Code:
            #delimit ;
            ...
            ;
            from your code. If you don't run your code from a do-file, then the code looks like this
            Code:
            summarize PERSONID, meanonly
            forval i = 1 / `r(max)' {
            replace ischild = (RO9 == `i') | (RO10 == `i')
            qui by IDHH (ischild), sort: replace ownchild = cond(RO10[_N] == `i', mchild[_N], fchild[_N]) if PERSONID == `i' & ischild[_N]
            }
            Amazing - thanks a lot. When I used the delimit it gave an error - but I guess that only works in a do file. I didn't know much about do files and it didn't mention it in the FAQ so was a bit confused. Thanks again!!

            Comment

            Working...
            X