Bookmark and Share

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]

Re: st: Need help using global macros


From   Nick Cox <[email protected]>
To   "[email protected]" <[email protected]>
Subject   Re: st: Need help using global macros
Date   Sun, 17 Nov 2013 14:31:54 +0000

This is nothing to do with your use of global macros. You defined

local all x y

and Stata takes you at your word. You meant

local all `x'  `y'

Here is a rewriting of your code, removing most use of macros.

foreach z in  Z\Data\file_summer_1  Z\Data2\file_winter_1  {
        use `z', clear
        <stuff>
        save "Y/Data/`z'_new.dta, replace
}

It is partly a matter of taste, but that is shorter and (I suggest)
clearer. Note that  -clear- at the beginning was redundant.

Your use of backward slashes was also a bad idea, as they have a dual
role in Stata. It is better to use forward slashes instead. See in [U]

18.3.11 Constructing Windows filenames by using macros

Stata uses the \ character to tell its parser not to expand macros.
Windows uses the \ character as the directory path separator.
Mostly, there is no problem using a \ in a filename. However, if you
are writing a program that contains a Windows path in macro path and a
filename in fname, do not assemble the final result as
`path'\`fname'
because Stata will interpret the \ as an instruction to not expand
`fname'. Instead, assemble the final result as
`path'/`fname'
Stata understands / as a directory separator on all platforms.

or alternatively

SJ-8-3  pr0042  . . . . . . .  Stata tip 65: Beware the backstabbing backslash
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
        Q3/08   SJ 8(3):446--447                                 (no commands)
        tip to use forward slash instead of backslash for
        directory and file names

which is accessible at http://www.stata-journal.com/sjpdf.html?articlenum=pr0042

Nick
[email protected]

On 17 November 2013 13:37, Michael Stewart
<[email protected]> wrote:

> I am trying to use global macro, set up an output folder to save files
> after data management .
>
> However,after data management,  I am not able to store the files as
> names similar to their original names.
>
> My code is as follows.
>
> ************Code Start**********************
> clear
> macro drop _all
> global output "Y\Data"
> local x "Z\Data\file_summer_1"
> local y "Z\Data2\file_winter_1"
> local all  x y
> foreach z in `all'{
> use ``z'',clear
> data management step
> .
> data management step
>
> save "$output\``z''_new.dta",replace
> }
> ************Code Ends*********************
>
> Problem : I am not able to store the files after data management in
> the output folder as "file_summer_1_new" and  "file_winter_1".The
> files are being stored as "x_new" and "y_new".
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index