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

st: RE: looping over local macros with multiple items

From   "Nick Cox" <>
To   <>
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. 


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:
....and so on....

Here is my code so far:

set mem 2g
set matsize 800

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

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

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

*   For searches and help try:

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