Announcement

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

  • How to export dataset to excel with a filename based on a temporary variable name in a program

    Hi Statalisters,

    I'm a beginner to Stata and had spent ages trying to figure out a way of generating a filename based on a temporary variable name in a program. For example, I'd like my export filename to be "estimation_1.xlsx" if `x' is an argument which has a variable name "estimation" in a program. Below is a toy sample.

    /*
    input estimation
    1
    2
    3
    4
    5
    end

    program toy
    args x
    export excel `x' using "estimation_1.xlsx"
    end

    toy estimation

    */

    I've tried some coding like below, but it didn't work.

    /*
    program toy
    args x
    tempvar x_name temp_filename filename
    gen `x_name' = "`x'"
    gen `temp_filename' = "_1.xlsx"
    gen `filename' = `x_name' + `temp_filename'
    export excel `x' using `filename'
    end

    */

    Since I have to export a bunch of files which depend on the argument x, I guess trying to automate the generation of filename would be very helpful. Thanks in advance for your help! Any advice would be highly appreciated!




  • #2
    How about:
    Code:
    program toy
    args x
    
    export excel `x' using estimation_`x'_1
    end
    That way the filename will contain the name of the variable provided as the argument of program toy.

    Comment


    • #3
      It works! Thanks very much!!!

      Comment

      Working...
      X