Announcement

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

  • missing value for string type and missing value for numeric type

    Hi
    ,
    I'm now trying to replace the two conditions.
    If the variable is the string, replace var =="
    OR if it is numeric replace
    var=.
    , but I don't know which one is the string or numeric because there is a lot of the variable in the data set.
    Provided an example data set. I also use the following code, but it doesn't work. Please suggest to me.


    foreach v of local
    varlist
    healthfac_yn
    healthfac_num healthfac_age_1 healthfac_impv_type_1 healthfac_type_other_
    1​​​​​​​{
    capture confirm numeric variable `v'
    if
    !_
    rc {
    replace `var'="" if healthfac_yn==0 &
    healthfac_num
    !=.
    }
    else {
    replace `var=. if

    

    healthfac_yn==0 &
    healthfac_num
    !=.
    }
    }

    ----------------------- copy starting from the next line -----------------------
    Code:
    * Example generated by -
    dataex-. To install: 
    ssc install 
    dataex
    
    clear
    input byte
    (
    healthfac_yn 
    healthfac_num healthfac_age_1) str209 healthfac_impv_type_1 str39 healthfac_type_other_1
    1 1 3 ""                                                                                                                                                                                                                  ""
    0
     .
     . ""                                                                                                                                                                                                                  ""
    0 . . ""                                                                                                                                                                                                                  ""
    0 . . ""                                                                                                                                                                                                                  ""
    0 . . ""                                                                                                                                                                                                                  ""
    0 . . ""                                                                                                                                                                                                                  ""
    1 1 3 "အမိုးပြန်လည်ပြုပြင်ခြင်း"                                                                                                                                          ""
    0 . . ""                                                                                                                                                                                                                  ""
    0 . . ""                                                                                                                                                                                                                  ""
    0 . . ""                                                                                                                                                                                                                  ""
    1 1 3 "အခင်း ခင်း၊ မျက်နှာကြက်ပြင်"                                                                                                                                     ""
    0 . . ""                                                                                                                                                                                                                  ""
    0 . . ""                                                                                                                                                                                                                  ""
    0 . . ""                                                                                                                                                                                                                  ""
    0 . . ""                                                                                                                                                                                                                  ""
    0 . . ""                                                                                                                                                                                                                  ""
    0 . . ""                                                                                                                                                                                                                  ""
    0 . . ""                                                                                                                                                                                                                  ""
    1 1 3 ""                                                                                                                                                                                                                  ""
    0 . . ""                                                                                                                                                                                                                  ""
    0 . . ""                                                                                                                                                                                                                  ""
    0 . . ""                                                                                                                                                                                                                  ""
    0 . . ""                                                                                                                                                                                                                  ""
    0 . . ""                                                                                                                                                                                                                  ""
    1 1 3 "ခြံစည်းရိုးခတ်"                                                                                                                                                                        ""
    1 1 3 "အမိုးနဲ့ မီးဖိုခန်း။"                                                                                                                                                        ""
    0 . . ""                                                                                                                                                                                                                  ""
    0 . . ""                                                                                                                                                                                                                  ""
    1 1 3 "အ​ဆောင်သစ်"                                                                                                                                                                                    ""
    1 1 2 ""                                                                                                                                                                                                                  ""
    1 1 2 ""                                                                                                                                                                                                                  ""
    1 1 3 ""                                                                                                                                                                                                                  ""
    0 . . ""                                                                                                                                                                                                                  ""
    0 . . ""                                                                                                                                                                                                                  ""
    1 1 3 "အ​ဆောင်သစ်​ဆောက်တယ်"                                                                                                                                                         ""
    0 . . ""                                                                                                                                                                                                                  ""
    0 . . ""                                                                                                                                                                                                                  ""
    0 . . ""                                                                                                                                                                                                                  ""
    0 . . ""                                                                                                                                                                                                                  ""
    0 . . ""                                                                                                                                                                                                                  ""
    0 . . ""                                                                                                                                                                                                                  ""
    0 . . ""                                                                                                                                                                                                                  ""
    0 . . ""                                                                                                                                                                                                                  ""
    1 1 2 ""                                                                                                                                                                                                                  ""
    1 1 2 ""                                                                                                                                                                                                                  ""
    0 . . ""                                                                                                                                                                                                                  ""
    1 1 3 ""                                                                                                                                                                                                                  ""
    0 . . ""                                                                                                                                                                                                                  ""
    1 1 3 ""                                                                                                                                                                                                                  ""
    0 . . ""                                                                                                                                                                                                                  ""
    0 . . ""                                                                                                                                                                                                                  ""
    0 . . ""                                                                                                                                                                                                                  ""
    1 1 3 ""                                                                                                                                                                                                                  ""
    0 . . ""                                                                                                                                                                                                                  ""
    1 1 3 ""                                                                                                                                                                                                                  ""
    1 1 3 "အ​ဆောက်အဦးတိုးချဲ့,အမိုးပြင်ခြင်း ,ကြမ်းခင်းပြင်ခြင်း ,​ဆေးပြန်သုတ်ခြင်း" ""
    0 . . ""                                                                                                                                                                                                                  ""
    1 1 2 ""                                                                                                                                                                                                                  ""
    0 . . ""                                                                                                                                                                                                                  ""
    0 . . ""                                                                                                                                                                                                                  ""
    0 . . ""                                                                                                                                                                                                                  ""
    0 . . ""                                                                                                                                                                                                                  ""
    0 . . ""                                                                                                                                                                                                                  ""
    1 1 1 ""                                                                                                                                                                                                                  ""
    0 . . ""                                                                                                                                                                                                                  ""
    0 . . ""                                                                                                                                                                                                                  ""
    1 1 2 "အမိုးအကာအခင်းပြန်ပြုပြင်"                                                                                                                                          ""
    0 . . ""                                                                                                                                                                                                                  ""
    1 1 3 "​အောက်ဆင့်လဲလှယ်ခြင်း,အခင်းလဲလှယ်ခြင်း"                                                                                                  ""
    0 . . ""                                                                                                                                                                                                                  ""
    1 1 1 ""                                                                                                                                                                                                                  ""
    0 . . ""                                                                                                                                                                                                                  ""
    0 . . ""                                                                                                                                                                                                                  ""
    0 . . ""                                                                                                                                                                                                                  ""
    0 . . ""                                                                                                                                                                                                                  ""
    1 1 1 ""                                                                                                                                                                                                                  ""
    0 . . ""                                                                                                                                                                                                                  ""
    1 1 2 ""                                                                                                                                                                                                                  ""
    1 1 3 "အသစ်ပြန်​ဆောက်"                                                                                                                                                                        ""
    0 . . ""                                                                                                                                                                                                                  ""
    0 . . ""                                                                                                                                                                                                                  ""
    1 1 2 "တံခါးပြန်ပြင်"                                                                                                                                                                           ""
    0 . . ""                                                                                                                                                                                                                  ""
    1 1 3 "ကြမ်းခင်းပြုပြင်ခြင်း"                                                                                                                                                   ""
    0 . . ""                                                                                                                                                                                                                  ""
    0 . . ""                                                                                                                                                                                                                  ""
    1 1 1 ""                                                                                                                                                                                                                  ""
    0 . . ""                                                                                                                                                                                                                  ""
    0 . . ""                                                                                                                                                                                                                  ""
    0 . . ""                                                                                                                                                                                                                  ""
    0 . . ""                                                                                                                                                                                                                  ""
    0 . . ""                                                                                                                                                                                                                  ""
    0 . . ""                                                                                                                                                                                                                  ""
    0 . . ""                                                                                                                                                                                                                  ""
    0 . . ""                                                                                                                                                                                                                  ""
    0 . . ""                                                                                                                                                                                                                  ""
    0 . . ""                                                                                                                                                                                                                  ""
    0 . . ""                                                                                                                                                                                                                  ""
    0 . . ""                                                                                                                                                                                                                  ""
    1 1 2 ""                                                                                                                                                                                                                  ""
    end
    label values healthfac_yn healthfac_yn
    label def healthfac_yn 0 "No", modify
    label def healthfac_yn 1 "Yes", modify
    label values healthfac_age_1 healthfac_age_1
    label def healthfac_age_1 1 "< 2 years", modify
    label def healthfac_age_1 2 "2 - 5 years", modify
    label def healthfac_age_1 3 ">5 years", modify
    ------------------ copy up to and including the previous line ------------------

  • #2
    There is an error in your dataex example. I am guessing that this is what you want. You just need to identify either numerical variables or string variables, and you can use the command ds for that.

    Code:
    ds, has(type numeric)
    local nums= r(varlist)
    local vars "healthfac_yn healthfac_num healthfac_age_1 healthfac_impv_type_1 healthfac_type_other_1"
    foreach v of local vars{
    if strpos("`nums'", "`v'")>0{
          replace `v'=. if healthfac_yn==0 & healthfac_num!=.
    }
    if strpos("`nums'", "`v'")==0{
          replace `v'="" if healthfac_yn==0 & healthfac_num!=.
    }
    }
    Last edited by Andrew Musau; 27 Nov 2019, 12:11.

    Comment

    Working...
    X