Announcement

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

  • Issue with replace/subinstr

    Hi All

    I have a dataset having two variables cards_hh and cards_other.. cards_hh is a multiple-choice question and A, B, and C are names of different cards and D is for the other type which needs to be specified in cards_other. I want to replace D in cards_hh if it presents with other cards (A, B, C), or say when cards_other is not blank.

    I have tried


    replace cards_hh=subinstr(cards_hh, "D", "",1) if cards_others!="" $ cards_hh!="D"

    , but It replaces all D from top to i bottom,n cards_hh whereas I want to keep observation in cards_hh where only D is present (15 cases)

    Please help

    I tried another way, but failed.


    if cards_hh!="D" {

    replace cards_hh=subinstr(cards_hh, "D", "",1) if cards_others!=""
    }



    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str4 cards_hh str60 cards_others
    "B" ""
    "A" ""
    "AC" ""
    "C" ""
    "AC" ""
    "AC" ""
    "A" ""
    "A" ""
    "AB" ""
    "AB" ""
    "AC" ""
    "AC" ""
    "AC" ""
    "ABCD" ""
    "ABC" ""
    "AC" ""
    "AC" ""
    "A" ""
    "AC" ""
    "A" ""
    "A" ""
    "A" ""
    "ACD" ""
    "ABC" ""
    "AC" ""
    "ABC" ""
    "D" "inke pass in cards me se koi card nhi bana hus he"
    "ABC" ""
    "AC" ""
    "AC" ""
    "AC" ""
    "AC" ""
    "AC" ""
    "AC" ""
    "ABC" ""
    "AB" ""
    "AC" ""
    "AC" ""
    "AC" ""
    "AC" ""
    "AC" ""
    "AC" ""
    "AC" ""
    "AC" ""
    "AC" ""
    "AC" ""
    "ACD" ""
    "ABD" ""
    "ACD" ""
    "ACD" ""
    "ABCD" ""
    "ABD" ""
    "ABD" ""
    "ACD" ""
    "ACD" ""
    "ACD" ""
    "ACD" ""
    "ACD" ""
    "ACD" ""
    "ACD" ""
    "ACD" ""
    "ACD" ""
    "ACD" ""
    "ABC" ""
    "ACD" ""
    "AD" ""
    "ACD" ""
    "ACD" ""
    "D" "central government health schem"
    "ACD" ""
    "ABC" ""
    "AC" ""
    "AC" ""
    "A" ""
    "AC" ""
    "ABD" ""
    "ACD" ""
    "AB" ""
    "ACD" ""
    "AC" ""
    "AC" ""
    "ABC" ""
    "AC" ""
    "ACD" ""
    "ABC" ""
    "AC" ""
    "C" ""
    "C" ""
    "AC" ""
    "C" ""
    "ABC" ""
    "C" ""
    "AC" ""
    "AC" ""
    "AC" ""
    "AC" ""
    "ABC" ""
    "AC" ""
    "A" ""
    "AC" ""
    end



  • #2
    What do you intend to happen with the qualifier

    Code:
     if cards_others!="" $ cards_hh!="D"
    as the dollar sign $ makes no sense there? I guess you mean & not $ but please confirm.

    Comment


    • #3
      Originally posted by Nick Cox View Post
      What do you intend to happen with the qualifier

      Code:
       if cards_others!="" $ cards_hh!="D"
      as the dollar sign $ makes no sense there? I guess you mean & not $ but please confirm.
      I tried but not getting desired result.

      replace cards_hh=subinstr(cards_hh, "D","",1) if cards_others!="" & cards_hh!="D"
      (0 real changes made)


      before and after running this code result is same. D should disappear after running the code.

      tab cards_hh

      Types of |
      cards the |
      head of the |
      household |
      has | Freq. Percent Cum.
      ------------+-----------------------------------
      A | 94 16.18 16.18
      AB | 19 3.27 19.45
      ABC | 53 9.12 28.57
      ABCD | 8 1.38 29.95
      ABD | 19 3.27 33.22
      AC | 287 49.40 82.62
      ACD | 48 8.26 90.88
      AD | 22 3.79 94.66
      B | 1 0.17 94.84
      C | 15 2.58 97.42
      D | 15 2.58 100.00
      ------------+-----------------------------------
      Total | 581 100.00

      Comment


      • #4
        The problem is with your if condition. The portion cards_others!="" is only true for two observations in your sample data, and in both of those, cards_hh is exactly "D", so the other part of your if condition, cards_hh!="D", fails. So your overall if condition allows no replacement since it is never true. Perhaps you mean OR ("|") instead of AND ("&")?

        Comment


        • #5
          Originally posted by Hemanshu Kumar View Post
          The problem is with your if condition. The portion cards_others!="" is only true for two observations in your sample data, and in both of those, cards_hh is exactly "D", so the other part of your if condition, cards_hh!="D", fails. So your overall if condition allows no replacement since it is never true. Perhaps you mean OR ("|") instead of AND ("&")?
          Thank you for your observation, I found the mistake in the code and corrected and is

          replace cards_hh=subinstr(cards_hh, "D","",1) if cards_others=="" & cards_hh!="D"
          Thanks all

          Comment

          Working...
          X