Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


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

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


From   Gauri Khanna <[email protected]>
To   Stata List <[email protected]>
Subject   RE: AW: st: AW: Syntax forvalues in Stata 11 SE
Date   Thu, 23 Sep 2010 13:07:45 +0000

Hello, 

Yes it works: here is the output. What do you suggest when defining -local max1-?

. local max1=2

. forvalues svy = 1/`max1' { 
  2.         use "'allsvys'", clear
  3.         keep if i==`svy'
  4. }
(65473 observations deleted)
(65470 observations deleted)

I do create the -local- macro before the loop as I did with this shortened code with -local max1=2-. 

Here is the entire code which I suspect is going to have many problems:

**********************************************
*****conversion******

cd "D:\UnitData\0  NCDInfoBase\1.  Analysis Team Only - Harmonization\d. Tobacco Use\Datasets - Results\2 conversion"

tempfile allsvys1 allsvys finaltemp
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 STATA CODE*/
save "'allsvys'", replace

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

    summarize smk_d
    local maxsd=r(max)
    if 'maxsd'>=95 local maxsd=95 
    local minsd=r(min)
    if 'minsd'<=5 local minsd=5

    summarize smk_c
    local maxsc=r(max)
    if 'maxsc'>=95 local maxsc=95 
    local minsc=r(min)
    if 'minsc'<=5 local minsc=5 

    summarize cig_d
    local maxcd=r(max)
    if 'maxcd'>=95 local maxcd=95 
    local mincd=r(min)
    if 'mincd'<=5 local mincd=5 

    summarize cig_c
    local maxcc=r(max)
    if 'maxcc'>=95 local maxcc=95
    local mincc=r(min)
    if 'mincc'<=5 local mincc=5 

    replace lgsmkd=ln((smk_d-('minsd'-5))/(('maxsd'+5)-smk_d))
    replace lgsmkc=ln((smk_c-('minsc'-5))/(('maxsc'+5)-smk_c))
    replace lgcigd=ln((cig_d-('mincd'-5))/(('maxcd'+5)-cig_d))
    replace lgcigc=ln((cig_c-('mincc'-5))/(('maxcc'+5)-cig_c))

    replace agesmkd=midage*lgsmkd
    replace agesmkc=midage*lgsmkc
    replace agecigd=midage*lgcigd
    replace agecigc=midage*lgcigc

    list un_region2 in 1
    list sex_code in 1

    sw regress lgsmkd lgsmkc midage agesmkc if sex_code!=3 & total_age_group!=1 & totalind!=1 & agegrp==0 & source_code!=101692 & source_code!=101731 & source_code!=101712 & source_code!=101697 & source_code!=101858 & source_code!=101893 & source_code!=101727, pr(0.05) lockterm1 hier
    mark sdsc if sex_code!=3 & total_age_group!=1 & totalind!=1 & agegrp==0 & lgsmkd!=. & lgsmkc!=. & source_code!=101692 & source_code!=101731 & source_code!=101712 & source_code!=101697 & source_code!=101858 & source_code!=101893 & source_code!=101727
    predict yhat1
    replace r2d=e(r2_a)
    replace smk_d_est = (('minsd'-5)+('maxsd'+5)*exp(yhat1))/(1+exp(yhat1))

    replace trace21="defconv smk c->d ok"   if smk_c != . & smk_c != 0 & smk_d == . & smk_d_est != . & smk_d_est <= smk_c & agegrp==0
    replace smk_d=smk_d_est                 if smk_c != . & smk_c != 0 & smk_d == . & smk_d_est != . & smk_d_est <= smk_c & agegrp==0
    
    replace trace21="defconv smk c->d smkc" if smk_c != . & smk_c != 0 & smk_d == . & smk_d_est != . & smk_d_est> smk_c  & agegrp==0
    replace smk_d=smk_c                     if smk_c != . & smk_c != 0 & smk_d == . & smk_d_est != . & smk_d_est> smk_c  & agegrp==0
    
    replace trace21="defconv smk c->d cigd" if smk_c != . & smk_c != 0 & smk_d_est != . & cig_d!=. & smk_d_est < cig_d  & agegrp==0
    replace smk_d=cig_d                     if smk_c != . & smk_c != 0 & smk_d_est != . & cig_d!=. & smk_d_est < cig_d  & agegrp==0

  ****now apply the same relationship to cigc-->cigd****

    matrix coef=e(b)
    matrix coef2=coef'
    local names:colnames coef
    local num=colsof(coef)
    local j=1

    foreach x of local names {
        if "`x'"=="lgsmkc" {        
            display "`x'"
            gen row'j'=lgcigc 
            local j='j'+1
        }
        if "`x'"=="midage" {
            display "`x'"
            gen row'j'=midage
            local j='j'+1
        }
        if "`x'"=="agesmkc" {
            display "`x'"
            gen row'j'=agecigc
            local j='j'+1
        }
        if "`x'"=="_cons" {
            display "`x'"
            gen row'j'=1
            continue, break
        }
    }

    replace error1=0 if 'j'=='num'

    mkmat row1-row'j', matrix(factor)
    drop row1-row'j'
    matrix yhat2=factor*coef2
    svmat yhat2, names(matcol) 
    rename yhat2y1 yhat2
    replace cig_d_est = (('mincd'-5)+('maxcd'+5)*exp(yhat2))/(1+exp(yhat2))

    replace trace21=plural(2, trace21, "+; cig c->d ok") if trace21!="" & cig_c != . & cig_c != 0 & smk_d==. & cig_d == . & cig_d_est != . & cig_d_est <= cig_c & agegrp==0
    replace trace21="defconv cig c->d ok"                if trace21=="" & cig_c != . & cig_c != 0 & smk_d==. & cig_d == . & cig_d_est != . & cig_d_est <= cig_c & agegrp==0
    replace cig_d=cig_d_est                              if cig_c != . & cig_c != 0 & smk_d==. & cig_d == . & cig_d_est != . & cig_d_est <= cig_c & agegrp==0
    
    replace trace21=plural(2, trace21, "+; cig c->d ok") if trace21!="" & cig_c != . & cig_c != 0 & smk_d!=. & smk_d!=0 & cig_d == . & cig_d_est != . & cig_d_est <= cig_c & cig_d_est <= smk_d & agegrp==0
    replace trace21="defconv cig c->d ok"                if trace21=="" & cig_c != . & cig_c != 0 & smk_d!=. & smk_d!=0 & cig_d == . & cig_d_est != . & cig_d_est <= cig_c & cig_d_est <= smk_d & agegrp==0
    replace cig_d=cig_d_est                              if cig_c != . & cig_c != 0 & smk_d!=. & smk_d!=0 & cig_d == . & cig_d_est != . & cig_d_est <= cig_c & cig_d_est <= smk_d & agegrp==0
    
    replace trace21=plural(2, trace21, "+; cig c->d cigc") if trace21!="" & cig_c != . & cig_c != 0 & smk_d==. & cig_d == . & cig_d_est != . & cig_d_est> cig_c & agegrp==0
    replace trace21="defconv cig c->d cigc"                if trace21=="" & cig_c != . & cig_c != 0 & smk_d==. & cig_d == . & cig_d_est != . & cig_d_est> cig_c & agegrp==0
    replace cig_d=cig_c                                    if cig_c != . & cig_c != 0 & smk_d==. & cig_d == . & cig_d_est != . & cig_d_est> cig_c & agegrp==0
    
    replace trace21=plural(2, trace21, "+; cig c->d cigc") if trace21!="" & cig_c != . & cig_c != 0 & smk_d!=.  & smk_d!=0 & cig_d == . & cig_d_est != . & cig_d_est> cig_c & cig_c<=smk_d & agegrp==0
    replace trace21="defconv cig c->d cigc"                if trace21=="" & cig_c != . & cig_c != 0 & smk_d!=.  & smk_d!=0 & cig_d == . & cig_d_est != . & cig_d_est> cig_c & cig_c<=smk_d & agegrp==0
    replace cig_d=cig_c                                    if cig_c != . & cig_c != 0 & smk_d!=.  & smk_d!=0 & cig_d == . & cig_d_est != . & cig_d_est> cig_c & cig_c<=smk_d & agegrp==0
    
    replace trace21=plural(2, trace21, "+; cig c->d smkd") if trace21!="" & cig_c != . & cig_c != 0 & smk_d!=. & smk_d!=0 & cig_d==. & cig_d_est != . & cig_d_est> cig_c & cig_c>smk_d & agegrp==0
    replace trace21="defcov cig c->d smkd"                 if trace21=="" & cig_c != . & cig_c != 0 & smk_d!=. & smk_d!=0 & cig_d==. & cig_d_est != . & cig_d_est> cig_c & cig_c>smk_d & agegrp==0
    replace cig_d=smk_d                                    if cig_c != . & cig_c != 0 & smk_d!=. & smk_d!=0 & cig_d==. & cig_d_est != . & cig_d_est> cig_c & cig_c>smk_d & agegrp==0
    
    matrix drop coef coef2 factor yhat2

************************************************************************************************************************************
************************************************************************************************************************************

    sw regress lgsmkc lgsmkd midage agesmkd if sdsc==1, pr(0.05) lockterm1 hier
    predict yhat3
    replace r2c=e(r2_a)
    replace smk_c_est = (('minsc'-5)+('maxsc'+5)*exp(yhat3))/(1+exp(yhat3))
    replace smk_c=smk_c_est  if smk_d != . & smk_d != 0 & smk_c == . & smk_c_est != . & cig_c==. & smk_c_est>= smk_d & agegrp==0
    replace smk_c=smk_c_est  if smk_d != . & smk_d != 0 & smk_c == . & smk_c_est != . & cig_c!=. & cig_c!=0 & smk_c_est>= smk_d & smk_d>=cig_c & agegrp==0
    replace smk_c=smk_d      if smk_d != . & smk_d != 0 & smk_c == . & smk_c_est != . & cig_c==. & smk_c_est < smk_d & agegrp==0
    replace smk_c=smk_d      if smk_d != . & smk_d != 0 & smk_c == . & smk_c_est != . & cig_c!=. & cig_c!=0 & smk_c_est < smk_d & smk_d>=cig_c & agegrp==0
    replace smk_c=cig_c      if smk_d != . & smk_d != 0 & smk_c == . & smk_c_est != . & cig_c!=. & cig_c!=0 & smk_c_est < smk_d & smk_d<cig_c & agegrp==0

    ****now apply the same relationship to cigd-->cigc****

    matrix coef=e(b)
    matrix coef2=coef'
    local names:colnames coef
    local num=colsof(coef)
    local j=1

    foreach x of local names {
        if "`x'"=="lgsmkd" {        
            display "`x'"
            gen row'j'=lgcigd 
            local j='j'+1
        }
        if "`x'"=="midage" {
            display "`x'"
            gen row'j'=midage
            local j='j'+1
        }
        if "`x'"=="agesmkd" {
            display "`x'"
            gen row'j'=agecigd
            local j='j'+1
        }
        if "`x'"=="_cons" {
            display "`x'"
            gen row'j'=1
            continue, break
        }
    }

    replace error2=0 if 'j'=='num'

    mkmat row1-row'j', matrix(factor)
    drop row1-row'j'
    matrix yhat4=factor*coef2
    svmat yhat4, names(matcol) 
    rename yhat4y1 yhat4
    replace cig_c_est = (('mincc'-5)+('maxcc'+5)*exp(yhat4))/(1+exp(yhat4))
    replace cig_c=cig_c_est  if cig_d != . & cig_d != 0 & cig_c == . & cig_c_est != . & smk_c==. & cig_c_est>= cig_d & agegrp==0
    replace cig_c=cig_c_est  if cig_d != . & cig_d != 0 & cig_c == . & cig_c_est != . & smk_c!=. & smk_c!=0 & cig_c_est <= smk_c & cig_c_est>=cig_d & agegrp==0
    replace cig_c=cig_d      if cig_d != . & cig_d != 0 & cig_c == . & cig_c_est != . & cig_c_est < cig_d & agegrp==0
    replace cig_c=smk_c      if cig_d != . & cig_d != 0 & cig_c == . & cig_c_est != . & smk_c!=. & smk_c!=0 & cig_c_est> smk_c & agegrp==0
    matrix drop coef coef2 factor yhat4

    cap append using  "'finaltemp'"
    save "'finaltemp'", replace
}



Many thanks, 

Gauri 
----------------------------------------
> From: [email protected]
> Date: Thu, 23 Sep 2010 12:44:47 +0000
> Subject: Re: AW: st: AW: Syntax forvalues in Stata 11 SE
> To: [email protected]
>
> On Thu, Sep 23, 2010 at 12:36 PM, Gauri Khanna  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 - [email protected]
> 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/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index