Quantcast

Bootstrapping to get Standard Errors for Regression Discontinuity Estimators

classic Classic list List threaded Threaded
25 messages Options
12
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Bootstrapping to get Standard Errors for Regression Discontinuity Estimators

Jen Zhen
Dear listers,

When bootstrapping Austin Nichol's rd command:

bs, reps(100): rd outcome assignment, mbw(100) ,

I find that often the resulting P value tells me the estimate is not
statistically significant at the conventional levels, even when visual
inspection and more basic methods like simple OLS regressions on a
treatment dummy, assignment and assignment squared suggest huge
statistical significance.

That makes me wonder whether possibly this boot-strapping method might
somehow understate the true statistical significance of the effect in
question? Or can and should I fully trust these results and conclude
that the estimate is not statistically significant at the conventional
levels?

Thanks and best regards,
JZ
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: Bootstrapping to get Standard Errors for Regression Discontinuity Estimators

Nick Cox
Jen Zhen:

You sent this email earlier on 20 September and it got no replies. In principle, I see four reasons for your perceived problem.

1. The theory underlying -rd- you are using is wrong, in that there are incorrect deductions from its assumptions.

2. The theory underlying -rd- does not fully apply to your data because assumptions are not satisfied in some serious sense.

3. Austin's code or Stata's bootstrap code is incorrect.

4. Your intuition is at fault.

Naturally, the explanations are not mutually exclusive.

My own guess is that your question is unanswerable. An expert in the field would doubt 1, anyone experienced in Stata would doubt 3, and we have no way of commenting on 2 or 4.

As the Statalist FAQ advises, it is better to revise a question not answered than just re-post it.

Nick
[hidden email]

Jen Zhen

When bootstrapping Austin Nichols' rd command:

bs, reps(100): rd outcome assignment, mbw(100) ,

I find that often the resulting P value tells me the estimate is not
statistically significant at the conventional levels, even when visual
inspection and more basic methods like simple OLS regressions on a
treatment dummy, assignment and assignment squared suggest huge
statistical significance.

That makes me wonder whether possibly this boot-strapping method might
somehow understate the true statistical significance of the effect in
question? Or can and should I fully trust these results and conclude
that the estimate is not statistically significant at the conventional
levels?


*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Bootstrapping to get Standard Errors for Regression Discontinuity Estimators

nshephard
Administrator
In reply to this post by Jen Zhen
Jen Zhen wrote
Dear listers,

When bootstrapping Austin Nichol's rd command:

bs, reps(100): rd outcome assignment, mbw(100) ,

I find that often the resulting P value tells me the estimate is not
statistically significant at the conventional levels, even when visual
inspection and more basic methods like simple OLS regressions on a
treatment dummy, assignment and assignment squared suggest huge
statistical significance.

That makes me wonder whether possibly this boot-strapping method might
somehow understate the true statistical significance of the effect in
question? Or can and should I fully trust these results and conclude
that the estimate is not statistically significant at the conventional
levels?"
What do you mean by "conventional levels [of significance]"?

You should set your threshold for declaring statistical significance in the context of your study.  Using p < 0.05 to declare something statistically significant is often inappropriate.

Often of greater interest is an estimate of the effect size (and associated CI's), what do these tell you?

see e.g. Gardner & Altman (1986) http://www.ncbi.nlm.nih.gov/pmc/articles/PMC1339793/pdf/bmjcred00225-0036.pdf 


Try more replications for your bootstrapping too, 100 isn't that many really, try at least 1000.

Neil
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Syntax forvalues in Stata 11 SE

Gauri Khanna
In reply to this post by Nick Cox

Hi,

I am a new user to the Stata 11.1 SE

I have run into syntax problems with the - forvalues- command while creating a loop.

My syntax which comes from Stata 9 is the following:

forvalues svy = 1/`max1' {
    use "'allsvys'", clear
    keep if i==`svy'
}

and I get the following error :

invalid syntax
r(198);

Subsequently, i would also like to run the following syntax (shortened for convenience)

foreach x of local names {
        if "`x'"=="lgsmkc" {       
            display "`x'"
            gen row`j'=lgcigc
            local j=`j'+1
        }
}

Can anyone help?

Gauri

     
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

AW: Syntax forvalues in Stata 11 SE

Martin Weiss-5

<>


How and when is -local- "max1" defined in your code? See this code:


*************
loc max1 2
forv i=1/`max1'{
di `i'
}

loc max1
forv i=1/`max1'{
di `i'
}
*************



HTH
Martin

-----Ursprüngliche Nachricht-----
Von: [hidden email]
[mailto:[hidden email]] Im Auftrag von Gauri Khanna
Gesendet: Donnerstag, 23. September 2010 13:53
An: Stata List
Betreff: st: Syntax forvalues in Stata 11 SE


Hi,

I am a new user to the Stata 11.1 SE

I have run into syntax problems with the - forvalues- command while creating
a loop.

My syntax which comes from Stata 9 is the following:

forvalues svy = 1/`max1' {
    use "'allsvys'", clear
    keep if i==`svy'
}

and I get the following error :

invalid syntax
r(198);

Subsequently, i would also like to run the following syntax (shortened for
convenience)

foreach x of local names {
        if "`x'"=="lgsmkc" {       
            display "`x'"
            gen row`j'=lgcigc
            local j=`j'+1
        }
}

Can anyone help?

Gauri

     
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Syntax forvalues in Stata 11 SE

nshephard
Administrator
In reply to this post by Gauri Khanna
On Thu, Sep 23, 2010 at 11:52 AM, Gauri Khanna <[hidden email]> wrote:

>
> Hi,
>
> I am a new user to the Stata 11.1 SE
>
> I have run into syntax problems with the - forvalues- command while creating a loop.
>
> My syntax which comes from Stata 9 is the following:
>
> forvalues svy = 1/`max1' {
>     use "'allsvys'", clear
>     keep if i==`svy'
> }
>
> and I get the following error :
>
> invalid syntax
> r(198);

What is the value of `max1' and how are you setting it prior to your loop?

You can use -set trace on- to look at how your -forval- line is
actually being interpreted.

>
> Subsequently, i would also like to run the following syntax (shortened for convenience)
>
> foreach x of local names {
>         if "`x'"=="lgsmkc" {
>             display "`x'"
>             gen row`j'=lgcigc
>             local j=`j'+1
>         }
> }
>
> Can anyone help?

What are you trying to achieve here, its not clear to me?

It looks like you'll create variables row1 to row[n] that will all be
identical to the variable 'lgcigc' which doesn't seem like a useful
thing to do.

Neil


--
"One should as a rule, respect public opinion in so far as is
necessary to avoid starvation and to keep out of prison, but anything
that goes beyond this is voluntary submission to an unnecessary
tyranny, and is likely to interfere with happiness in all kinds of
ways..." - Bertrand Russell

Email - [hidden email]
Website - http://slack.ser.man.ac.uk/
Photos - http://www.flickr.com/photos/slackline/

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: Syntax forvalues in Stata 11 SE

Nick Cox
In reply to this post by Gauri Khanna
The most obvious problem is the line

    use "'allsvys'", clear

in which the ' character should be `.

Nick
[hidden email]

Gauri Khanna

I am a new user to the Stata 11.1 SE

I have run into syntax problems with the - forvalues- command while creating a loop.

My syntax which comes from Stata 9 is the following:

forvalues svy = 1/`max1' {
    use "'allsvys'", clear
    keep if i==`svy'
}

and I get the following error :

invalid syntax
r(198);

Subsequently, i would also like to run the following syntax (shortened for convenience)

foreach x of local names {
        if "`x'"=="lgsmkc" {       
            display "`x'"
            gen row`j'=lgcigc
            local j=`j'+1
        }
}


*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: RE: Syntax forvalues in Stata 11 SE

Nick Cox
... meaning the first such character.

Nick
[hidden email]

Nick Cox

The most obvious problem is the line

    use "'allsvys'", clear

in which the ' character should be `.

Gauri Khanna

I am a new user to the Stata 11.1 SE

I have run into syntax problems with the - forvalues- command while creating a loop.

My syntax which comes from Stata 9 is the following:

forvalues svy = 1/`max1' {
    use "'allsvys'", clear
    keep if i==`svy'
}

and I get the following error :

invalid syntax
r(198);

Subsequently, i would also like to run the following syntax (shortened for convenience)

foreach x of local names {
        if "`x'"=="lgsmkc" {       
            display "`x'"
            gen row`j'=lgcigc
            local j=`j'+1
        }
}


*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: AW: Syntax forvalues in Stata 11 SE

Gauri Khanna
In reply to this post by Martin Weiss-5
Thank you for your responses Martin, Neil and Nick.

I have defined -local-max1 and here is a more detailed code which shows temporary files as well as draws on other previously saved datasets. I have ended the do file abruptly to try and isolate the syntax problem.

The codes work up to the -forvalues- codes which is the last three lines.

Note that I changed the syntax for the temporary files from ` to ' i.e. `allsvys' to 'allsyvs' which did not run in Stata 11.

Thanks. Gauri

****************************
use  "reshape.dta", clear
gen totalind=0

append using "D:\UnitData\0  NCDInfoBase\1.  Analysis Team Only - Harmonization\d. Tobacco Use\Datasets - Results\7 total age grp\total age raw.dta"
replace totalind=1 if totalind==.
replace total_age_group=0 if totalind==1

gen sex=0 if sex_code==1
replace sex=1 if sex_code==2

gen agegrp=1 if midage<17
replace agegrp=0 if start_age==16 & end_age==16
replace agegrp=0 if agegrp==.

*drop untrustworthy WHS, they are BIH, Kazah & Slovakia
*drop if source_code==101692 | source_code==101731 (commented out, reg models already exclude those surveys)
 
*tab un_region if lgsmkd !=. & lgsmkc !=. & sex!=. & total_age_group!=1
*tab un_region

gen lgsmkd=.
gen lgsmkc=.
gen lgcigd=.
gen lgcigc=.
gen agesmkd=.
gen agesmkc=.
gen agecigd=.
gen agecigc=.

gen smk_d_est=.
gen smk_c_est=.
gen cig_d_est=.
gen cig_c_est=.

gen str60 trace21=""

gen r2d=.
gen r2c=.

gen un_region2=un_region
replace un_region2="Oceania" if un_region =="Melanesia" | un_region=="Polynesia" | un_region=="Micronesia"
replace un_region2="Eastern Europe" if un_name=="Armenia" |  un_name=="Azerbaijan" |  un_name=="Georgia" |  un_name=="Turkey" |  un_name=="Uzbekistan" |  un_name=="Turkmenistan" |  un_name=="Kazakhstan" |  un_name=="Kyrgyzstan" | un_name=="Tajikistan" 
replace un_region2="Southern Europe" if un_name=="Cyprus" | un_name=="Israel"

*save `allsvys1', replace /*OLD CODE: STATA 9*/
save "'allsvys1'", replace
drop if sex_code!=3 & total_age_group!=1 & agegrp==0
save "conversion1.dta", replace

****Regression****

**for age 16 and more**

set more off
set matsize 5000

*use `allsvys1', clear /*OLD CODE: STATA 9*/
use "'allsvys1'", clear
egen i=group(un_region2 sex_code) if sex_code!=3 & total_age_group!=1 & agegrp==0
summarize i
local max1=r(max)
gen error1=.
gen error2=.
*save `allsvys', replace /*OLD CODE: STATA 9*/
save "'allsvys'", replace

forvalues svy = 1/'max1' {
    use "`allsvys'", clear
    keep if i==`svy'
}


Thanks,

Gauri

----------------------------------------

> From: [hidden email]
> To: [hidden email]
> Subject: st: AW: Syntax forvalues in Stata 11 SE
> Date: Thu, 23 Sep 2010 13:55:33 +0200
>
>
> <>
>
>
> How and when is -local- "max1" defined in your code? See this code:
>
>
> *************
> loc max1 2
> forv i=1/`max1'{
> di `i'
> }
>
> loc max1
> forv i=1/`max1'{
> di `i'
> }
> *************
>
>
>
> HTH
> Martin
>
> -----Ursprüngliche Nachricht-----
> Von: [hidden email]
> [mailto:[hidden email]] Im Auftrag von Gauri Khanna
> Gesendet: Donnerstag, 23. September 2010 13:53
> An: Stata List
> Betreff: st: Syntax forvalues in Stata 11 SE
>
>
> Hi,
>
> I am a new user to the Stata 11.1 SE
>
> I have run into syntax problems with the - forvalues- command while creating
> a loop.
>
> My syntax which comes from Stata 9 is the following:
>
> forvalues svy = 1/`max1' {
>     use "'allsvys'", clear
>     keep if i==`svy'
> }
>
> and I get the following error :
>
> invalid syntax
> r(198);
>
> Subsequently, i would also like to run the following syntax (shortened for
> convenience)
>
> foreach x of local names {
>         if "`x'"=="lgsmkc" {
>             display "`x'"
>             gen row`j'=lgcigc
>             local j=`j'+1
>         }
> }
>
> Can anyone help?
>
> Gauri
>
>
> *
> * For searches and help try:
> * http://www.stata.com/help.cgi?search
> * http://www.stata.com/support/statalist/faq
> * http://www.ats.ucla.edu/stat/stata/
>
>
> *
> * For searches and help try:
> * http://www.stata.com/help.cgi?search
> * http://www.stata.com/support/statalist/faq
> * http://www.ats.ucla.edu/stat/stata/
     
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: Revised - Syntax forvalues in Stata 11 SE -

Gauri Khanna
Here are the corrected codes - please ignore earlier email:

Gauri
Thank you for your responses Martin, Neil and Nick.
 
I have defined -local-max1 and here is a more detailed code which shows temporary files as well as draws on other previously saved datasets. I have ended the do file abruptly to try and isolate the syntax problem.
 
The codes work up to the -forvalues- codes which is the last three lines.
 
Note that I changed the syntax for the temporary files from ` to ' i.e. `allsvys' to 'allsyvs' which did not run in Stata 11.
 
Thanks. Gauri
 
****************************
use  "reshape.dta", clear
gen totalind=0
 
append using "D:\UnitData\0  NCDInfoBase\1.  Analysis Team Only - Harmonization\d. Tobacco Use\Datasets - Results\7 total age grp\total age raw.dta"
replace totalind=1 if totalind==.
replace total_age_group=0 if totalind==1
 
gen sex=0 if sex_code==1
replace sex=1 if sex_code==2
 
gen agegrp=1 if midage<17
replace agegrp=0 if start_age==16 & end_age==16
replace agegrp=0 if agegrp==.
 
*drop untrustworthy WHS, they are BIH, Kazah & Slovakia
*drop if source_code==101692 | source_code==101731 (commented out, reg models already exclude those surveys)
 
*tab un_region if lgsmkd !=. & lgsmkc !=. & sex!=. & total_age_group!=1
*tab un_region
 
gen lgsmkd=.
gen lgsmkc=.
gen lgcigd=.
gen lgcigc=.
gen agesmkd=.
gen agesmkc=.
gen agecigd=.
gen agecigc=.
 
gen smk_d_est=.
gen smk_c_est=.
gen cig_d_est=.
gen cig_c_est=.
 
gen str60 trace21=""
 
gen r2d=.
gen r2c=.
 
gen un_region2=un_region
replace un_region2="Oceania" if un_region =="Melanesia" | un_region=="Polynesia" | un_region=="Micronesia"
replace un_region2="Eastern Europe" if un_name=="Armenia" |  un_name=="Azerbaijan" |  un_name=="Georgia" |  un_name=="Turkey" |  un_name=="Uzbekistan" |  un_name=="Turkmenistan" |  un_name=="Kazakhstan" |  un_name=="Kyrgyzstan" | un_name=="Tajikistan" 
replace un_region2="Southern Europe" if un_name=="Cyprus" | un_name=="Israel"
 
*save `allsvys1', replace /*OLD CODE: STATA 9*/
save "'allsvys1'", replace
drop if sex_code!=3 & total_age_group!=1 & agegrp==0
save "conversion1.dta", replace
 
****Regression****
 
**for age 16 and more**
 
set more off
set matsize 5000
 
*use `allsvys1', clear /*OLD CODE: STATA 9*/
use "'allsvys1'", clear
egen i=group(un_region2 sex_code) if sex_code!=3 & total_age_group!=1 & agegrp==0
summarize i
local max1=r(max)
gen error1=.
gen error2=.
*save `allsvys', replace /*OLD CODE: STATA 9*/
save "'allsvys'", replace
 
forvalues svy = 1/`max1' {
    use "'allsvys'", clear
    keep if i==`svy'
}
 
 
Thanks,
 
Gauri

----------------------------------------

> From: [hidden email]
> To: [hidden email]
> Subject: RE: st: AW: Syntax forvalues in Stata 11 SE
> Date: Thu, 23 Sep 2010 12:10:31 +0000
>
> Thank you for your responses Martin, Neil and Nick.
>
> I have defined -local-max1 and here is a more detailed code which shows temporary files as well as draws on other previously saved datasets. I have ended the do file abruptly to try and isolate the syntax problem.
>
> The codes work up to the -forvalues- codes which is the last three lines.
>
> Note that I changed the syntax for the temporary files from ` to ' i.e. `allsvys' to 'allsyvs' which did not run in Stata 11.
>
> Thanks. Gauri
>
> ****************************
> use  "reshape.dta", clear
> gen totalind=0
>
> append using "D:\UnitData\0  NCDInfoBase\1.  Analysis Team Only - Harmonization\d. Tobacco Use\Datasets - Results\7 total age grp\total age raw.dta"
> replace totalind=1 if totalind==.
> replace total_age_group=0 if totalind==1
>
> gen sex=0 if sex_code==1
> replace sex=1 if sex_code==2
>
> gen agegrp=1 if midage<17
> replace agegrp=0 if start_age==16 & end_age==16
> replace agegrp=0 if agegrp==.
>
> *drop untrustworthy WHS, they are BIH, Kazah & Slovakia
> *drop if source_code==101692 | source_code==101731 (commented out, reg models already exclude those surveys)
>
> *tab un_region if lgsmkd !=. & lgsmkc !=. & sex!=. & total_age_group!=1
> *tab un_region
>
> gen lgsmkd=.
> gen lgsmkc=.
> gen lgcigd=.
> gen lgcigc=.
> gen agesmkd=.
> gen agesmkc=.
> gen agecigd=.
> gen agecigc=.
>
> gen smk_d_est=.
> gen smk_c_est=.
> gen cig_d_est=.
> gen cig_c_est=.
>
> gen str60 trace21=""
>
> gen r2d=.
> gen r2c=.
>
> gen un_region2=un_region
> replace un_region2="Oceania" if un_region =="Melanesia" | un_region=="Polynesia" | un_region=="Micronesia"
> replace un_region2="Eastern Europe" if un_name=="Armenia" |  un_name=="Azerbaijan" |  un_name=="Georgia" |  un_name=="Turkey" |  un_name=="Uzbekistan" |  un_name=="Turkmenistan" |  un_name=="Kazakhstan" |  un_name=="Kyrgyzstan" | un_name=="Tajikistan"
> replace un_region2="Southern Europe" if un_name=="Cyprus" | un_name=="Israel"
>
> *save `allsvys1', replace /*OLD CODE: STATA 9*/
> save "'allsvys1'", replace
> drop if sex_code!=3 & total_age_group!=1 & agegrp==0
> save "conversion1.dta", replace
>
> ****Regression****
>
> **for age 16 and more**
>
> set more off
> set matsize 5000
>
> *use `allsvys1', clear /*OLD CODE: STATA 9*/
> use "'allsvys1'", clear
> egen i=group(un_region2 sex_code) if sex_code!=3 & total_age_group!=1 & agegrp==0
> summarize i
> local max1=r(max)
> gen error1=.
> gen error2=.
> *save `allsvys', replace /*OLD CODE: STATA 9*/
> save "'allsvys'", replace
>
> forvalues svy = 1/'max1' {
>     use "`allsvys'", clear
>     keep if i==`svy'
> }
>
>
> Thanks,
>
> Gauri
>
> ----------------------------------------
>> From: [hidden email]
>> To: [hidden email]
>> Subject: st: AW: Syntax forvalues in Stata 11 SE
>> Date: Thu, 23 Sep 2010 13:55:33 +0200
>>
>>
>> <>
>>
>>
>> How and when is -local- "max1" defined in your code? See this code:
>>
>>
>> *************
>> loc max1 2
>> forv i=1/`max1'{
>> di `i'
>> }
>>
>> loc max1
>> forv i=1/`max1'{
>> di `i'
>> }
>> *************
>>
>>
>>
>> HTH
>> Martin
>>
>> -----Ursprüngliche Nachricht-----
>> Von: [hidden email]
>> [mailto:[hidden email]] Im Auftrag von Gauri Khanna
>> Gesendet: Donnerstag, 23. September 2010 13:53
>> An: Stata List
>> Betreff: st: Syntax forvalues in Stata 11 SE
>>
>>
>> Hi,
>>
>> I am a new user to the Stata 11.1 SE
>>
>> I have run into syntax problems with the - forvalues- command while creating
>> a loop.
>>
>> My syntax which comes from Stata 9 is the following:
>>
>> forvalues svy = 1/`max1' {
>> use "'allsvys'", clear
>> keep if i==`svy'
>> }
>>
>> and I get the following error :
>>
>> invalid syntax
>> r(198);
>>
>> Subsequently, i would also like to run the following syntax (shortened for
>> convenience)
>>
>> foreach x of local names {
>> if "`x'"=="lgsmkc" {
>> display "`x'"
>> gen row`j'=lgcigc
>> local j=`j'+1
>> }
>> }
>>
>> Can anyone help?
>>
>> Gauri
>>
>>
>> *
>> * For searches and help try:
>> * http://www.stata.com/help.cgi?search
>> * http://www.stata.com/support/statalist/faq
>> * http://www.ats.ucla.edu/stat/stata/
>>
>>
>> *
>> * For searches and help try:
>> * http://www.stata.com/help.cgi?search
>> * http://www.stata.com/support/statalist/faq
>> * http://www.ats.ucla.edu/stat/stata/
>
> *
> * For searches and help try:
> * http://www.stata.com/help.cgi?search
> * http://www.stata.com/support/statalist/faq
> * http://www.ats.ucla.edu/stat/stata/
     
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: AW: Syntax forvalues in Stata 11 SE

Nick Cox
In reply to this post by Gauri Khanna
As you say, 'allsyvs' will not work in Stata 11 (or 10 (or ...))) because it is incorrect.

"`allsyvs'" is the appropriate call.

The " " are needed if the filename contains any spaces.

This all presumes that a local macro has been defined upstream of the code you cite.

Nick
[hidden email]

Gauri Khanna

Thank you for your responses Martin, Neil and Nick.

I have defined -local-max1 and here is a more detailed code which shows temporary files as well as draws on other previously saved datasets. I have ended the do file abruptly to try and isolate the syntax problem.

The codes work up to the -forvalues- codes which is the last three lines.

Note that I changed the syntax for the temporary files from ` to ' i.e. `allsvys' to 'allsyvs' which did not run in Stata 11.

Thanks. Gauri

****************************
use  "reshape.dta", clear
gen totalind=0

append using "D:\UnitData\0  NCDInfoBase\1.  Analysis Team Only - Harmonization\d. Tobacco Use\Datasets - Results\7 total age grp\total age raw.dta"
replace totalind=1 if totalind==.
replace total_age_group=0 if totalind==1

gen sex=0 if sex_code==1
replace sex=1 if sex_code==2

gen agegrp=1 if midage<17
replace agegrp=0 if start_age==16 & end_age==16
replace agegrp=0 if agegrp==.

*drop untrustworthy WHS, they are BIH, Kazah & Slovakia
*drop if source_code==101692 | source_code==101731 (commented out, reg models already exclude those surveys)
 
*tab un_region if lgsmkd !=. & lgsmkc !=. & sex!=. & total_age_group!=1
*tab un_region

gen lgsmkd=.
gen lgsmkc=.
gen lgcigd=.
gen lgcigc=.
gen agesmkd=.
gen agesmkc=.
gen agecigd=.
gen agecigc=.

gen smk_d_est=.
gen smk_c_est=.
gen cig_d_est=.
gen cig_c_est=.

gen str60 trace21=""

gen r2d=.
gen r2c=.

gen un_region2=un_region
replace un_region2="Oceania" if un_region =="Melanesia" | un_region=="Polynesia" | un_region=="Micronesia"
replace un_region2="Eastern Europe" if un_name=="Armenia" |  un_name=="Azerbaijan" |  un_name=="Georgia" |  un_name=="Turkey" |  un_name=="Uzbekistan" |  un_name=="Turkmenistan" |  un_name=="Kazakhstan" |  un_name=="Kyrgyzstan" | un_name=="Tajikistan" 
replace un_region2="Southern Europe" if un_name=="Cyprus" | un_name=="Israel"

*save `allsvys1', replace /*OLD CODE: STATA 9*/
save "'allsvys1'", replace
drop if sex_code!=3 & total_age_group!=1 & agegrp==0
save "conversion1.dta", replace

****Regression****

**for age 16 and more**

set more off
set matsize 5000

*use `allsvys1', clear /*OLD CODE: STATA 9*/
use "'allsvys1'", clear
egen i=group(un_region2 sex_code) if sex_code!=3 & total_age_group!=1 & agegrp==0
summarize i
local max1=r(max)
gen error1=.
gen error2=.
*save `allsvys', replace /*OLD CODE: STATA 9*/
save "'allsvys'", replace

forvalues svy = 1/'max1' {
    use "`allsvys'", clear
    keep if i==`svy'
}


Thanks,

Gauri

----------------------------------------

> From: [hidden email]
> To: [hidden email]
> Subject: st: AW: Syntax forvalues in Stata 11 SE
> Date: Thu, 23 Sep 2010 13:55:33 +0200
>
>
> <>
>
>
> How and when is -local- "max1" defined in your code? See this code:
>
>
> *************
> loc max1 2
> forv i=1/`max1'{
> di `i'
> }
>
> loc max1
> forv i=1/`max1'{
> di `i'
> }
> *************
>
>
>
> HTH
> Martin
>
> -----Ursprüngliche Nachricht-----
> Von: [hidden email]
> [mailto:[hidden email]] Im Auftrag von Gauri Khanna
> Gesendet: Donnerstag, 23. September 2010 13:53
> An: Stata List
> Betreff: st: Syntax forvalues in Stata 11 SE
>
>
> Hi,
>
> I am a new user to the Stata 11.1 SE
>
> I have run into syntax problems with the - forvalues- command while creating
> a loop.
>
> My syntax which comes from Stata 9 is the following:
>
> forvalues svy = 1/`max1' {
>     use "'allsvys'", clear
>     keep if i==`svy'
> }
>
> and I get the following error :
>
> invalid syntax
> r(198);
>
> Subsequently, i would also like to run the following syntax (shortened for
> convenience)
>
> foreach x of local names {
>         if "`x'"=="lgsmkc" {
>             display "`x'"
>             gen row`j'=lgcigc
>             local j=`j'+1
>         }
> }
>
> Can anyone help?

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

AW: st: AW: Syntax forvalues in Stata 11 SE

Martin Weiss-5
In reply to this post by Gauri Khanna

<>

In

*************
forvalues svy = 1/'max1' {
*************

change the left single quote to `, as NJC has said...


HTH
Martin


-----Ursprüngliche Nachricht-----
Von: [hidden email]
[mailto:[hidden email]] Im Auftrag von Gauri Khanna
Gesendet: Donnerstag, 23. September 2010 14:11
An: Stata List
Betreff: RE: st: AW: Syntax forvalues in Stata 11 SE

Thank you for your responses Martin, Neil and Nick.

I have defined -local-max1 and here is a more detailed code which shows
temporary files as well as draws on other previously saved datasets. I have
ended the do file abruptly to try and isolate the syntax problem.

The codes work up to the -forvalues- codes which is the last three lines.

Note that I changed the syntax for the temporary files from ` to ' i.e.
`allsvys' to 'allsyvs' which did not run in Stata 11.

Thanks. Gauri

****************************
use  "reshape.dta", clear
gen totalind=0

append using "D:\UnitData\0  NCDInfoBase\1.  Analysis Team Only -
Harmonization\d. Tobacco Use\Datasets - Results\7 total age grp\total age
raw.dta"
replace totalind=1 if totalind==.
replace total_age_group=0 if totalind==1

gen sex=0 if sex_code==1
replace sex=1 if sex_code==2

gen agegrp=1 if midage<17
replace agegrp=0 if start_age==16 & end_age==16
replace agegrp=0 if agegrp==.

*drop untrustworthy WHS, they are BIH, Kazah & Slovakia
*drop if source_code==101692 | source_code==101731 (commented out, reg
models already exclude those surveys)
 
*tab un_region if lgsmkd !=. & lgsmkc !=. & sex!=. & total_age_group!=1
*tab un_region

gen lgsmkd=.
gen lgsmkc=.
gen lgcigd=.
gen lgcigc=.
gen agesmkd=.
gen agesmkc=.
gen agecigd=.
gen agecigc=.

gen smk_d_est=.
gen smk_c_est=.
gen cig_d_est=.
gen cig_c_est=.

gen str60 trace21=""

gen r2d=.
gen r2c=.

gen un_region2=un_region
replace un_region2="Oceania" if un_region =="Melanesia" |
un_region=="Polynesia" | un_region=="Micronesia"
replace un_region2="Eastern Europe" if un_name=="Armenia" | 
un_name=="Azerbaijan" |  un_name=="Georgia" |  un_name=="Turkey" | 
un_name=="Uzbekistan" |  un_name=="Turkmenistan" |  un_name=="Kazakhstan" | 
un_name=="Kyrgyzstan" | un_name=="Tajikistan" 
replace un_region2="Southern Europe" if un_name=="Cyprus" |
un_name=="Israel"

*save `allsvys1', replace /*OLD CODE: STATA 9*/
save "'allsvys1'", replace
drop if sex_code!=3 & total_age_group!=1 & agegrp==0
save "conversion1.dta", replace

****Regression****

**for age 16 and more**

set more off
set matsize 5000

*use `allsvys1', clear /*OLD CODE: STATA 9*/
use "'allsvys1'", clear
egen i=group(un_region2 sex_code) if sex_code!=3 & total_age_group!=1 &
agegrp==0
summarize i
local max1=r(max)
gen error1=.
gen error2=.
*save `allsvys', replace /*OLD CODE: STATA 9*/
save "'allsvys'", replace

forvalues svy = 1/'max1' {
    use "`allsvys'", clear
    keep if i==`svy'
}


Thanks,

Gauri

----------------------------------------

> From: [hidden email]
> To: [hidden email]
> Subject: st: AW: Syntax forvalues in Stata 11 SE
> Date: Thu, 23 Sep 2010 13:55:33 +0200
>
>
> <>
>
>
> How and when is -local- "max1" defined in your code? See this code:
>
>
> *************
> loc max1 2
> forv i=1/`max1'{
> di `i'
> }
>
> loc max1
> forv i=1/`max1'{
> di `i'
> }
> *************
>
>
>
> HTH
> Martin
>
> -----Ursprüngliche Nachricht-----
> Von: [hidden email]
> [mailto:[hidden email]] Im Auftrag von Gauri Khanna
> Gesendet: Donnerstag, 23. September 2010 13:53
> An: Stata List
> Betreff: st: Syntax forvalues in Stata 11 SE
>
>
> Hi,
>
> I am a new user to the Stata 11.1 SE
>
> I have run into syntax problems with the - forvalues- command while
creating

> a loop.
>
> My syntax which comes from Stata 9 is the following:
>
> forvalues svy = 1/`max1' {
>     use "'allsvys'", clear
>     keep if i==`svy'
> }
>
> and I get the following error :
>
> invalid syntax
> r(198);
>
> Subsequently, i would also like to run the following syntax (shortened for
> convenience)
>
> foreach x of local names {
>         if "`x'"=="lgsmkc" {
>             display "`x'"
>             gen row`j'=lgcigc
>             local j=`j'+1
>         }
> }
>
> Can anyone help?
>
> Gauri
>
>
> *
> * For searches and help try:
> * http://www.stata.com/help.cgi?search
> * http://www.stata.com/support/statalist/faq
> * http://www.ats.ucla.edu/stat/stata/
>
>
> *
> * For searches and help try:
> * http://www.stata.com/help.cgi?search
> * http://www.stata.com/support/statalist/faq
> * http://www.ats.ucla.edu/stat/stata/
     
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

AW: RE: Syntax forvalues in Stata 11 SE

Martin Weiss-5
In reply to this post by Nick Cox

<>

" The most obvious problem is the line..."


Bu would this lead to an "invalid syntax" error?


HTH
Martin

-----Ursprüngliche Nachricht-----
Von: [hidden email]
[mailto:[hidden email]] Im Auftrag von Nick Cox
Gesendet: Donnerstag, 23. September 2010 14:00
An: '[hidden email]'
Betreff: st: RE: Syntax forvalues in Stata 11 SE

The most obvious problem is the line

    use "'allsvys'", clear

in which the ' character should be `.

Nick
[hidden email]

Gauri Khanna

I am a new user to the Stata 11.1 SE

I have run into syntax problems with the - forvalues- command while creating
a loop.

My syntax which comes from Stata 9 is the following:

forvalues svy = 1/`max1' {
    use "'allsvys'", clear
    keep if i==`svy'
}

and I get the following error :

invalid syntax
r(198);

Subsequently, i would also like to run the following syntax (shortened for
convenience)

foreach x of local names {
        if "`x'"=="lgsmkc" {       
            display "`x'"
            gen row`j'=lgcigc
            local j=`j'+1
        }
}


*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: AW: RE: Syntax forvalues in Stata 11 SE

Nick Cox
I don't know. But as Gauri's code seem to flip randomly in terms of different quote characters, I wonder whether he/she has different versions of supposedly the same code which are being sent in different posts.

Nick
[hidden email]

Martin Weiss

" The most obvious problem is the line..."

Bu would this lead to an "invalid syntax" error?

Nick Cox

The most obvious problem is the line

    use "'allsvys'", clear

in which the ' character should be `.

Gauri Khanna

I am a new user to the Stata 11.1 SE

I have run into syntax problems with the - forvalues- command while creating
a loop.

My syntax which comes from Stata 9 is the following:

forvalues svy = 1/`max1' {
    use "'allsvys'", clear
    keep if i==`svy'
}

and I get the following error :

invalid syntax
r(198);

Subsequently, i would also like to run the following syntax (shortened for
convenience)

foreach x of local names {
        if "`x'"=="lgsmkc" {       
            display "`x'"
            gen row`j'=lgcigc
            local j=`j'+1
        }
}


*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: AW: Syntax forvalues in Stata 11 SE

Gauri Khanna
In reply to this post by Nick Cox
Hello Nick, Martin and Neil,

I have tried what you suggested and I still get the following error:

. forvalues svy = 1/`max1' {
  2.         use "`allsvys'", clear
  3.         keep if i==`svy'
  4. }
invalid syntax
r(198);

If I remove the double quotes around -allsvys- I get the same error:

. forvalues svy = 1/`max1' {
  2.         use `allsvys', clear
  3.         keep if i==`svy'
  4. }
invalid syntax
r(198);


Many thanks for your continued attention.

Gauri

----------------------------------------

> From: [hidden email]
> To: [hidden email]
> Date: Thu, 23 Sep 2010 13:18:36 +0100
> Subject: RE: st: AW: Syntax forvalues in Stata 11 SE
>
> As you say, 'allsyvs' will not work in Stata 11 (or 10 (or ...))) because it is incorrect.
>
> "`allsyvs'" is the appropriate call.
>
> The " " are needed if the filename contains any spaces.
>
> This all presumes that a local macro has been defined upstream of the code you cite.
>
> Nick
> [hidden email]
>
> Gauri Khanna
>
> Thank you for your responses Martin, Neil and Nick.
>
> I have defined -local-max1 and here is a more detailed code which shows temporary files as well as draws on other previously saved datasets. I have ended the do file abruptly to try and isolate the syntax problem.
>
> The codes work up to the -forvalues- codes which is the last three lines.
>
> Note that I changed the syntax for the temporary files from ` to ' i.e. `allsvys' to 'allsyvs' which did not run in Stata 11.
>
> Thanks. Gauri
>
> ****************************
> use  "reshape.dta", clear
> gen totalind=0
>
> append using "D:\UnitData\0  NCDInfoBase\1.  Analysis Team Only - Harmonization\d. Tobacco Use\Datasets - Results\7 total age grp\total age raw.dta"
> replace totalind=1 if totalind==.
> replace total_age_group=0 if totalind==1
>
> gen sex=0 if sex_code==1
> replace sex=1 if sex_code==2
>
> gen agegrp=1 if midage<17
> replace agegrp=0 if start_age==16 & end_age==16
> replace agegrp=0 if agegrp==.
>
> *drop untrustworthy WHS, they are BIH, Kazah & Slovakia
> *drop if source_code==101692 | source_code==101731 (commented out, reg models already exclude those surveys)
>
> *tab un_region if lgsmkd !=. & lgsmkc !=. & sex!=. & total_age_group!=1
> *tab un_region
>
> gen lgsmkd=.
> gen lgsmkc=.
> gen lgcigd=.
> gen lgcigc=.
> gen agesmkd=.
> gen agesmkc=.
> gen agecigd=.
> gen agecigc=.
>
> gen smk_d_est=.
> gen smk_c_est=.
> gen cig_d_est=.
> gen cig_c_est=.
>
> gen str60 trace21=""
>
> gen r2d=.
> gen r2c=.
>
> gen un_region2=un_region
> replace un_region2="Oceania" if un_region =="Melanesia" | un_region=="Polynesia" | un_region=="Micronesia"
> replace un_region2="Eastern Europe" if un_name=="Armenia" |  un_name=="Azerbaijan" |  un_name=="Georgia" |  un_name=="Turkey" |  un_name=="Uzbekistan" |  un_name=="Turkmenistan" |  un_name=="Kazakhstan" |  un_name=="Kyrgyzstan" | un_name=="Tajikistan"
> replace un_region2="Southern Europe" if un_name=="Cyprus" | un_name=="Israel"
>
> *save `allsvys1', replace /*OLD CODE: STATA 9*/
> save "'allsvys1'", replace
> drop if sex_code!=3 & total_age_group!=1 & agegrp==0
> save "conversion1.dta", replace
>
> ****Regression****
>
> **for age 16 and more**
>
> set more off
> set matsize 5000
>
> *use `allsvys1', clear /*OLD CODE: STATA 9*/
> use "'allsvys1'", clear
> egen i=group(un_region2 sex_code) if sex_code!=3 & total_age_group!=1 & agegrp==0
> summarize i
> local max1=r(max)
> gen error1=.
> gen error2=.
> *save `allsvys', replace /*OLD CODE: STATA 9*/
> save "'allsvys'", replace
>
> forvalues svy = 1/'max1' {
>     use "`allsvys'", clear
>     keep if i==`svy'
> }
>
>
> Thanks,
>
> Gauri
>
> ----------------------------------------
>> From: [hidden email]
>> To: [hidden email]
>> Subject: st: AW: Syntax forvalues in Stata 11 SE
>> Date: Thu, 23 Sep 2010 13:55:33 +0200
>>
>>
>> <>
>>
>>
>> How and when is -local- "max1" defined in your code? See this code:
>>
>>
>> *************
>> loc max1 2
>> forv i=1/`max1'{
>> di `i'
>> }
>>
>> loc max1
>> forv i=1/`max1'{
>> di `i'
>> }
>> *************
>>
>>
>>
>> HTH
>> Martin
>>
>> -----Ursprüngliche Nachricht-----
>> Von: [hidden email]
>> [mailto:[hidden email]] Im Auftrag von Gauri Khanna
>> Gesendet: Donnerstag, 23. September 2010 13:53
>> An: Stata List
>> Betreff: st: Syntax forvalues in Stata 11 SE
>>
>>
>> Hi,
>>
>> I am a new user to the Stata 11.1 SE
>>
>> I have run into syntax problems with the - forvalues- command while creating
>> a loop.
>>
>> My syntax which comes from Stata 9 is the following:
>>
>> forvalues svy = 1/`max1' {
>> use "'allsvys'", clear
>> keep if i==`svy'
>> }
>>
>> and I get the following error :
>>
>> invalid syntax
>> r(198);
>>
>> Subsequently, i would also like to run the following syntax (shortened for
>> convenience)
>>
>> foreach x of local names {
>> if "`x'"=="lgsmkc" {
>> display "`x'"
>> gen row`j'=lgcigc
>> local j=`j'+1
>> }
>> }
>>
>> Can anyone help?
>
> *
> * For searches and help try:
> * http://www.stata.com/help.cgi?search
> * http://www.stata.com/support/statalist/faq
> * http://www.ats.ucla.edu/stat/stata/
     
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

AW: st: AW: Syntax forvalues in Stata 11 SE

Martin Weiss-5

<>


-set trace on- and show us the area around the error...



HTH
Martin


-----Ursprüngliche Nachricht-----
Von: [hidden email]
[mailto:[hidden email]] Im Auftrag von Gauri Khanna
Gesendet: Donnerstag, 23. September 2010 14:28
An: Stata List
Betreff: RE: st: AW: Syntax forvalues in Stata 11 SE

Hello Nick, Martin and Neil,

I have tried what you suggested and I still get the following error:

. forvalues svy = 1/`max1' {
  2.         use "`allsvys'", clear
  3.         keep if i==`svy'
  4. }
invalid syntax
r(198);

If I remove the double quotes around -allsvys- I get the same error:

. forvalues svy = 1/`max1' {
  2.         use `allsvys', clear
  3.         keep if i==`svy'
  4. }
invalid syntax
r(198);


Many thanks for your continued attention.

Gauri

----------------------------------------
> From: [hidden email]
> To: [hidden email]
> Date: Thu, 23 Sep 2010 13:18:36 +0100
> Subject: RE: st: AW: Syntax forvalues in Stata 11 SE
>
> As you say, 'allsyvs' will not work in Stata 11 (or 10 (or ...))) because
it is incorrect.
>
> "`allsyvs'" is the appropriate call.
>
> The " " are needed if the filename contains any spaces.
>
> This all presumes that a local macro has been defined upstream of the code
you cite.
>
> Nick
> [hidden email]
>
> Gauri Khanna
>
> Thank you for your responses Martin, Neil and Nick.
>
> I have defined -local-max1 and here is a more detailed code which shows
temporary files as well as draws on other previously saved datasets. I have
ended the do file abruptly to try and isolate the syntax problem.
>
> The codes work up to the -forvalues- codes which is the last three lines.
>
> Note that I changed the syntax for the temporary files from ` to ' i.e.
`allsvys' to 'allsyvs' which did not run in Stata 11.
>
> Thanks. Gauri
>
> ****************************
> use  "reshape.dta", clear
> gen totalind=0
>
> append using "D:\UnitData\0  NCDInfoBase\1.  Analysis Team Only -
Harmonization\d. Tobacco Use\Datasets - Results\7 total age grp\total age
raw.dta"

> replace totalind=1 if totalind==.
> replace total_age_group=0 if totalind==1
>
> gen sex=0 if sex_code==1
> replace sex=1 if sex_code==2
>
> gen agegrp=1 if midage<17
> replace agegrp=0 if start_age==16 & end_age==16
> replace agegrp=0 if agegrp==.
>
> *drop untrustworthy WHS, they are BIH, Kazah & Slovakia
> *drop if source_code==101692 | source_code==101731 (commented out, reg
models already exclude those surveys)

>
> *tab un_region if lgsmkd !=. & lgsmkc !=. & sex!=. & total_age_group!=1
> *tab un_region
>
> gen lgsmkd=.
> gen lgsmkc=.
> gen lgcigd=.
> gen lgcigc=.
> gen agesmkd=.
> gen agesmkc=.
> gen agecigd=.
> gen agecigc=.
>
> gen smk_d_est=.
> gen smk_c_est=.
> gen cig_d_est=.
> gen cig_c_est=.
>
> gen str60 trace21=""
>
> gen r2d=.
> gen r2c=.
>
> gen un_region2=un_region
> replace un_region2="Oceania" if un_region =="Melanesia" |
un_region=="Polynesia" | un_region=="Micronesia"
> replace un_region2="Eastern Europe" if un_name=="Armenia" |
un_name=="Azerbaijan" |  un_name=="Georgia" |  un_name=="Turkey" |
un_name=="Uzbekistan" |  un_name=="Turkmenistan" |  un_name=="Kazakhstan" |
un_name=="Kyrgyzstan" | un_name=="Tajikistan"
> replace un_region2="Southern Europe" if un_name=="Cyprus" |
un_name=="Israel"

>
> *save `allsvys1', replace /*OLD CODE: STATA 9*/
> save "'allsvys1'", replace
> drop if sex_code!=3 & total_age_group!=1 & agegrp==0
> save "conversion1.dta", replace
>
> ****Regression****
>
> **for age 16 and more**
>
> set more off
> set matsize 5000
>
> *use `allsvys1', clear /*OLD CODE: STATA 9*/
> use "'allsvys1'", clear
> egen i=group(un_region2 sex_code) if sex_code!=3 & total_age_group!=1 &
agegrp==0

> summarize i
> local max1=r(max)
> gen error1=.
> gen error2=.
> *save `allsvys', replace /*OLD CODE: STATA 9*/
> save "'allsvys'", replace
>
> forvalues svy = 1/'max1' {
>     use "`allsvys'", clear
>     keep if i==`svy'
> }
>
>
> Thanks,
>
> Gauri
>
> ----------------------------------------
>> From: [hidden email]
>> To: [hidden email]
>> Subject: st: AW: Syntax forvalues in Stata 11 SE
>> Date: Thu, 23 Sep 2010 13:55:33 +0200
>>
>>
>> <>
>>
>>
>> How and when is -local- "max1" defined in your code? See this code:
>>
>>
>> *************
>> loc max1 2
>> forv i=1/`max1'{
>> di `i'
>> }
>>
>> loc max1
>> forv i=1/`max1'{
>> di `i'
>> }
>> *************
>>
>>
>>
>> HTH
>> Martin
>>
>> -----Ursprüngliche Nachricht-----
>> Von: [hidden email]
>> [mailto:[hidden email]] Im Auftrag von Gauri Khanna
>> Gesendet: Donnerstag, 23. September 2010 13:53
>> An: Stata List
>> Betreff: st: Syntax forvalues in Stata 11 SE
>>
>>
>> Hi,
>>
>> I am a new user to the Stata 11.1 SE
>>
>> I have run into syntax problems with the - forvalues- command while
creating

>> a loop.
>>
>> My syntax which comes from Stata 9 is the following:
>>
>> forvalues svy = 1/`max1' {
>> use "'allsvys'", clear
>> keep if i==`svy'
>> }
>>
>> and I get the following error :
>>
>> invalid syntax
>> r(198);
>>
>> Subsequently, i would also like to run the following syntax (shortened
for

>> convenience)
>>
>> foreach x of local names {
>> if "`x'"=="lgsmkc" {
>> display "`x'"
>> gen row`j'=lgcigc
>> local j=`j'+1
>> }
>> }
>>
>> Can anyone help?
>
> *
> * For searches and help try:
> * http://www.stata.com/help.cgi?search
> * http://www.stata.com/support/statalist/faq
> * http://www.ats.ucla.edu/stat/stata/
     
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: AW: st: AW: Syntax forvalues in Stata 11 SE

Gauri Khanna
Hi,


Here is the error with - set trace on -

. set trace on

. forvalues svy = 1/`max1' {
  2.         use `allsvys', clear
  3.         keep if i==`svy'
  4. }
- forvalues svy = 1/`max1' {
= forvalues svy = 1/ {
invalid syntax
r(198);

Many thanks, Gauri (a she)

----------------------------------------

> From: [hidden email]
> To: [hidden email]
> Subject: AW: st: AW: Syntax forvalues in Stata 11 SE
> Date: Thu, 23 Sep 2010 14:31:54 +0200
>
>
> <>
>
>
> -set trace on- and show us the area around the error...
>
>
>
> HTH
> Martin
>
>
> -----Ursprüngliche Nachricht-----
> Von: [hidden email]
> [mailto:[hidden email]] Im Auftrag von Gauri Khanna
> Gesendet: Donnerstag, 23. September 2010 14:28
> An: Stata List
> Betreff: RE: st: AW: Syntax forvalues in Stata 11 SE
>
> Hello Nick, Martin and Neil,
>
> I have tried what you suggested and I still get the following error:
>
> . forvalues svy = 1/`max1' {
>   2.         use "`allsvys'", clear
>   3.         keep if i==`svy'
>   4. }
> invalid syntax
> r(198);
>
> If I remove the double quotes around -allsvys- I get the same error:
>
> . forvalues svy = 1/`max1' {
>   2.         use `allsvys', clear
>   3.         keep if i==`svy'
>   4. }
> invalid syntax
> r(198);
>
>
> Many thanks for your continued attention.
>
> Gauri
>
> ----------------------------------------
>> From: [hidden email]
>> To: [hidden email]
>> Date: Thu, 23 Sep 2010 13:18:36 +0100
>> Subject: RE: st: AW: Syntax forvalues in Stata 11 SE
>>
>> As you say, 'allsyvs' will not work in Stata 11 (or 10 (or ...))) because
> it is incorrect.
>>
>> "`allsyvs'" is the appropriate call.
>>
>> The " " are needed if the filename contains any spaces.
>>
>> This all presumes that a local macro has been defined upstream of the code
> you cite.
>>
>> Nick
>> [hidden email]
>>
>> Gauri Khanna
>>
>> Thank you for your responses Martin, Neil and Nick.
>>
>> I have defined -local-max1 and here is a more detailed code which shows
> temporary files as well as draws on other previously saved datasets. I have
> ended the do file abruptly to try and isolate the syntax problem.
>>
>> The codes work up to the -forvalues- codes which is the last three lines.
>>
>> Note that I changed the syntax for the temporary files from ` to ' i.e.
> `allsvys' to 'allsyvs' which did not run in Stata 11.
>>
>> Thanks. Gauri
>>
>> ****************************
>> use "reshape.dta", clear
>> gen totalind=0
>>
>> append using "D:\UnitData\0 NCDInfoBase\1. Analysis Team Only -
> Harmonization\d. Tobacco Use\Datasets - Results\7 total age grp\total age
> raw.dta"
>> replace totalind=1 if totalind==.
>> replace total_age_group=0 if totalind==1
>>
>> gen sex=0 if sex_code==1
>> replace sex=1 if sex_code==2
>>
>> gen agegrp=1 if midage<17
>> replace agegrp=0 if start_age==16 & end_age==16
>> replace agegrp=0 if agegrp==.
>>
>> *drop untrustworthy WHS, they are BIH, Kazah & Slovakia
>> *drop if source_code==101692 | source_code==101731 (commented out, reg
> models already exclude those surveys)
>>
>> *tab un_region if lgsmkd !=. & lgsmkc !=. & sex!=. & total_age_group!=1
>> *tab un_region
>>
>> gen lgsmkd=.
>> gen lgsmkc=.
>> gen lgcigd=.
>> gen lgcigc=.
>> gen agesmkd=.
>> gen agesmkc=.
>> gen agecigd=.
>> gen agecigc=.
>>
>> gen smk_d_est=.
>> gen smk_c_est=.
>> gen cig_d_est=.
>> gen cig_c_est=.
>>
>> gen str60 trace21=""
>>
>> gen r2d=.
>> gen r2c=.
>>
>> gen un_region2=un_region
>> replace un_region2="Oceania" if un_region =="Melanesia" |
> un_region=="Polynesia" | un_region=="Micronesia"
>> replace un_region2="Eastern Europe" if un_name=="Armenia" |
> un_name=="Azerbaijan" | un_name=="Georgia" | un_name=="Turkey" |
> un_name=="Uzbekistan" | un_name=="Turkmenistan" | un_name=="Kazakhstan" |
> un_name=="Kyrgyzstan" | un_name=="Tajikistan"
>> replace un_region2="Southern Europe" if un_name=="Cyprus" |
> un_name=="Israel"
>>
>> *save `allsvys1', replace /*OLD CODE: STATA 9*/
>> save "'allsvys1'", replace
>> drop if sex_code!=3 & total_age_group!=1 & agegrp==0
>> save "conversion1.dta", replace
>>
>> ****Regression****
>>
>> **for age 16 and more**
>>
>> set more off
>> set matsize 5000
>>
>> *use `allsvys1', clear /*OLD CODE: STATA 9*/
>> use "'allsvys1'", clear
>> egen i=group(un_region2 sex_code) if sex_code!=3 & total_age_group!=1 &
> agegrp==0
>> summarize i
>> local max1=r(max)
>> gen error1=.
>> gen error2=.
>> *save `allsvys', replace /*OLD CODE: STATA 9*/
>> save "'allsvys'", replace
>>
>> forvalues svy = 1/'max1' {
>> use "`allsvys'", clear
>> keep if i==`svy'
>> }
>>
>>
>> Thanks,
>>
>> Gauri
>>
>> ----------------------------------------
>>> From: [hidden email]
>>> To: [hidden email]
>>> Subject: st: AW: Syntax forvalues in Stata 11 SE
>>> Date: Thu, 23 Sep 2010 13:55:33 +0200
>>>
>>>
>>> <>
>>>
>>>
>>> How and when is -local- "max1" defined in your code? See this code:
>>>
>>>
>>> *************
>>> loc max1 2
>>> forv i=1/`max1'{
>>> di `i'
>>> }
>>>
>>> loc max1
>>> forv i=1/`max1'{
>>> di `i'
>>> }
>>> *************
>>>
>>>
>>>
>>> HTH
>>> Martin
>>>
>>> -----Ursprüngliche Nachricht-----
>>> Von: [hidden email]
>>> [mailto:[hidden email]] Im Auftrag von Gauri Khanna
>>> Gesendet: Donnerstag, 23. September 2010 13:53
>>> An: Stata List
>>> Betreff: st: Syntax forvalues in Stata 11 SE
>>>
>>>
>>> Hi,
>>>
>>> I am a new user to the Stata 11.1 SE
>>>
>>> I have run into syntax problems with the - forvalues- command while
> creating
>>> a loop.
>>>
>>> My syntax which comes from Stata 9 is the following:
>>>
>>> forvalues svy = 1/`max1' {
>>> use "'allsvys'", clear
>>> keep if i==`svy'
>>> }
>>>
>>> and I get the following error :
>>>
>>> invalid syntax
>>> r(198);
>>>
>>> Subsequently, i would also like to run the following syntax (shortened
> for
>>> convenience)
>>>
>>> foreach x of local names {
>>> if "`x'"=="lgsmkc" {
>>> display "`x'"
>>> gen row`j'=lgcigc
>>> local j=`j'+1
>>> }
>>> }
>>>
>>> Can anyone help?
>>
>> *
>> * For searches and help try:
>> * http://www.stata.com/help.cgi?search
>> * http://www.stata.com/support/statalist/faq
>> * http://www.ats.ucla.edu/stat/stata/
>
> *
> * For searches and help try:
> * http://www.stata.com/help.cgi?search
> * http://www.stata.com/support/statalist/faq
> * http://www.ats.ucla.edu/stat/stata/
>
>
> *
> * For searches and help try:
> * http://www.stata.com/help.cgi?search
> * http://www.stata.com/support/statalist/faq
> * http://www.ats.ucla.edu/stat/stata/
     
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

AW: AW: st: AW: Syntax forvalues in Stata 11 SE

Martin Weiss-5

<>


So somehow "max1" is not defined, as suspected all along...



HTH
Martin


-----Ursprüngliche Nachricht-----
Von: [hidden email]
[mailto:[hidden email]] Im Auftrag von Gauri Khanna
Gesendet: Donnerstag, 23. September 2010 14:37
An: Stata List
Betreff: RE: AW: st: AW: Syntax forvalues in Stata 11 SE

Hi,


Here is the error with - set trace on -

. set trace on

. forvalues svy = 1/`max1' {
  2.         use `allsvys', clear
  3.         keep if i==`svy'
  4. }
- forvalues svy = 1/`max1' {
= forvalues svy = 1/ {
invalid syntax
r(198);

Many thanks, Gauri (a she)

----------------------------------------

> From: [hidden email]
> To: [hidden email]
> Subject: AW: st: AW: Syntax forvalues in Stata 11 SE
> Date: Thu, 23 Sep 2010 14:31:54 +0200
>
>
> <>
>
>
> -set trace on- and show us the area around the error...
>
>
>
> HTH
> Martin
>
>
> -----Ursprüngliche Nachricht-----
> Von: [hidden email]
> [mailto:[hidden email]] Im Auftrag von Gauri Khanna
> Gesendet: Donnerstag, 23. September 2010 14:28
> An: Stata List
> Betreff: RE: st: AW: Syntax forvalues in Stata 11 SE
>
> Hello Nick, Martin and Neil,
>
> I have tried what you suggested and I still get the following error:
>
> . forvalues svy = 1/`max1' {
>   2.         use "`allsvys'", clear
>   3.         keep if i==`svy'
>   4. }
> invalid syntax
> r(198);
>
> If I remove the double quotes around -allsvys- I get the same error:
>
> . forvalues svy = 1/`max1' {
>   2.         use `allsvys', clear
>   3.         keep if i==`svy'
>   4. }
> invalid syntax
> r(198);
>
>
> Many thanks for your continued attention.
>
> Gauri
>
> ----------------------------------------
>> From: [hidden email]
>> To: [hidden email]
>> Date: Thu, 23 Sep 2010 13:18:36 +0100
>> Subject: RE: st: AW: Syntax forvalues in Stata 11 SE
>>
>> As you say, 'allsyvs' will not work in Stata 11 (or 10 (or ...))) because
> it is incorrect.
>>
>> "`allsyvs'" is the appropriate call.
>>
>> The " " are needed if the filename contains any spaces.
>>
>> This all presumes that a local macro has been defined upstream of the
code

> you cite.
>>
>> Nick
>> [hidden email]
>>
>> Gauri Khanna
>>
>> Thank you for your responses Martin, Neil and Nick.
>>
>> I have defined -local-max1 and here is a more detailed code which shows
> temporary files as well as draws on other previously saved datasets. I
have

> ended the do file abruptly to try and isolate the syntax problem.
>>
>> The codes work up to the -forvalues- codes which is the last three lines.
>>
>> Note that I changed the syntax for the temporary files from ` to ' i.e.
> `allsvys' to 'allsyvs' which did not run in Stata 11.
>>
>> Thanks. Gauri
>>
>> ****************************
>> use "reshape.dta", clear
>> gen totalind=0
>>
>> append using "D:\UnitData\0 NCDInfoBase\1. Analysis Team Only -
> Harmonization\d. Tobacco Use\Datasets - Results\7 total age grp\total age
> raw.dta"
>> replace totalind=1 if totalind==.
>> replace total_age_group=0 if totalind==1
>>
>> gen sex=0 if sex_code==1
>> replace sex=1 if sex_code==2
>>
>> gen agegrp=1 if midage<17
>> replace agegrp=0 if start_age==16 & end_age==16
>> replace agegrp=0 if agegrp==.
>>
>> *drop untrustworthy WHS, they are BIH, Kazah & Slovakia
>> *drop if source_code==101692 | source_code==101731 (commented out, reg
> models already exclude those surveys)
>>
>> *tab un_region if lgsmkd !=. & lgsmkc !=. & sex!=. & total_age_group!=1
>> *tab un_region
>>
>> gen lgsmkd=.
>> gen lgsmkc=.
>> gen lgcigd=.
>> gen lgcigc=.
>> gen agesmkd=.
>> gen agesmkc=.
>> gen agecigd=.
>> gen agecigc=.
>>
>> gen smk_d_est=.
>> gen smk_c_est=.
>> gen cig_d_est=.
>> gen cig_c_est=.
>>
>> gen str60 trace21=""
>>
>> gen r2d=.
>> gen r2c=.
>>
>> gen un_region2=un_region
>> replace un_region2="Oceania" if un_region =="Melanesia" |
> un_region=="Polynesia" | un_region=="Micronesia"
>> replace un_region2="Eastern Europe" if un_name=="Armenia" |
> un_name=="Azerbaijan" | un_name=="Georgia" | un_name=="Turkey" |
> un_name=="Uzbekistan" | un_name=="Turkmenistan" | un_name=="Kazakhstan" |
> un_name=="Kyrgyzstan" | un_name=="Tajikistan"
>> replace un_region2="Southern Europe" if un_name=="Cyprus" |
> un_name=="Israel"
>>
>> *save `allsvys1', replace /*OLD CODE: STATA 9*/
>> save "'allsvys1'", replace
>> drop if sex_code!=3 & total_age_group!=1 & agegrp==0
>> save "conversion1.dta", replace
>>
>> ****Regression****
>>
>> **for age 16 and more**
>>
>> set more off
>> set matsize 5000
>>
>> *use `allsvys1', clear /*OLD CODE: STATA 9*/
>> use "'allsvys1'", clear
>> egen i=group(un_region2 sex_code) if sex_code!=3 & total_age_group!=1 &
> agegrp==0
>> summarize i
>> local max1=r(max)
>> gen error1=.
>> gen error2=.
>> *save `allsvys', replace /*OLD CODE: STATA 9*/
>> save "'allsvys'", replace
>>
>> forvalues svy = 1/'max1' {
>> use "`allsvys'", clear
>> keep if i==`svy'
>> }
>>
>>
>> Thanks,
>>
>> Gauri
>>
>> ----------------------------------------
>>> From: [hidden email]
>>> To: [hidden email]
>>> Subject: st: AW: Syntax forvalues in Stata 11 SE
>>> Date: Thu, 23 Sep 2010 13:55:33 +0200
>>>
>>>
>>> <>
>>>
>>>
>>> How and when is -local- "max1" defined in your code? See this code:
>>>
>>>
>>> *************
>>> loc max1 2
>>> forv i=1/`max1'{
>>> di `i'
>>> }
>>>
>>> loc max1
>>> forv i=1/`max1'{
>>> di `i'
>>> }
>>> *************
>>>
>>>
>>>
>>> HTH
>>> Martin
>>>
>>> -----Ursprüngliche Nachricht-----
>>> Von: [hidden email]
>>> [mailto:[hidden email]] Im Auftrag von Gauri
Khanna

>>> Gesendet: Donnerstag, 23. September 2010 13:53
>>> An: Stata List
>>> Betreff: st: Syntax forvalues in Stata 11 SE
>>>
>>>
>>> Hi,
>>>
>>> I am a new user to the Stata 11.1 SE
>>>
>>> I have run into syntax problems with the - forvalues- command while
> creating
>>> a loop.
>>>
>>> My syntax which comes from Stata 9 is the following:
>>>
>>> forvalues svy = 1/`max1' {
>>> use "'allsvys'", clear
>>> keep if i==`svy'
>>> }
>>>
>>> and I get the following error :
>>>
>>> invalid syntax
>>> r(198);
>>>
>>> Subsequently, i would also like to run the following syntax (shortened
> for
>>> convenience)
>>>
>>> foreach x of local names {
>>> if "`x'"=="lgsmkc" {
>>> display "`x'"
>>> gen row`j'=lgcigc
>>> local j=`j'+1
>>> }
>>> }
>>>
>>> Can anyone help?
>>
>> *
>> * For searches and help try:
>> * http://www.stata.com/help.cgi?search
>> * http://www.stata.com/support/statalist/faq
>> * http://www.ats.ucla.edu/stat/stata/
>
> *
> * For searches and help try:
> * http://www.stata.com/help.cgi?search
> * http://www.stata.com/support/statalist/faq
> * http://www.ats.ucla.edu/stat/stata/
>
>
> *
> * For searches and help try:
> * http://www.stata.com/help.cgi?search
> * http://www.stata.com/support/statalist/faq
> * http://www.ats.ucla.edu/stat/stata/
     
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: AW: st: AW: Syntax forvalues in Stata 11 SE

nshephard
Administrator
In reply to this post by Gauri Khanna
On Thu, Sep 23, 2010 at 12:36 PM, Gauri Khanna <[hidden email]> wrote:

> Hi,
>
>
> Here is the error with - set trace on -
>
> . set trace on
>
> . forvalues svy = 1/`max1' {
>   2.         use `allsvys', clear
>   3.         keep if i==`svy'
>   4. }
> - forvalues svy = 1/`max1' {
> = forvalues svy = 1/ {
> invalid syntax
> r(198);

There you have, it despite setting the local macro max1 earlier in
your do-file, when it comes to executing the -forval- loop the local
macro is empty, so -forval- can't repeat something from 1 to
"nothing"!

However, you wrote that you have the following....

local max1=r(max)
gen error1=.
gen error2=.
*save `allsvys', replace /*OLD CODE: STATA 9*/
save "'allsvys'", replace

forvalues svy = 1/`max1' {
    use "'allsvys'", clear
    keep if i==`svy'
}


...which suggests that you are setting the local macro before the loop.

I suspect there may be some confusion from your full do-file error to
the shortened version snippet of code you have posted above where max1
is undefined.

So does...

local max1 = 2
forvalues svy = 1/`max1' {
         use `allsvys', clear
         keep if i==`svy'
}

...work?

Neil



--
"One should as a rule, respect public opinion in so far as is
necessary to avoid starvation and to keep out of prison, but anything
that goes beyond this is voluntary submission to an unnecessary
tyranny, and is likely to interfere with happiness in all kinds of
ways..." - Bertrand Russell

Email - [hidden email]
Website - http://slack.ser.man.ac.uk/
Photos - http://www.flickr.com/photos/slackline/

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

AW: AW: st: AW: Syntax forvalues in Stata 11 SE

Martin Weiss-5

<>


It is vital that Gauri run the do-file in one piece so that her -local-s
stick around...



HTH
Martin


-----Ursprüngliche Nachricht-----
Von: [hidden email]
[mailto:[hidden email]] Im Auftrag von Neil Shephard
Gesendet: Donnerstag, 23. September 2010 14:45
An: [hidden email]
Betreff: Re: AW: st: AW: Syntax forvalues in Stata 11 SE

On Thu, Sep 23, 2010 at 12:36 PM, Gauri Khanna <[hidden email]> wrote:

> Hi,
>
>
> Here is the error with - set trace on -
>
> . set trace on
>
> . forvalues svy = 1/`max1' {
>   2.         use `allsvys', clear
>   3.         keep if i==`svy'
>   4. }
> - forvalues svy = 1/`max1' {
> = forvalues svy = 1/ {
> invalid syntax
> r(198);

There you have, it despite setting the local macro max1 earlier in
your do-file, when it comes to executing the -forval- loop the local
macro is empty, so -forval- can't repeat something from 1 to
"nothing"!

However, you wrote that you have the following....

local max1=r(max)
gen error1=.
gen error2=.
*save `allsvys', replace /*OLD CODE: STATA 9*/
save "'allsvys'", replace

forvalues svy = 1/`max1' {
    use "'allsvys'", clear
    keep if i==`svy'
}


...which suggests that you are setting the local macro before the loop.

I suspect there may be some confusion from your full do-file error to
the shortened version snippet of code you have posted above where max1
is undefined.

So does...

local max1 = 2
forvalues svy = 1/`max1' {
         use `allsvys', clear
         keep if i==`svy'
}

...work?

Neil



--
"One should as a rule, respect public opinion in so far as is
necessary to avoid starvation and to keep out of prison, but anything
that goes beyond this is voluntary submission to an unnecessary
tyranny, and is likely to interfere with happiness in all kinds of
ways..." - Bertrand Russell

Email - [hidden email]
Website - http://slack.ser.man.ac.uk/
Photos - http://www.flickr.com/photos/slackline/

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
12
Loading...