Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Searching for a string variable among numeric variables and making changes to the same.

    I have several variables - some numeric and some string - in my dataset. The string variables are so because the missing values are denoted as "NA" among non missing numeric observations. I want to replace the "NA" by "." and then destring the variables. One way to destring the data would be to use the following:

    foreach v of varlist v1 v5 v7 v9 .... {
    replace `v' = subinstr(`v', "NA", ".", .)
    }
    destring, replace

    But since there are too many variables its cumbersome to type all of them using foreach. Is there an efficient way to do this?

  • #2
    Often asked here. See e.g. http://www.statalist.org/forums/foru...s-in-a-dataset

    But you can solve the problem more directly.

    Code:
     
    destring *, replace ignore("NA")
    will ignore numeric variables and map "NA" to missing directly and so cut out the unnecessary conversion to "."

    Demonstration:

    Code:
    clear 
    set obs 42 
    gen nOK = 1 
    gen sOK = "frog" 
    gen sBad = cond(_n < 42, string(_n), "NA") 
    destring *, ignore("NA") replace

    Comment


    • #3
      Thanks a lot. That was really useful.

      Comment

      Working...
      X