Announcement

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

  • -post- command error: "invalid syntax post: above message corresponds to expression 4, variable p_ols1"

    Hello Statalist Community,

    I hope everything is well!
    I'm going crazy with a loop I'm trying to implement on stata. I would like to save the results to another .dta file, as my dataset is very large, and to do this I use the -post- command.

    However, I've been getting the same error for a while, and I don't understand why it's happening.

    Here's part of my do-file:



    Code:
    // --------------------------------------------- //
    clear all
    capture log close
    set more off
    set seed 202402
    set varabbrev off
    version 17
    // --------------------------------------------- //
    * global
    global data "C:/Users/miduarte/Desktop/Ongoing_Projects/test_HolaLuz_Data/New_dataset_15112023/stata/2_export_telemedida/data/random/"
    global destkop "C:/Users/miduarte/Desktop"
    // --------------------------------------------- //
    
    use `id_zip_complete', clear
    
    cd "${desktop}"
        *Set up file to capture coefficient estimates
        postutil clear
        tempname iLevOLSResults
        postfile `iLevOLSResults' ID p_ols0 se_ols0 p_ols1 se_ols1 p_ols2 se_ols2 p_ols3 se_ols3  ///
            using "iLevOLSResultsIV_GN.dta", replace
    
        levelsof id, local(idlist)
    
        local loop = 1
        disp c(current_time)
    
        foreach i of local idlist {
            disp "loop == `loop'"
            disp "id == `i'"
    
            preserve
                keep if id == `i'
    
                qui {
                    
                    *generate time and temp variables
                    egen byte ym = group(y m)
                    
                    gen int block=1 if hr>=1&hr<=4
                    replace block=2 if hr>=5&hr<=8
                    replace block=3 if hr>=9&hr<=12
                    replace block=4 if hr>=13&hr<=16
                    replace block=5 if hr>=17&hr<=20
                    replace block=6 if hr>=21&hr<=24
    
                    
    
                     gen lnkwh = asinh(kwh)
                    gen int peak = 1 if block >= 3
                    replace peak = 0 if peak == .
    
                    foreach x in 1 7 365 {
                        gen tau_`x' = (t+0.5)/`x'*_pi*2
                        gen tau2_`x' = tau_`x'^2
                        forvalues k = 1(1)4 {
                            gen cos`k'tau_`x' = cos(tau_`x'/`k')
                        }
                    }
                    
                    * re-scale variables
                    foreach v of varlist tau* {
                        egen mean`v' = mean(`v')
                        egen sd`v' = sd(`v')
                        replace `v' = (`v'-mean`v')/sd`v'
                        drop mean`v' sd`v'
                    }
                    
    
                }
                            
                * Regressions *****************
                * Create locals (clean from previous loop)
                forvalues spec = 0(1)3 {
                    cap forvalues inst = 0(1)1 {
                        local beta_ols`spec'`inst' = .
                        local beta_se`spec'`inst' = .
                    }
                }
                * Fixed effects
                forvalues spec = 0(1)3 {
                    cap {
                        qui reghdfe lnkwh, absorb(`xvars_ols`spec'')
                        local beta_ols`spec' = e(b)[1,1]
                        local se_ols`spec' = sqrt(e(V)[1,1])
                    }
                    cap forvalues inst = 0(1)1 {
                        qui ivreghdfe lnkwh, absorb(`xvars_ols`spec'')
                        local beta_iv`spec'`inst' = e(b)[1,1]
                        local se_iv`spec'`inst' = sqrt(e(V)[1,1])
                    }
                }
    
    
                post `iLevOLSResults' (`i') (`beta_ols0') (`se_ols0') ///
                            (`beta_ols1') (`se_ols1') ///
                            (`beta_ols2') (`se_ols2') ///
                            (`beta_ols3') (`se_ols3') 
                
            restore    
            
            local loop = `loop' + 1
        }
        
    postclose `iLevOLSResults'
    use "${desktop}/iLevOLSResultsIV_GN.dta", clear
    
    exit, clear
    And this is the error I get:

    Code:
    invalid syntax
    post:  above message corresponds to expression 4, variable p_ols1
    r(198);

    I think it's a coding error on my part, but I don't know where. Here is a small -dataex-:



    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input long id float(y m dom) byte hr float kwh long sp_zipcode
    1039 2021 3 24 18 .219 8021
    1039 2021 2 10 12 .146 8021
    1039 2021 1 31  3 .133 8021
    end


    I beg you: could someone please help me understand my error?
    Many thanks in advance!

    Michael
    Last edited by Michael Duarte Goncalves; 09 Feb 2024, 03:39.

  • #2
    1. You reference t in : gen tau_`x' = (t+0.5)/`x'*_pi*2 , but it is never defined

    2. You reference but never define : `xvars_ols`spec''

    The regression command fail which result in the local beta_ and se_ to be empty


    Here is a simpler example that works:
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input long id float(y m dom) byte hr float kwh long sp_zipcode 
        1039 2021 3 24 18 .219 8021  
        1039 2021 2 10 12 .146 8021  
        1039 2021 1 31  3 .133 8021 
        1 2021 3 24 18 .219 8021  
        1 2021 2 10 12 .146 8021  
        1 2021 1 31  3 .133 8021 
    end
    
    postutil clear
    tempname iLevOLSResults
    postfile `iLevOLSResults' ID p_ols0 se_ols0 p_ols1 se_ols1 p_ols2 se_ols2 p_ols3 se_ols3  ///
        using "iLevOLSResultsIV_GN.dta", replace
    
    levelsof id, local(idlist)
    
    foreach i of local idlist {
        preserve
        keep if id == `i'
        gen lnkwh = asinh(kwh)
        forvalues spec = 0(1)3 {
            cap {
                qui reghdfe lnkwh, noabsorb
                local beta_ols`spec' = e(b)[1,1]
                local se_ols`spec' = sqrt(e(V)[1,1])
            }
        }
        post `iLevOLSResults' (`i')     (`beta_ols0') (`se_ols0') ///
            (`beta_ols1') (`se_ols1') ///
            (`beta_ols2') (`se_ols2') ///
            (`beta_ols3') (`se_ols3') 
        restore
    
    }
    postclose `iLevOLSResults'
    use iLevOLSResultsIV_GN,clear
    list

    Comment


    • #3
      Hi Scott Merryman:

      Thank you so much for your provided answer.
      I tried the code that you posted in #2, and everything works perfectly well.

      Thank you so much for your time and help.
      I wish you a beautiful day.

      All the best,

      Michael

      Comment


      • #4
        Hi Statalist Community,

        I'll come back to the same theme. This time, I've actually defined my variable t and my local `xvars_ols`spec'', because I need them for later use.
        However, I'm getting the same error as before. Here is my exhaustive code:

        Code:
        * 13.02.2024*
        * Michael D.G.*
        
        
        // --------------------------------------------- //
        clear all
        capture log close
        set more off
        set seed 202402
        set varabbrev off
        version 17
        // --------------------------------------------- //
        * global
        
        global data "C:/Users/miduarte/Desktop/Ongoing_Projects/test_HolaLuz_Data/New_dataset_15112023/stata/2_export_telemedida/data/random/"
        global destkop "C:/Users/miduarte/Desktop"
        
        // --------------------------------------------- //
        * 1. Parameter setting
        local xvars_ols0 = "i.block"
        local xvars_ols1 = "i.block tau*"
        local xvars_ols2 = "i.block cos*"
        local xvars_ols3 = "i.block cos* tau*"
        
        
        cd "${data}/../merged"
        use "export_contratos_cleaned_households.dta", clear
        keep id sp_zipcode
        duplicates drop id, force
        
        tempfile id_and_zip
        save `id_and_zip', replace
        
        * 2. Loop over consumers before June 1, 2021
        cd "${data}"
        use "append_012021_072023_randomSamp.dta", clear
        keep if date_elec_consumption <= td(01jun2021)
        
        tostring id date_elec_consumption, replace
        gen new_id = id + "_" + date_elec_consumption
        reshape long h_, i(new_id) j(hr)
        
        rename h_ kwh
        drop new_id
        destring id date_elec_consumption, replace
        
        format %td date_elec_consumption
        
        gen y = year(date_elec_consumption)
        gen m = month(date_elec_consumption)
        gen dom = day(date_elec_consumption)
        gen t = date_elec_consumption+hr/24
        drop date_elec_consumption
        
        merge m:1 id using `id_and_zip', keep(3) nogen
        tempfile id_zip_complete
        save `id_zip_complete', replace
        
        use `id_zip_complete', clear
        
        cd "${desktop}"
            *Set up file to capture coefficient estimates
            postutil clear
            tempname iLevOLSResults
            postfile `iLevOLSResults' ID p_ols0 se_ols0 p_ols1 se_ols1 p_ols2 se_ols2 p_ols3 se_ols3  ///
                using "iLevOLSResultsIV_GN.dta", replace
        
            levelsof id, local(idlist)
        
            local loop = 1
            disp c(current_time)
        
            foreach i of local idlist {
                disp "loop == `loop'"
                disp "id == `i'"
        
                preserve
                    keep if id == `i'
        
                    qui {
                        
                        *generate time and temp variables
                        egen byte ym = group(y m)
                        
                        gen int block=1 if hr>=1&hr<=4
                        replace block=2 if hr>=5&hr<=8
                        replace block=3 if hr>=9&hr<=12
                        replace block=4 if hr>=13&hr<=16
                        replace block=5 if hr>=17&hr<=20
                        replace block=6 if hr>=21&hr<=24
        
                        
        
                         gen lnkwh = asinh(kwh)
                        gen int peak = 1 if block >= 3
                        replace peak = 0 if peak == .
        
                        foreach x in 1 7 365 {
                            gen tau_`x' = (t+0.5)/`x'*_pi*2
                            gen tau2_`x' = tau_`x'^2
                            forvalues k = 1(1)4 {
                                gen cos`k'tau_`x' = cos(tau_`x'/`k')
                            }
                        }
                        
                        * re-scale variables
                        foreach v of varlist tau* {
                            egen mean`v' = mean(`v')
                            egen sd`v' = sd(`v')
                            replace `v' = (`v'-mean`v')/sd`v'
                            drop mean`v' sd`v'
                        }
                        
        
                    }
                                
                    * Regressions *****************
                    * Create locals (clean from previous loop)
                    forvalues spec = 0(1)3 {
                        cap forvalues inst = 0(1)1 {
                            local beta_ols`spec'`inst' = .
                            local beta_se`spec'`inst' = .
                        }
                    }
                    * Fixed effects
                    forvalues spec = 0(1)3 {
                        cap {
                            qui reghdfe lnkwh, absorb(`xvars_ols`spec'')
                            local beta_ols`spec' = e(b)[1,1]
                            local se_ols`spec' = sqrt(e(V)[1,1])
                        }
                        cap forvalues inst = 0(1)1 {
                            qui ivreghdfe lnkwh, absorb(`xvars_ols`spec'')
                            local beta_iv`spec'`inst' = e(b)[1,1]
                            local se_iv`spec'`inst' = sqrt(e(V)[1,1])
                        }
                    }
        
        
                    post `iLevOLSResults' (`i') (`beta_ols0') (`se_ols0') ///
                                (`beta_ols1') (`se_ols1') ///
                                (`beta_ols2') (`se_ols2') ///
                                (`beta_ols3') (`se_ols3')
                    
                restore    
                
                local loop = `loop' + 1
            }
            
        postclose `iLevOLSResults'
        use "${desktop}/iLevOLSResultsIV_GN.dta", clear
        
        exit, clear
        And the error I obtained:

        Code:
        loop == 1
        id == 1039
        (7,248,864 observations deleted)
        invalid syntax
        post:  above message corresponds to expression 4, variable p_ols1
        r(198);
        Here is a -dataex- with variable t defined:


        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input long id float(y m dom) byte hr float(t kwh) long sp_zipcode
        1039 2021 4  1 19  22371.79 .119 8021
        1039 2021 1 28 20 22308.834 1.21 8021
        1039 2021 2  9 19  22320.79 .594 8021
        1039 2021 4  6 16 22376.666 .128 8021
        1039 2021 2 15 23  22326.96 .316 8021
        end

        Could someone please help me?

        I really appreciate any help you can provide.
        Best,

        Michael
        Last edited by Michael Duarte Goncalves; 13 Feb 2024, 02:24.

        Comment


        • #5
          Could anyone give me some feedback on #4, please?

          I don't understand my error(s), as I have defined properly the variables (or this I think).

          Thanks in advance for your help on that!
          Best,

          Michael

          Comment

          Working...
          X