用文件名中的局部和全局变量保存dta文件

时间:2019-05-31 15:33:24

标签: stata backslash stata-macros

在Stata中,我尝试使用本地和全局宏一起在文件名中保存result文件。

但是,本地宏没有正确解析。

全局宏global result "C:\Users\...\....\..." 是以下文件夹目录:

x

本地宏foreach x of varlist ret_yyy { ....... save "$result\Reg_Coefficients\`x'", replace } 是在循环中定义的:

{{1}}

这只会在“结果”文件夹下生成一个名为“ Reg_Coefficients`x'”的文件。

我希望将文件保存在文件夹“ Reg_Coefficients”下。

1 个答案:

答案 0 :(得分:2)

使用正斜杠(/)代替:

sysuse auto, clear
global result "C:/Users/.../..../..."

foreach x of varlist * { 
    display "$result/Reg_Coefficients/`x'"
}

C:/Users/.../..../.../Reg_Coefficients/make
C:/Users/.../..../.../Reg_Coefficients/price
C:/Users/.../..../.../Reg_Coefficients/mpg
C:/Users/.../..../.../Reg_Coefficients/rep78
C:/Users/.../..../.../Reg_Coefficients/headroom
C:/Users/.../..../.../Reg_Coefficients/trunk
C:/Users/.../..../.../Reg_Coefficients/weight
C:/Users/.../..../.../Reg_Coefficients/length
C:/Users/.../..../.../Reg_Coefficients/turn
C:/Users/.../..../.../Reg_Coefficients/displacement
C:/Users/.../..../.../Reg_Coefficients/gear_ratio
C:/Users/.../..../.../Reg_Coefficients/foreign