简体   繁体   English

如何在Stata中将`if`与本地宏一起使用?

[英]How to use `if` with local macro in Stata?

I am trying to assign values of income in 2012 to locals. 我正在尝试将2012年的income价值分配给当地人。 So that local val_AK would have value of income in state "AK", val_AL would have value of income in state "AL".... Here is the Stata 11 version of .dta 使局部val_AK将有一些有价值的income状态“AK”, val_AL将有一些有价值的income状态“AL” ......这里是.dta的Stata的11版

The following code generates AK not found r(111); 以下代码生成AK not found r(111); However AK does exist in state_nsw , which is a string variable: 但是AK确实存在于state_nsw ,它是一个字符串变量:

foreach i in "AK" "AL" "AR" "AZ" {
     if (state_nsw=="`i'" & year==2012) { local val_`i'=income}
     }

I also tried using this method and this: 我也尝试过使用这种方法

if (state_nsw=="`i'" ...
...
// and got invalid name r(198)

Where is my mistake? 我的错误在哪里?

As state_nsw is a string variable it can only be compared with either a literal string or another string variable. 由于state_nsw是一个字符串变量,因此只能与文字字符串或另一个字符串变量进行比较。

Simplifying down to the first value in your loop 简化到循环中的第一个值

foreach i in "AK" {
    if (state_nsw == `i' & year == 2012) { local val_`i'=income }
}

the inner statement is interpreted as 内部语句被解释为

    if (state_nsw == AK & year == 2012) { local val_AK = income } 

Now as far as Stata is concerned AK is not a literal string, so it can only be the name of a string variable. 现在就Stata而言, AK不是文字字符串,因此它只能是字符串变量的名称。 But you have no such variable, which is why you got the error you did. 但是您没有这样的变量,这就是为什么出现错误的原因。 What would have been more appropriate would have been 更合适的是

    if (state_nsw == "AK" & year == 2012) { local val_AK = income } 

or, more generally, 或更一般而言

   if (state_nsw == "`i'" & year == 2012) { local val_`i'=income }

So, why did you get the error invalid name r(198): 那么,为什么会得到错误的invalid name r(198):

 foreach i in "AK" "AL" "AR" "AZ" {
         if (state_nsw=="`i'" & year==2012) { local val_`i'=income}
         }

I can't answer that from looking at your code, but see below. 我无法通过查看您的代码来回答问题,但请参见下文。

Your troubles are not over yet. 您的麻烦还没有结束。 Your code is very confused. 您的代码非常混乱。

will always be interpreted as 将始终被解释为

    local val_`i' = income[1] 

regardless of the if command. 无论if命令如何。

  • Placing { } on the same line after if has been out-of-date since version 8 and may not work as you wish. if { }自版本8起已过时, if { }放在同一行上,可能无法按您希望的那样工作。

What should work is 应该起作用的是

  foreach i in AK AL AR AZ { 
      su income if year == 2012 & state_nsw == "`i'", meanonly 
      local val_`i' = r(mean) 
  }

although why you want to do that is a puzzle. 尽管为什么要这样做是一个难题。

I can't still open the data. 我仍然无法打开数据。 Try following: 请尝试以下操作:

foreach i in "AK" "AL" "AR" "AZ"{
    gen val_`i'=income if state_nsw=="`i'" & year==2012
    local val `val' val_`i'
    }

Sample data and output: 
state   val year
AK  13  2010
AL  112 2010
AR  12  2010
AZ  14  2010


    val_AK  val_AL  val_AR  val_AZ  
    13              
            112         
                    12      
                             14 

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

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