Statalist


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

st: RE: looping over local macros with multiple items


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: looping over local macros with multiple items
Date   Wed, 1 Apr 2009 19:01:08 +0100

There is a lot of extraneous detail to wade through here, but I think
you need three nested loops, not one. 

The essence of your problem appears to be generating a Cartesian product
of three sets. You here loop over the union of the sets, which is not
the same thing. 

Nick 
[email protected] 

Eric A. Booth


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

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