Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • using global macro names in folder names and complex files names

    Hello,

    How can I use global macro variable names in folder and file names ? It works when all the macro names are at the end of the expression, but when it is inside, I get error messages.

    for instance

    Code:
    ***Without macros (to show what I would like to do)
     use "X:\HLE\SILC\Prep SILC data\FR\Further_steps\2006\Prep_data_06.dta",clear
                /*here 2 command lines without problems */               
                *stset DOE, fail(event==1) id(ID_$yrsilc) origin(DOB) enter(date_surv) scale(365.25)
                 *stsplit curage, at(16(1)85)                 
    save "X:\HLE\SILC\Prep SILC data\FR\Further_steps\2006\STsplit_Noweight_06_15_95.dta", replace
    
    ********WITH macros, I have tried this:
    
    global yrsilc "06"
    global agemin "16"
    global agemax "85"
    
    use "X:\HLE\SILC\Prep SILC data\FR\Further_steps\20$yrsilc\Prep_data_$yrsilc.dta",clear
     ****command lines
    save "X:\HLE\SILC\Prep SILC data\FR\Further_steps\20$yrsilc\STsplit_Noweight_$yrsilc_$agemin_$agemax.dta", replace
    HTML Code:
    file X:\HLE\SILC\Prep SILC data\FR\Further_steps\2006\STsplit_Noweight_95.dta saved
    The global macro names in the middle of the filename are not working($yrsilc and $agemin); the last one is working ($agemax).
    I have also tried to put . , {} , but it did not work neither. I suppose there is a sign for terminating the macroname ? like in SAS the .

    Thank you in advance, Françoise

  • #2
    Underscores can be part of macro names. Therefore, you need to disambiguate.

    Code:
    . global foo 42
    
    . di "$foo_"
    
    
    . di "${foo}_"
    42_
    You say you used braces { } but you don't show us how.

    Also, don't mix backslashes with macro references. This is documented:

    http://www.stata.com/manuals14/u18.pdf 18.3.11

    http://www.stata-journal.com/sjpdf.h...iclenum=pr0042

    (Searching this forum alone for mentions of pr0042 yielded 20 mentions since 31 March 2014; now 21.)

    (A different comment is that the gain from global macros really isn't obvious in your example.)

    Comment


    • #3
      Thanks Nick : It works now perfectly and you saved me a lot of time !

      Why this macro ?
      indeed, I want a) to repeat the calculations for years 2006 to 2016
      b) repeat the calculations for lower and upper age limits
      c) use the age limits for selections, in the calculations commands
      d) name my results (output file and titles) with the choices made with the macros values

      have a nice day,
      Françoise

      Comment


      • #4
        I think you're implying that your commands are sample commands within a longer program looping over more files. So, the macros are themselves changed in that loop.

        Fine if so. But all we can see in your code is of the form


        B is A
        ...
        reference to B
        when a reference to A would suffice.

        I would always, always (*) use locals, not globals, wherever I thought macros were needed at all, but that's partly a strong preference on programming style.

        (*) There are very, very occasional exceptions.

        Comment


        • #5
          Nick Cox Thanks for Comment #2. Relatively new to Stata and couldn't figure out why my filenames weren't coming out right when declaring with a global. The pointer to that reference was very helpful (and valid even 10 years later)!

          Comment

          Working...
          X