Statalist


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

st: Re: Re: looping over local macros with multiple items


From   "Martin Weiss" <[email protected]>
To   <[email protected]>
Subject   st: Re: Re: looping over local macros with multiple items
Date   Wed, 1 Apr 2009 20:05:11 +0200

<>

Instead of the -tuples- thing, you could probably nest the -foreach- loops, that would be easier...


HTH
Martin
_______________________
----- Original Message ----- From: "Martin Weiss" <[email protected]>
To: <[email protected]>
Sent: Wednesday, April 01, 2009 7:54 PM
Subject: st: Re: looping over local macros with multiple items


<>
The loop

***foreach n in `reporttype' `populationtype' `sessiontype'  {
    outsheet using "`sf'test/
`reporttype'_`populationtype'_`sessiontype'.csv", replace comma
}
***

has "`n'" as the macro changing while the loop is running. "`reporttype' `populationtype' `sessiontype'" do not change, so every time Stata runs through the loop you construct the same file - which you do not notice as -replace- has been specified.

Try -ssc d tuples-


HTH
Martin
_______________________
----- Original Message ----- From: "Eric A. Booth" <[email protected]>
To: <[email protected]>
Sent: Wednesday, April 01, 2009 7:39 PM
Subject: st: looping over local macros with multiple items


Hello:

I am attempting to create (via -outsheet-) a set of .csv files with filenames generated from a combination of "category names" stored in local macros. I am trying to use looping to create filenames from all the possible combinations of the "category names" but I'm not sure how to get Stata to move past the first item in each of the "category names" local macros. (I am using Stata 10 , 64-bit for Mac)

When I run the code below, I get the folder "test" with only one .csv named with all the items in each of the "category names" macros. So, I get the file:

registration none_cshsn othermch_ap ac ag an.csv

but, I'd like for this to create all the combinations of file names from the items in the "category names" macros:
registration_cshsn_ac.csv
registration_cshsn_ag.csv
registration_cshsn_an.csv
registration_othermch_ap.csv
registration_othermch_ac.csv
registration_othermch_ag.csv
registration_othermch_an.csv
....and so on....

Here is my code so far:

*********************************
clear
set mem 2g
set matsize 800

//location of files:
local sf "/users/p/desktop/part/" //<--change this filepath to your local directory
cd "`sf'"
pwd

//1.  empty dataset with 100 obs
set obs 100
gen temp = [_n]

//2.  category names
local reporttype "registration none"
local populationtype "cshsn othermch"
local sessiontype "ap ac ag an"
**NOTE:  I've tried this part with and without the quotes, ///
 as well as with quotes on each individual item**

//3.  generate some variables that will be filled in later
local cols name phone address city county zip email
foreach c in `cols' {
    gen str40 `c' = ""
}

**generates a blank dataset with variables from `cols'

save "`sf'master.dta", replace //will be used later for joining (- joinby-) some data

capture  !mkdir "`sf'test/"
cd "`sf'test/"
pwd

foreach n in `reporttype' `populationtype' `sessiontype'  {
outsheet using "`sf'test/ `reporttype'_`populationtype'_`sessiontype'.csv", replace comma
}

//QC check (lists the names of the files in the directory:

local from=strlower("`c(pwd)'")
local list: dir "`from'" files "registration*"

foreach l in `list' {
    di "`l'" as res
}

/*
QC check currently displays:
registration none_cshsn othermch_ap ac ag an.csv
r; t=0.00 12:34:08
*/
//end
*********************************


Thanks,

Eric

__
Eric A. Booth
Public Policy Research Institute
Texas A&M University
[email protected]
Office: +979.845.6754
Fax: +979.845.0249

*
*   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/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index