Announcement

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

  • Local macros & "use" command

    Hi

    I would like to use local date macros to create date-stamped file names for my log files and datasets. Creating them seems to have been straightforward so far, using the code below:

    Code:
    **create local date macro for naming files
    
    di "$S_DATE"
    local date = daily("$S_DATE", "DMY")
    di "`date'"
    di %tdCY-N-D 20418
    local date : di  %tdCY-N-D  daily("$S_DATE", "DMY")
    di "`date'"
    
    save studyinfo_`date'.dta, replace
    However, when I then want to retrieve today's .dta file later, using the code below, I get the error message "file studyinfo_.dta not found" - so the macro doesn't seem to be being recognised? This is important as I am switching between multiple .dta files in each do file.

    Code:
    use studyinfo_`date'.dta, clear
    Is it possible to combine the use command and a local macro, as I am trying to do?

    (I have a feeling there is a simple answer to this but have struggled to find one online, partly I think because the question involves the ubiquitous term "use"...)

    Thanks very much

    Emily

  • #2
    If you want to retrieve your .dta file in future days, macros cannot help you since they will disappear at the end of the session (even global macros too), i.e., your macros will no longer exist once you quit Stata and start it again.

    The problem you asked occurs because local macros are temporary names. According to Stata’s Manual (can be accessed via PDF link in the help-file),
    Global macros, once defined, are available anywhere in Stata. Local macros exist solely within the program or do-file in which they are defined. If that program or do-file calls another program or do-file, the local macros previously defined temporarily cease to exist, and their existence is reestablished when the calling program regains control. When a program or do-file ends, its local macros are permanently deleted.
    So you can combine local macros and the use command within the day you created your .dta file by 1) run a do-file which contains both the code of defining local macro and the use command, or 2) define your local macro in the command window (then this will last until you quit Stata) and then type the use command. But in any of these cases, the value of the local macro will be different if you define the local macro another day and this is why I said macros cannot help you.

    Comment

    Working...
    X