Announcement

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

  • If command not working

    Hi all
    This is my first attempt at using dataex, hope it works.

    I am having a problem with a simple if command, it doesn't seem to identify the observations that fit the condition.

    Check this dataset sample:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str45 vasodilatad
    ""                              
    "0"                             
    "0"                             
    "0"                             
    "0"                             
    "aas, amlodipina, pentoxifilina"
    "0"                             
    "0"                             
    "0"                             
    "amlodipina"                    
    "aas,pentoxifilina"             
    "0"                             
    "0"                             
    "0"                             
    end

    The variable vasodilat is the original one I am trying to clean. It is a string variable containing several drugs being taken by each patient, separated by commas. I am trying to create 1/0 variables for each drug listed there.
    I started by separating each individual drug in separate variables using the split command.
    Then, starting with the case of the drug "aas", it should follow:
    - If vasodilatad is missing, the future variable "aas" should be =.
    - If vasodilatad has no drug listed which is =="aas", the future variable "aas" should be coded as 0.
    - if vasodilatad has "aas" listed, the future variable "aas" should be =1.

    I tried approaching it like this:

    Code:
    split vasodilatad, parse(,) gen(vasodil)
    gen aas=.
    if vasodil1=="aas" | vasodil2=="aas" | vasodil3=="aas" | vasodil4=="aas" replace aas=1
    else replace aas=0
    replace aas=. if vasodilatad==""
    But as you can see in the following resulting dataset, I can't make it work, as there are observations with "aas" as one of the drugs listed, which was correctly separated in vasodil1/4, but aas is still =0.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str45 vasodilatad str13 vasodil1 str14(vasodil2 vasodil3) str8 vasodil4 float aas
    ""                               ""           ""              ""               "" .
    "0"                              "0"          ""              ""               "" 0
    "0"                              "0"          ""              ""               "" 0
    "0"                              "0"          ""              ""               "" 0
    "0"                              "0"          ""              ""               "" 0
    "aas, amlodipina, pentoxifilina" "aas"        " amlodipina"   " pentoxifilina" "" 0
    "0"                              "0"          ""              ""               "" 0
    "0"                              "0"          ""              ""               "" 0
    "0"                              "0"          ""              ""               "" 0
    "amlodipina"                     "amlodipina" ""              ""               "" 0
    "aas,pentoxifilina"              "aas"        "pentoxifilina" ""               "" 0
    "0"                              "0"          ""              ""               "" 0
    "0"                              "0"          ""              ""               "" 0
    "0"                              "0"          ""              ""               "" 0
    end


    I have reviewed the code over and over and can't find the issue, I am sure it must be pretty simple but I just can't see it.

    Thank you...!

  • #2
    Filipepaula:
    what if you type:
    Code:
    . replace aas=. if vasodilatad=="0"
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      Dear Carlo, thanks for your reply. That wouldn’t be ok because when vasodil==“0” aas should be =0, not missing. Nevertheless, the 0/missing are working, it’s the cases where aas should be ==1 which aren’t correctly classified..

      Comment


      • #4
        your question is not completely clear to me but it seems very unlikely you want the -if- command; see
        Code:
        help ifcmd
        
        and compare that to 
        
        help if

        Comment


        • #5
          This is an FAQ although to my mind the question in the FAQ is really the answer.

          https://www.stata.com/support/faqs/p...-if-qualifier/

          The if command and if qualifier only very rarely do the same thing. The if command has many uses, but given a variable name it looks only in the first observation, which is not at all what you want. That is why it does not (appear to) work, which here means: Your code is legal but doesn't do what you want.

          While we're at it, here is not only what you want but a shorter version too.

          Code:
          replace aas = 1 if vasodil1=="aas" | vasodil2=="aas" | vasodil3=="aas" | vasodil4=="aas"
          
          
          replace aas = 1 if inlist("aas", vasodil1, vasodil2, vasodil3, vasodil4)

          EDIT

          In fact, the 0, 1 indicator variable you want can be got in one line

          Code:
          gen aas = inlist("aas", vasodil1, vasodil2, vasodil3, vasodil4)
          There is no obvious point to initializing it with missing values.
          Last edited by Nick Cox; 09 May 2022, 07:53.

          Comment


          • #6
            Thank you Nick, that’s perfect!

            Comment

            Working...
            X