Announcement

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

  • Invalid syntax error

    Hello, anyone have an idea why Im getting an invalid syntax error right at the end of this loop?

    Code:
    * declare local macros for ownership names
    local name1 "SOEs"
    local name2 "PRIs"
    local name3 "FIEs"
    
    * loop over ownership categories
    forvalues o=1/3 {
    
        * loop through entry year 2001 to 2016
        forvalues y=2001 {
            * open the dataset
            use "$output/Temp.dta", clear
    
            * restrict to firms whose first year in the data is the current entry year
            egen firstyear=min(year), by(madn)
            keep if firstyear==`y'
    
            * keep if the firm is the current ownership type in entry year
            gen temp=owner if year==`y'
            egen owner`y'=max(temp), by(madn)
            drop temp
            keep if owner`y'==`o'
    
            * restrict to firms in manufacturing in entry year
            gen temp=vsic1993 if year==`y'
            egen vsic`y'=max(temp), by(madn)
            drop temp
            keep if vsic`y'>1500 & vsic`y'<3700
    
            * create a variable that tracks end of year employment during the entry year
            gen temp=empend if year==`y'
            egen empend`y'=max(temp), by(madn)
            drop temp
    
            * calculate mean initial and contemporary employment among survivors by year
            collapse (mean) empend empend`y', by(year)
    
            label variable year "Year"
    
            * create a line graph of the two mean employment series
            * if SOEs
            if `o'==1 {
                line empend year, lcolor(red) || line empend`y' year, lcolor(blue) lpattern(dash) ///
                    ytitle("") xlabel(2001(2)2017) ///
                    graphregion(color(white)) ylabel(0(100)600, angle(0)) legend(rows(1) label(1 "Contemporary employment") label(2 "Initial employment"))
            }
            * if PRIs
            if `o'==2 {
                line empend year, lcolor(red) || line empend`y' year, lcolor(blue) lpattern(dash) ///
                    ytitle("") xlabel(2001(2)2017) ///
                    graphregion(color(white)) ylabel(0(20)100, angle(0)) legend(rows(1) label(1 "Contemporary employment") label(2 "Initial employment"))
            }
            * if FIEs
            if `o'==3 {
                line empend year, lcolor(red) || line empend`y' year, lcolor(blue) lpattern(dash) ///
                    ytitle("") xlabel(2001(2)2017) ///
                    graphregion(color(white)) ylabel(0(200)800, angle(0)) legend(rows(1) label(1 "Contemporary employment") label(2 "Initial employment"))
            }
            
            * export the graph to pdf
            graph export "$tabfig/mean_employment_surviving_`name`o''_`y'entry.pdf", as (pdf) replace
            
            * calculate relative to entry year
            gen sempend=empend/empend`y'
            
                * save the dataset
            gen startyear=`y'
            gen owner=`o'
            save "$temp\Mean employment among surviving `name`o'' `y' entrants.dta", replace
    
        }
        * end of looping through entry years 2001 to 2016
    
        * append over the years
        use "$temp/Mean employment among surviving `name`o'' 2001 entrants.dta", clear
        forvalues y=2002/2016 {
            append using "$temp/Mean employment among surviving `name`o'' `y' entrants.dta"
        }
    
        * save the dataset
        save "$temp/Mean employment among surviving `name`o'' entrants.dta", replace
        
    }
    * end of looping through ownership
    Thanks :D

  • #2
    Oops I figured it out I should be using foreach y in 2001 instead of forvalues

    Comment


    • #3
      Short answer: No.

      Longer answer: Here are some points from reading your code:

      1. It is hard to debug someone else's code if we don't see what is in the global macros you're using. Subtle or not subtle errors can arise if the global macros are not defined or have inappropriate content. (With exceptions of probability 0, I agree with Clyde Schechter that global macros are a poor choice. It is much better to show local macros visibly defined.)


      2. This loop won't do what the comment says. Only the start year is mentioned.

      Code:
      * loop through entry year 2001 to 2016
      forvalues y=2001 {
      3.

      Code:
      gen temp=vsic1993 if year==`y'
      egen vsic`y'=max(temp), by(madn)
      drop temp
      This kind of construct can be boiled down to

      Code:
      egen vsic`y'  = max(cond(year == `y', vsic1993, .)), by(madn)

      Comment


      • #4
        Thanks!

        Comment

        Working...
        X