How to use `if` with local macro in Stata?

I am trying to assign values of income in 2012 to locals. 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

The following code generates AK not found r(111); However AK does exist in state_nsw, which is a string variable:

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?

Upvotes: 1

Views: 20123

Answers (2)

Metrics
Metrics

Reputation: 15458

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 

Upvotes: 2

Nick Cox
Nick Cox

Reputation: 37368

As state_nsw is a string variable it can only be compared with either a literal string or another string variable.

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. 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):

 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.

  • Placing { } on the same line after if has been out-of-date since version 8 and may not work as you wish.

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.

Upvotes: 5

Related Questions