Announcement

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

  • Forvalues error: invalid name

    Hello,
    I'm using STATA 11 and am trying to run the following code:
    Code:
        forvalues i = 1(1)67 {
                industry_recode `i'
                statsby num_emp=r(N), by(educ_attain age_cohorts sex classwkr) nodots saving/*
                */("filepath\counts`i'.dta")/*
                */ :summarize if empstat == 1 & DJAinds==`i' [weight=perwt2]
            }
            forvalues i = 69(1)88 {
                industry_recode `i'
                statsby num_emp=r(N), by( educ_attain age_cohorts sex classwkr) nodots saving/*
                */("filepath\counts`i'.dta")/*
                */ :summarize if empstat == 1 & DJAinds==`i'[weight=perwt2]
            }
    It runs the first loop but returns an error as soon as the second loop starts.

    Code:
    69 invalid name
    With a rc of 198.

    If the same style of looping is used but with just display commands on the inside it runs fine.:

    Code:
        forvalues i = 1(1)67 {
               di `i'
            }
            forvalues i = 69(1)88 {
             di `i'
            }
    I don't understand why it gives an error for one but not the other.

    The purpose of these loops are to run a program (industry_recode) in order to properly weight the commands to be executed by -statsby-. The reason it needs to be broken into two loops is that
    there is a break where industry_recode does not have an industry code for 68.

    Thank you in advance for any light that can be shed on this problem.

  • #2
    Your problem is that in your second loop you are missing a space before the left bracket that begins your weight specification. As a consequence, Stata is treating it as the beginning of a subscript, as if it were going to be something like 69[_n-1].

    Someone may be able to suggest an easy way to code this as a single loop: I am not at my Stata system to test at the moment.
    Last edited by William Lisowski; 10 Apr 2015, 05:26.

    Comment


    • #3
      You are right that the forvalues syntax looks fine here, so we are left with a guess that industry_recode is not happy with certain input or that one of your variables is not what you think it is.

      So whatever the problem, it arises from a user-written program you don't show us or reference or from data we can't see.

      By the way, one loop is possible using

      Code:
       
      foreach i of numlist 1/67 69/88 { 
      
      }
      Also, you seem to be creating 87 different summary datasets. That doesn't seem a good idea. I am confident that that there is a better way if only the hidden program can be explained.

      Apart from that detail, the beast looks like one line:

      Code:
        
       contract educ_attain age_cohorts sex classwkr DJAinds if empstat == 1 [weight=perwt2]

      Comment


      • #4
        Nick confirms my expectation that a single loop could be coded, and suggests further improvements. His posting and mine crossed, on reading his I suspect he took your statement that your code "returns an error as soon as the second loop starts" literally, as I might have had I read more carefully. I assume you mean that it returns an error during the initial loop, but I expect it was after industry_recode 69 ran successfully.

        Welcome to the club of Stata users who are tripped up by its quirky syntax, as I have been repeatedly. In this case, because of the multiple meanings of the left bracket [, spaces are more significant than they are in some alternative statistical systems.

        Comment


        • #5
          William: Good catch on the [.

          Comment


          • #6
            Thank you very much for your responses (and for finding my bug). I did not realize that stata could use loops like that, and was not familiar with the contract command. You are right that the industry recode program is quirky, and I agree that this approach is not the best.

            I am not sure where in the loop it throws an error, although I will check on that.

            This is what the industry_recode function does for i=69:

            Code:
            else if `DJA'==69 {
                 //DJAindus98 recodes
                replace DJAinds=69 if IND1990==791
                replace DJAinds=69 if IND1990==741
                replace DJAinds=69 if IND1990==420
                replace DJAinds=69 if IND1990==750
                replace DJAinds=69 if IND1990==751
                replace DJAinds=69 if IND1990==800
                replace DJAinds=69 if IND1990==742
                replace DJAinds=69 if IND1990==801
            
                //perwt2 recodes
                replace perwt2=round(perwt*0.0136) if IND1990==791
                replace perwt2=round(perwt*0.0253) if IND1990==741
                replace perwt2=round(perwt*0.0343) if IND1990==420
                replace perwt2=round(perwt*0.1677) if IND1990==750
                replace perwt2=round(perwt*0.1677) if IND1990==751
                replace perwt2=round(perwt*0.2769) if IND1990==800
                replace perwt2=round(perwt*0.9591) if IND1990==742
                replace perwt2=round(perwt*0.987) if IND1990==801
            }
            The reason I was using summarize is because I need to get totals for several other variables such as:

            Code:
            forvalues i = 1(1)67 {
                    industry_recode `i'
                    statsby Total_WAGE=r(sum), by( educ_attain age_cohorts sex classwkr) nodots saving/*
                    */("filepath\countsF`i'.dta")/*
                    */ :summarize incwage & DJAinds==`i' [weight=perwt2]
            }
            But I want the totals, and counts as variables in just one dataset, so I had planned to use -append- and -merge- to do so.

            The problem is that the weights need to be assigned dynamically, because some of the IND1990 codes go to two different DJAinds codes.

            Comment

            Working...
            X