Indexing 如何获取宏中的值索引
我有一个全局宏:Indexing 如何获取宏中的值索引,indexing,global,stata,stata-macros,Indexing,Global,Stata,Stata Macros,我有一个全局宏: global global_macro sheep frog dragon 我循环这个宏,我希望能够根据全局_宏的当前索引值生成三个新变量: 所以我想让它产生变量new_var_1,new_var_2和new_var_3,因为在全局_宏中绵羊是第一个值,青蛙是第二个,龙是第三个。索引将首先包含1,然后是2,最后是3 我知道在扩展宏函数word中基本上有相反的功能。这允许基于索引访问宏中的值,而不是基于值访问索引 是否有一个函数可以执行我想要的操作?这应该可以 local n:
global global_macro sheep frog dragon
我循环这个宏,我希望能够根据全局_宏的当前索引值生成三个新变量:
所以我想让它产生变量new_var_1,new_var_2和new_var_3,因为在全局_宏中绵羊是第一个值,青蛙是第二个,龙是第三个。索引将首先包含1,然后是2,最后是3
我知道在扩展宏函数word中基本上有相反的功能。这允许基于索引访问宏中的值,而不是基于值访问索引
是否有一个函数可以执行我想要的操作?这应该可以
local n: word count $global_macro
forval index = 1/`n' {
gen new_var_`index' = 5
}
最好这样做
local n: word count $global_macro
forval index = 1/`n' {
gen new_var_`index' = 5
}
最好的我想这正是你想要的:
clear
set more off
// original list/indices
local wordlist sheep frog dragon
// attach indices to words
quietly forvalues i = 1/3 {
local creature : word `i' of `wordlist'
local `creature'_idx `i'
}
// access indices based on words
foreach i in frog dragon sheep {
display "Creature `i' has index ``i'_idx'"
}
它可能是可以重构的,但主要的一点是为每个单词创建一个保存其相应索引的局部;然后,您可以根据单词访问任何单词的索引
我可能缺少一些明显的功能/命令来执行您想要的操作。我认为这正是您想要的:
clear
set more off
// original list/indices
local wordlist sheep frog dragon
// attach indices to words
quietly forvalues i = 1/3 {
local creature : word `i' of `wordlist'
local `creature'_idx `i'
}
// access indices based on words
foreach i in frog dragon sheep {
display "Creature `i' has index ``i'_idx'"
}
它可能是可以重构的,但主要的一点是为每个单词创建一个保存其相应索引的局部;然后,您可以根据单词访问任何单词的索引
我可能缺少一些明显的功能/命令来执行您想要的操作