简体   繁体   English

在循环中包含变量本地宏

[英]Including a variable local macro inside a loop

Using a forvalues loop, I am merging a list of 400 individual datasets. 使用forvalues循环,我合并了400个单个数据集的列表。

These datasets can be one of 10 distinct values (defined by a variable in the dataset): depending on the dataset, I would merge with a different dataset. 这些数据集可以是10个不同的值(在所述数据集的变量定义)中的一个:根据所述数据集,我会merge用不同的数据集。 For example, if Player 90 was type 9, I would want to merge with Type_9.dta rather than Type_8 or Type_7 . 例如,如果Player 90是type 9,我想与Type_9.dta而不是Type_8Type_7

What I want is something like this: 我想要的是这样的:

forvalues x = 1/400 {
    use "player_`x'.dta"

    * some way to turn the value of player type into a local macro l *

    merge 1:1 using "type_`l'.dta"
}

How can i get the variable type into a macro that changes for each type through the loop? 我如何才能将变量类型放入宏,该宏通过循环针对每种类型而变化?

The structure of your data and the ultimate goal are not quite clear to me, so there may be more efficient ways to do that. 您的数据结构和最终目标对我来说还不太清楚,因此可能会有更有效的方法来做到这一点。

If player_type does not vary within each player_* data set, you can use levelsof . 如果player_type在每个player_ *数据集中均不变化,则可以使用levelsof This has the feature that if player_type varies for some reason like a data entry error, the loop will error out. 它具有以下特性:如果player_type由于某种原因(例如数据输入错误)而发生变化,则循环将出错。

forvalues x = 1/400 { 
   use "player_`x'.dta"
   levelsof player_type, local(l)
   merge 1:1 **some_id_var** using "type_`l'.dta" 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM