Announcement

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

  • using loop for saving and directory

    Hello, I try to use the loop for my analysis and save them and draw charts.
    so I receive an error several times. (If I put `v' in the directory part )

    Code:
    clear
    foreach v in "lntot_birth" "ltotalfertility" {
    foreach vart in "0" "1" "2" "3" "4" "5" {
    cd "C:\Users\KHATEREH\Desktop\MyProject\`v'"
    use "C:\Users\KHATEREH\Desktop\dataset_fertility_paper (2).dta"
    statsby _b _se e(r2_a), clear: reghdfe f(`vart').`v' l(0/2)dum_recession l(1/2)`v' , absorb( i.ifscode i.year) vce(cluster ifscode)
    
    foreach var in dum_recession {
        gen t_`var' = _b_`var'/_se_`var'
        gen time = "obs`vart'"
    save `v'`vart'.dta, replace
        }
        }
        }
    unable to change to C:\Users\KHATEREH\Desktop\MyProject`v'
    r(170);
    if not I receive 12 dta files but them again for drawing and merging them I receive an error.


    Code:
    foreach v in "lntot_birth" "ltotalfertility" {
        
    use "C:\Users\KHATEREH\Desktop\MyProject\0.dta\`v'"
    foreach var in  "1" "2" "3" "4" "5" {
    append using "C:\Users\KHATEREH\Desktop\MyProject\`var'.dta "
    *drop _merge
    }
    gen date_string = substr(time, 4, 2)
    destring date_string, gen(year)
    
    keep  year  _b_dum_recession _se_dum_recession t_dum_recession
    order  year  _b_dum_recession _se_dum_recession t_dum_recession
    gen label="all"
    bysort label (year) : gen first = _n == _N-5
    **expand only the first obs
    expand 2 if first==1
    **** generate the new time variable
    bysort label (year) :gen time2 = _n-2
    ***recode the variables
    replace _b_dum_recession=0 if time2==-1
    replace _se_dum_recession=0 if time2==-1  
    replace t_dum_recession=0 if time2==-1  
    bysort label (year) : gen low = _b_dum_recession-(_se_dum_recession*1.645)
    bysort label (year) : gen high = _b_dum_recession+(_se_dum_recession*1.645)
    gen a=0
    *a=0 is for charts  
    save "C:\Users\KHATEREH\Desktop\MyProject\all", replace
    }
    Code:
    file C:\Users\KHATEREH\Desktop\MyProject\0.dta`v' not found
    I hope I receive your advices.

    Thank you so much.

    regards,

  • #2
    The following reveals the problem.

    Code:
    foreach v in lntot_birth ltotalfertility {
        di "C:\Users\KHATEREH\Desktop\MyProject\`v'"
    }
    
    foreach v in lntot_birth ltotalfertility {
        di "C:\Users\KHATEREH\Desktop\MyProject\\`v'"
    }
    So it seems that you have to escape the backslash.

    Code:
    . foreach v in lntot_birth ltotalfertility {
      2. 
    .     di "C:\Users\KHATEREH\Desktop\MyProject\`v'"
      3. 
    . }
    C:\Users\KHATEREH\Desktop\MyProject`v'
    C:\Users\KHATEREH\Desktop\MyProject`v'
    
    . 
    . 
    . 
    . foreach v in lntot_birth ltotalfertility {
      2. 
    .     di "C:\Users\KHATEREH\Desktop\MyProject\\`v'"
      3. 
    . }
    C:\Users\KHATEREH\Desktop\MyProject\lntot_birth
    C:\Users\KHATEREH\Desktop\MyProject\ltotalfertility

    Comment


    • #3
      To what Andrew wrote, let me add that you should review the very short section 18.3.11 of the Stata User's Guide PDF manual included in your Stata installation and accessible through Stata's Help menu. There you will learn that
      Stata understands / as a directory separator on all platforms.
      so that another solution to your problem is to change
      Code:
      use "C:\Users\KHATEREH\Desktop\MyProject\0.dta\`v'"
      to
      Code:
      use "C:\Users\KHATEREH\Desktop\MyProject\0.dta/`v'"
      or indeed to
      Code:
      use "C:/Users/KHATEREH/Desktop/MyProject/0.dta/`v'"

      Comment


      • #4
        @Andrew Musau and @William Lisowski thank you so much for your reply. This is the final report that I did and it works. Thank you.

        Code:
        clear
        foreach v in "lntot_birth" "ltotalfertility" {
        foreach vart in "0" "1" "2" "3" "4" "5" {
        cd "C:\Users\KHATEREH\Desktop\MyProject/`v'"
        use "C:\Users\KHATEREH\Desktop\dataset_fertility_paper (2).dta" 
        statsby _b _se e(r2_a), clear: reghdfe f(`vart').`v' l(0/2)dum_recession l(1/2)`v' , absorb( i.ifscode i.year) vce(cluster ifscode) 
        foreach var in dum_recession {
            gen t_`var' = _b_`var'/_se_`var'
            gen time = "obs`vart'"
        save `v'`vart'.dta, replace
            }
            } 
            }
        and then

        Code:
        clear
        foreach v in "lntot_birth" "ltotalfertility" {
        use "C:\Users\KHATEREH\Desktop\MyProject/`v'/`v'0.dta"
        foreach var in  "1" "2" "3" "4" "5" {
        append using  "C:\Users\KHATEREH\Desktop\MyProject/`v'/`v'`var'.dta"
        *drop _merge
        }
        gen date_string = substr(time, 4, 2)
        destring date_string, gen(year)
        
        keep  year  _b_dum_recession _se_dum_recession t_dum_recession 
        order  year  _b_dum_recession _se_dum_recession t_dum_recession 
        gen label="all"
        bysort label (year) : gen first = _n == _N-5
        **expand only the first obs
        expand 2 if first==1
        **** generate the new time variable
        bysort label (year) :gen time2 = _n-2
        ***recode the variables
        replace _b_dum_recession=0 if time2==-1
        replace _se_dum_recession=0 if time2==-1  
        replace t_dum_recession=0 if time2==-1  
        bysort label (year) : gen low = _b_dum_recession-(_se_dum_recession*1.645)
        bysort label (year) : gen high = _b_dum_recession+(_se_dum_recession*1.645)
        gen a=0 
        *a=0 is for charts  
        save "C:\Users\KHATEREH\Desktop\MyProject/`v'all.dta", replace
        }
        regards,

        Comment

        Working...
        X