Using varlist multiple times in Stata

1k views Asked by At

I have three variables varA, varB and varC.
I attempted to first replace "missing" with NA in all three variables then add a label to all three variables.

First I replaced "missing" with NA:

local mylist1 varA-varC
foreach v1 of varlist `mylist1'  { 
   replace `v1'="NA" if `v1' =="missing"
}

Now if I want to call the list again to add the same label to all three variables:

foreach v1 of varlist `mylist1' {
   label var `v1' "testvaraible"
}

but I will get an error message saying :

varlist required

Could anyone explain why I can't recall the list?

2

There are 2 answers

4
Nick Cox On

For your first example, this would be legal syntax if the variables concerned were all string:

local mylist1 varA-varC
foreach v of varlist `mylist1'  { 
    replace `v' = "NA" if `v' == "missing"
}

Notice the different punctuation for referring to a local macro (different left and right quotation marks) and the difference in placing braces.

It is difficult even to work out what you want in your second example, but the loop is over differing values of a local macro v which you never refer to inside the loop. Also, depending on the definition of the unspecified local macro testvaraible [sic], it is still puzzling why you would label three variables identically.

You may need to be much more explicit about your data and exactly what you want if this does not answer the question. In particular, we can't see definitions for local macros v1 and testvaraible.

5
AudioBubble On

I came to this discussion after the edits made in response to the discussion around the previous answer. At this point, copying the code as it now stands in the original post, the problem has apparently been corrected. Hate to post this as an answer, but it's apparently too long for a comment.

. set obs 1
obs was 0, now 1

. generate str8 varA = "a"

. generate str8 varB = "missing"

. generate str8 varC = "c"

. local mylist1 varA-varC

. foreach v1 of varlist `mylist1'  { 
  2.    replace `v1'="NA" if `v1' =="missing"
  3. }
(0 real changes made)
(1 real change made)
(0 real changes made)

. foreach v1 of varlist `mylist1' {
  2.    label var `v1' "testvariable"
  3. }

. list, clean noobs

    varA   varB   varC  
       a     NA      c  

. describe

Contains data
  obs:             1                          
 vars:             3                          
 size:            24                          
------------------------------------------------------------------------------------------------
              storage   display    value
variable name   type    format     label      variable label
------------------------------------------------------------------------------------------------
varA            str8    %9s                   testvariable
varB            str8    %9s                   testvariable
varC            str8    %9s                   testvariable
------------------------------------------------------------------------------------------------
Sorted by:  
     Note:  dataset has changed since last saved

.