Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down at the end of May, and its replacement, statalist.org is already up and running.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


From   Nick Cox <n.j.cox@durham.ac.uk>
To   "'statalist@hsphsun2.harvard.edu'" <statalist@hsphsun2.harvard.edu>
Subject   RE: st: AW: Syntax forvalues in Stata 11 SE
Date   Thu, 23 Sep 2010 13:18:36 +0100

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 
n.j.cox@durham.ac.uk 

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: martin.weiss1@gmx.de
> To: statalist@hsphsun2.harvard.edu
> 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: owner-statalist@hsphsun2.harvard.edu
> [mailto:owner-statalist@hsphsun2.harvard.edu] 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/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index