Statalist


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

AW: st: How to use the marco names?


From   "Martin Weiss" <[email protected]>
To   <[email protected]>
Subject   AW: st: How to use the marco names?
Date   Mon, 3 Aug 2009 16:33:34 +0200

<> 


". foreach i of local r(files) {

is that r(files) is not considered a local macro."



You can use another -foreach- syntax to get the thing moving, though:


*************
fs d*.dta

foreach file in `r(files)'{
	di "`file'"
}
*************



HTH
Martin


-----Ursprüngliche Nachricht-----
Von: [email protected]
[mailto:[email protected]] Im Auftrag von Dan Blanchette
Gesendet: Montag, 3. August 2009 16:22
An: statalist
Betreff: Re: st: How to use the marco names?

The SSC command -fs-, returns the file names in the 
macro r(files) with double quotes around each filename.
So if you:

. fs *.csv
bs.csv     is.csv     price.csv  big.csv    share.csv  mag.csv    plu.csv


and use compound quotes around `r(files)':

. di `"`r(files)'"'
"bs.csvis.csv" "price.csv" "big.csv" "share.csv" "mag.csv" "plu.csv"

and use compound quotes again when setting the local macro hh:

. local hh `"`r(files)'"'

the double quotes around the filenames will be preserved.
The errors you experienced were due to the internal double quotes and
some of them being removed by Stata since you didn't use compound
double quotes.

The problem with:

. foreach i of local r(files) {

is that r(files) is not considered a local macro.

Hope this helps,

Dan Blanchette
Research Associate
Center for Entrepreneurship and Innovation
Duke University's Fuqua School of Business
[email protected]



------------------------------------------------------------
>From 	  [email protected]
To 	  statalist<[email protected]>
Subject 	  st: How to use the marco names?
Date 	  Sun, 02 Aug 2009 14:47:51 +0800

Dear statalists,

I am confused by marco names. Could anyone help me ? 
In my opinion, the following four methods should get the
same results.However, not as I expected.

. fs *.csv
bs.csv     is.csv     price.csv  big.csv    share.csv  mag.csv    plu.csv

. di `r(files)'
bs.csvis.csvprice.csvbig.csvshare.csvmag.csvplu.csv

. local hh `r(files)'

. foreach i of local r(files) {
   2. di "`i'"
   3. clear
   4. insheet using `i'
   5. }
{ required
r(100);

. foreach i in `r(files)' {
   2. di "`i'"
   3. clear
   4. insheet using `i'
   5. }
bs.csv
(6 vars, 13299 obs)
is.csv
(9 vars, 13403 obs)
price.csv
(4 vars, 2714 obs)
big.csv
(6 vars, 53870 obs)
share.csv
(9 vars, 2755 obs)
mag.csv
(8 vars, 45028 obs)
plu.csv
(7 vars, 2751 obs)

. foreach i in `hh' {
   2. di "`i'"
   3. clear
   4. insheet using `i'
   5. }
bs.csv
invalid '"' 
r(198);

. foreach i of local hh {
   2. di "`i'"
   3. clear
   4. insheet using `i'
   5. }
bs.csv
invalid '"' 
r(198);

Thank you for any help.

Sincerely,

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