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]
st: Re: Re: loop: how can I download CSV files and append it
From 
 
"Joseph Coveney" <[email protected]> 
To 
 
<[email protected]> 
Subject 
 
st: Re: Re: loop: how can I download CSV files and append it 
Date 
 
Sun, 20 Mar 2011 15:45:46 +0900 
I wrote:
                                 . . . the program stores each website address
in its own local macro.  The series of local macros are named as numbers: 1, 2,
3, . . .  
--------------------------------------------------------------------------------
In Stata, local macros with numbers as their names are used to hold arguments to
do-file and other programs.  In order to avoid unintentionally overwriting an
argument to a program someday, it is probably a good practice to avoid naming 
local macros as numbers.  Instead, you can give such local macro names a short 
prefix, as shown in the revised illustration below.  Here they are named 
website1, website2, . . ., and the loop index is named more conventionally as i.
The local macros' contents are retrieved in the second loop as `website'`i', 
which is a little clearer to read, too.
Joseph Coveney
version 11.1
clear *
set more off
// Read-in your list of Web addresses
insheet using sites.txt, names clear
// Put each address into its own local macro `website1', `website2', . . .
local site_total `=_N'
forvalues i = 1/`site_total' {
    local website`i' = sites[`i']
}
// Load CSV files and append using a temporary file for storing
tempfile append
quietly forvalues i = `site_total'(-1)1 {
    insheet using "`website`i''", names comma clear
    if `i' == `site_total' {
        save "`append'"
    }
    else {
        append using "`append'"
        save "`append'", replace
    }
}
// Save the final result
outsheet using ap.csv, comma names quote
exit
*
*   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/