Announcement

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

  • How to call date set in global to file name while exporting?

    So here I am trying to make a global date instead of local (which works for each separate file.) since I am using the same syntax in multiple files. The local syntax for this works perfectly fine. however, when I using the same syntax to make it a global then its not capturing the date while exporting.


    ** FOR GLOBAL
    global today = date("`c(current_date)'", "DMY")-1
    display `today'
    global today_str: disp %tdm_D `today'
    global today_dashes: disp %tdYY-Mon-DD `today'
    global today_dashes = "20`today_dashes'"


    ** FOR LOCAL

    local lastday = date("`c(current_date)'", "DMY")-1
    local lastday_str: disp %tdm_D `lastday'
    local lastday_dashes: disp %tdYY-Mon-DD `lastday'
    local lastday_dashes = "20`lastday_dashes'"

    * Use the global macro in the export command
    export delimited "$audioscrutiny/audio_scrutiny_$today_dashes", replace

    *Use the global macro in the export command
    export delimited "$audioscrutiny/audio_scrutiny_`lastday_dashes', replace

    Below we can see that the files generated using local has dates but the global one has a missing date info. How can I fix this issue of global date?
    Click image for larger version

Name:	DSADA.png
Views:	1
Size:	9.5 KB
ID:	1738151

    Last edited by Raj Sarm; 26 Dec 2023, 00:32.

  • #2
    The global audioscrutiny is not defined in any code you show us.

    The global today is defined but you don't use it in defining any later globals, as references (e.g.) to `today' would be to local today, not global today.

    Try this

    Code:
    global today = date("`c(current_date)'", "DMY")-1
    display "$today"
    global today_str: disp %tdm_D $today 
    global today_dashes: disp %tdYY-Mon-DD $today 
    global today_dashes  "20$today"

    Comment


    • #3
      Thank you, Nick for the suggestion. I tried using this edits suggested by you and I can see the it worked however, the date formatting is not reflecting as needed. Trying to get this format 2023-Dec-27 so all the folders and files to be saved in this name format. I am attaching the highlights of the result and apologies as I had to cover few details.
      Click image for larger version

Name:	jhbb.png
Views:	1
Size:	91.9 KB
ID:	1738252


      Comment


      • #4
        You need to spell out that you want the century too.

        Code:
        . di %tdCCYY-Mon-DD mdy(12, 27, 2023)
        2023-Dec-27

        Comment


        • #5
          Click image for larger version

Name:	dsf.png
Views:	1
Size:	25.7 KB
ID:	1738304
          TThe format hasn't changed for the same.

          Comment


          • #6
            Your code inserts 20 twice. The century prefix comes once from the display format and once again from the following command.

            Code:
            . di %tdCCYY-Mon-DD mdy(12, 27, 2023)
            2023-Dec-27
            
            . global whatever : di %tdCCYY-Mon-DD mdy(12, 28, 2023)
            
            . global whatever "20$whatever"
            
            . mac li
            whatever:       202023-Dec-28

            Comment


            • #7
              So do I have to keep editing the date (12,28,2023) manually for each other day as I need to run this code on daily basis and I am trying to code in a way where the code captures the computer date instead of manually editing this everyday. Just like in the first post I had mentioned using locals instead of globals and It worked perfectly. However, I woul dbe using this syntax in multiple do. files therefore trying to centralize it by making global.

              Comment


              • #8
                The need for globals here seems illusory. As I understand it, you want to take the current date and save a file each day. All that you need to do is make the rest of the file name visible to code that maps the current date to a file name. Perhaps I am not understanding what you want, but I don't understand what you're finding difficult.

                Comment

                Working...
                X