简体   繁体   English

然后使用egen命令循环遍历变量的级别

[英]Looping over levels of a variable then using egen command

I want to create a new variable that counts all the occurrences of a specified drug code over 27 columns. 我想创建一个新变量,用于计算27列中指定药物代码的所有出现次数。 However, I want to do this for all drug codes that I have available. 但是,我想对所有可用的药品代码执行此操作。 I am using a loop and the egen function anycount() . 我正在使用循环和egen函数anycount()

However, when I have tried to run it I get the error: 但是,当我尝试运行它时,出现错误:

invalid name 无效的名字
r(198); R(198);

Below you can find an example of my data: 您可以在下面找到我的数据示例:

clear
input DrugList Drug1 Drug2 Drug3 Drug4 Drug5
1234 7934 1234 . . .
5678 1234 5678 . . .
9876 9876 3456 . . .
3456 9876 . . . .
7934 9876 5678 7934 1234 .
17453 5678 . . . .
end

The code I am using is the following: 我使用的代码如下:

levelsof DrugList

foreach drug in `r(levels)'{
    egen d_`drug' = anycount(Drug1-Drug27), values(`drug')
}

The output I expect is this: 我期望的输出是这样的:

  +------------------------------------------------------+
  | d_1234   d_3456   d_5678   d_7934   d_9876   d_17453 |
  |------------------------------------------------------|
  |      1        0        0        1        0         0 |
  |      1        0        1        0        0         0 |
  |      0        1        0        0        1         0 |
  |      0        0        0        0        1         0 |
  |      1        0        1        1        1         0 |
  |      0        0        1        0        0         0 |
  +------------------------------------------------------+

If I run the code without a loop for a specific code then everything works: 如果我运行的代码没有针对特定代码的循环,则一切正常:

egen d_1234 = anycount(Drug1-Drug27), values(1234)

What am I doing wrong? 我究竟做错了什么?

The problem is that in your original data, you have drug codes in DrugList that contain decimals, negative values or even both. 问题在于,在原始数据中,您在DrugList中有药物代码,其中包含小数,负值甚至是两者。 Stata does not consider these legal for variable names. Stata认为这些变量名称不合法。

Consider the following toy example based on a slightly modified version of the data in your question: 考虑以下基于玩具中问题数据的修改版本的玩具示例:

clear
input DrugList Drug1 Drug2 Drug3 Drug4 Drug5
1234 7934 1234 . . .
5678 1234 5678 . . .
9876 9876 3456 . . .
3456.46 9876 . . . .
7934 9876 5678 7934 1234 .
17453 5678 . . . .
end

levelsof DrugList, clean

foreach drug in `r(levels)' {
    egen d_`drug' = anycount(Drug1-Drug5), values(`drug')
}
d_3456.4599609375 invalid name
r(198);

A solution that does not require removing the offending observations is to use the floor() function: 一个不需要删除有问题的观察结果的解决方案是使用floor()函数:

clear
input DrugList Drug1 Drug2 Drug3 Drug4 Drug5
1234 7934 1234 . . .
5678 1234 5678 . . .
9876 9876 3456 . . .
3456.46 9876 . . . .
7934 9876 5678 7934 1234 .
17453 5678 . . . .
end

levelsof DrugList, clean

foreach drug in `r(levels)' {
    local drug = floor(`drug')
    egen d_`drug' = anycount(Drug1-Drug5), values(`drug')
}

If you have negative values in DrugList you can use the abs() function instead: 如果DrugList值为负, DrugList可以改用abs()函数:

clear
input DrugList Drug1 Drug2 Drug3 Drug4 Drug5
1234 7934 1234 . . .
5678 1234 5678 . . .
9876 9876 3456 . . .
-3456 9876 . . . .
7934 9876 5678 7934 1234 .
17453 5678 . . . .
end

levelsof DrugList, clean

foreach drug in `r(levels)' {
    local drug = abs(`drug')
    egen d_`drug' = anycount(Drug1-Drug5), values(`drug')
}

Of course, you can also combine the aforementioned functions: 当然,您也可以结合上述功能:

clear
input DrugList Drug1 Drug2 Drug3 Drug4 Drug5
1234 7934 1234 . . .
5678 1234 5678 . . .
9876 9876 3456 . . .
-3456.46 9876 . . . .
7934 9876 5678 7934 1234 .
17453 5678 . . . .
end

levelsof DrugList, clean

foreach drug in `r(levels)' {
    local drug = abs(floor(`drug'))
    egen d_`drug' = anycount(Drug1-Drug5), values(`drug')
}

The results in all cases are those expected: 在所有情况下的结果都是预期的:

list, separator(0)

     +------------------------------------------------------+
     | d_3456   d_1234   d_5678   d_7934   d_9876   d_17453 |
     |------------------------------------------------------|
  1. |      0        1        0        1        0         0 |
  2. |      0        1        1        0        0         0 |
  3. |      1        0        0        0        1         0 |
  4. |      0        0        0        0        1         0 |
  5. |      0        1        1        1        1         0 |
  6. |      0        0        1        0        0         0 |
     +------------------------------------------------------+

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

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