Announcement

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

  • Problem with loop r(198) invalid name

    Dear Statalist community,

    I am using a loop with while in Stata 13.0 on Windows 10. I have the following code that I am running:

    Code:
    preserve
    set seed 339487731
    drawnorm SN_b1-SN_b26, n(1000000) means(e(b)) cov(e(V)) clear
    
    postutil clear
    postfile mypost prob_hat0 lo0 prob_hat1 lo1 hi1 diff_hat diff_lo diff_hi using "C:\Users\Anja\Documents\Esmées werk\Datasets\interaction_dataset2_test.dta", replace
    noisily display "start"
    
    
    
    local a = 0 
    while ‘a’ <= 810 {
        {
    scalar h_treatiesmem_open_a=5.686188
    scalar h_gmi_a=238.3265 
    scalar h_treatiesmem_open_b=5.736796 
    scalar h_gmi_b=231.458  
    scalar h_interaction_b=6544.354 
    scalar h_contig=.3270718 
    scalar h_comlang_ethno=.3071823 
    scalar h_colony=.0729282 
    scalar h_lndistcap=7.607629 
    scalar h_lngdp1_a=8.343579 
    scalar h_lngdp1_b=8.350814
    scalar h_lnpop_a=16.56221 
    scalar h_lnpop_b=16.48816 
    scalar h_Landlocked_a=.1403315 
    scalar h_Landlocked_b=.1546961     
    scalar h_Agri_gdp_a=15.7774 
    scalar h_Agri_gdp_b=17.01067 
    scalar h_com_law_a=.2981366             
    scalar h_com_law_b=.3240506 
    scalar h_polity2_a=-.1072913     
    scalar h_polity2_b=.2903407        
    scalar h_constant=1                
    
    generate x_betahat0 = SN_b1*h_treatiesmem_open_a ///
                            + SN_b2*‘a’ ///
                            + SN_b3*h_treatiesmem_open_a*‘a’ ///
                            + SN_b4*h_treatiesmem_open_b ///
                            + SN_b5*h_gmi_b ///
                            + SN_b6*h_interaction_b ///
                            + SN_b7*h_contig ///
                            + SN_b8*h_comlang_ethno ///
                            + SN_b9*h_colony ///
                            + SN_b10*h_lndistcap ///
                            + SN_b11*h_lngdp1_a ///
                            + SN_b12*h_lngdp1_b ///
                            + SN_b13*h_lnpop_a ///
                            + SN_b14*h_lnpop_b ///
                            + SN_b15*h_Landlocked_a ///
                            + SN_b16*h_Landlocked_b ///
                            + SN_b17*h_Agri_gdp_a ///
                            + SN_b18*h_Agri_gdp_b ///
                            + SN_b19*h_com_law_a ///
                            + SN_b20*h_com_law_b ///
                            + SN_b21*h_polity2_a ///
                            + SN_b22*h_polity2_b ///
                            + SN_b23*h_constant 
                            
    generate x_betahat1 = SN_b1*(h_treatiesmem_open_a+1) ///
                            + SN_b2*‘a’ ///
                            + SN_b3*(h_treatiesmem_open_a+1)*‘a’ ///
                            + SN_b4*h_treatiesmem_open_b ///
                            + SN_b5*h_gmi_b ///
                            + SN_b6*h_interaction_b ///
                            + SN_b7*h_contig ///
                            + SN_b8*h_comlang_ethno ///
                            + SN_b9*h_colony ///
                            + SN_b10*h_lndistcap ///
                            + SN_b11*h_lngdp1_a ///
                            + SN_b12*h_lngdp1_b ///
                            + SN_b13*h_lnpop_a ///
                            + SN_b14*h_lnpop_b ///
                            + SN_b15*h_Landlocked_a ///
                            + SN_b16*h_Landlocked_b ///
                            + SN_b17*h_Agri_gdp_a ///
                            + SN_b18*h_Agri_gdp_b ///
                            + SN_b19*h_com_law_a ///
                            + SN_b20*h_com_law_b ///
                            + SN_b21*h_polity2_a ///
                            + SN_b22*h_polity2_b ///
                            + SN_b23*h_constant 
    
    gen prob0=logistic(x_betahat0)
    gen prob1=logistic(x_betahat1)
    gen diff=prob1-prob0 
    
    egen probhat0=mean(prob0)
    egen probhat1=mean(prob1)
    egen diffhat=mean(diff)
    
    tempname prob_hat0 lo0 hi0 prob_hat1 lo1 hi1 diff_hat diff_lo diff_hi
    
    _pctile prob0, p(2.5,97.5) 
    scalar ‘lo0’ = r(r1)
    scalar ‘hi0’ = r(r2)
    
    _pctile prob1, p(2.5,97.5)
    scalar ‘lo1’= r(r1)
    scalar ‘hi1’= r(r2)
    
    _pctile diff, p(2.5,97.5)
    scalar ‘diff_lo’= r(r1)
    scalar ‘diff_hi’= r(r2)
    
    scalar ‘prob_hat0’=probhat0
    scalar ‘prob_hat1’=probhat1
    scalar ‘diff_hat’=diffhat
    
    post mypost (‘prob_hat0’) (‘lo0’) (‘hi0’) (‘prob_hat1’) (‘lo1’) (‘hi1’) ///
    (‘diff_hat’) (‘diff_lo’) (‘diff_hi’)
    
        }
    
    drop x_betahat0 x_betahat1 prob0 prob1 diff probhat0 probhat1 diffhat
    local a=‘a’+ 1
    display "." _c
    
    }
    
    
    display ""
    postclose mypost
    
    restore
    merge using "C:\Users\Anja\Documents\Esmées werk\Datasets\interaction_dataset2_test.dta"

    After executing, Stata is showing me the error (198): 'a' invalid name. When I try to trace the error it seems that Stata does not recognize 'a' the second time it runs the loop.

    I have tried to change '' into ‘’, I've run my entire code (and not only the loop), but nothing seems to solve the issue.

    Some more information: I use an ordered logit model and the dataset contains 23 variables. I use the loop to make a plot that shows the interaction between treatiesmem_open_a and gmi_a (following Matt Golder's example from http://mattgolder.com/files/interact...teraction3.pdf). The variable of interest ranges from 0-810 (that why I use the command while.) After scalar the mean of each variable is given.

    Thank you in advance. I am happy to provide more information.

    Jaantje

  • #2
    Welcome to Statalist.

    I'm not sure if that's the problem, but the grammatical rule to refer to a local/global variable is this:

    Code:
    `the_var'
    They are asymmetric quotes. The opening one is "grave accent", in US keyboard it's at the top left, next to "1" and often keyed together with tilde (~). The closing one is the usual single quotation mark.

    Also, make sure you're using straight quotation mark. If you typed all the code inside Stata do file editor or any code editor, it should be fine. If you have edited the code in MS Word, etc. The quote may be a slanted formatted quote, and I believe in earlier Stata version they may not work.
    Last edited by Ken Chui; 02 Jun 2022, 07:48.

    Comment


    • #3
      Thank you so much for your fast reply! Your suggestion worked.

      Best,
      Jaantje

      Comment


      • #4
        Glad that helped. Good luck with the analysis.

        Comment


        • #5
          Thank you!

          Comment

          Working...
          X