Announcement

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

  • The value of a string variable does not seem to be recognized as the good variable when i try to turn it into a numeric variable

    Dear Satalist



    I am using stata 12, abd I am trying to convert the variable V5_MOTIFS that takes the value "Je ne sais pas" , Inexistant" , "A renforcer" , "Trop léger" " Suffisant" to a categorical variable

    More precisely I want this variable to take te value of

    - "0 "when it takes the value of "Je ne sais pas
    - "1" when it takes the value of "Inexistant"
    - "2" when it takes the value of "Trop léger"
    - "3" when it takes the value if "A renforcer"
    - "4" when it takes the value of "Suffisant"

    To do that I proceeded in several steps. First I turned my variable from a string to a numeric variable using the command :
    encode V5_MOTIFS , gen( V5_MOTIFS_ )

    Then I replace the string value by a numeric value using the commands

    replace V5_MOTIFS_=0 if V5_MOTIFS=="Je ne sais pas"
    replace V5_MOTIFS_=1 if V5_MOTIFS=="Inexistant"
    replace V5_MOTIFS_=2 if V5_MOTIFS=="Trop léger"
    replace V5_MOTIFS_=3 if V5_MOTIFS=="A renforcer"
    replace V5_MOTIFS_=4 if V5_MOTIFS=="Suffisant"


    However I does not seem to work and remplace the value by the good value

    For exemple when I use
    replace V5_MOTIFS_=1 if V5_MOTIFS=="Inexistant"
    The varaibles V5_MOTIFS_ get the value "." attributed" instead of "1"

    wHEN i USE
    replace V5_MOTIFS_=2 if V5_MOTIFS=="Trop léger"


    The variable V5_MOTIFS_ gets the value A renforcer instead of 2


    Here an example of my datastets. I only kept one variable V5_MOTIFS but I have the same issue with others

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str14 V5_MOTIFS
    "."             
    "."             
    "."             
    "A renforcer"   
    "."             
    "Suffisant"     
    "."             
    "Trop léger"    
    "Suffisant"     
    "A renforcer"   
    "."             
    "Suffisant"     
    "Trop léger"    
    "Trop léger"    
    "A renforcer"   
    "."             
    "Suffisant"     
    "A renforcer"   
    "."             
    "A renforcer"   
    "."             
    "Suffisant"     
    "A renforcer"   
    "."             
    "A renforcer"   
    "Trop léger"    
    "A renforcer"   
    "Inexistant"    
    "Suffisant"     
    "."             
    "."             
    "A renforcer"   
    "Trop léger"    
    "Trop léger"    
    "A renforcer"   
    "Trop léger"    
    "A renforcer"   
    "."             
    "A renforcer"   
    "A renforcer"   
    "."             
    "A renforcer"   
    "A renforcer"   
    "Trop léger"    
    "."             
    "Inexistant"    
    "."             
    "."             
    "."             
    "."             
    "A renforcer"   
    "Trop léger"    
    "."             
    "Trop léger"    
    "A renforcer"   
    "Trop léger"    
    "Trop léger"    
    "Suffisant"     
    "."             
    "."             
    "Trop léger"    
    "."             
    "Trop léger"    
    "."             
    "."             
    "A renforcer"   
    "Suffisant"     
    "A renforcer"   
    "."             
    "Suffisant"     
    "Suffisant"     
    "Je ne sais pas"
    "Je ne sais pas"
    "."             
    "A renforcer"   
    "A renforcer"   
    "."             
    "Suffisant"     
    "."             
    "Trop léger"    
    "A renforcer"   
    "Trop léger"    
    "A renforcer"   
    "Trop léger"    
    "Suffisant"     
    "Suffisant"     
    "."             
    "."             
    "Suffisant"     
    "Trop léger"    
    "A renforcer"   
    "Trop léger"    
    "Suffisant"     
    "."             
    "."             
    "Trop léger"    
    "Suffisant"     
    "Trop léger"    
    "A renforcer"   
    "Trop léger"    
    end
    Thanks a lot for your time,
    Best regards,
    Najiba







  • #2
    When you encode, the command by default uses the strings that exist and encodes them in alphabetical order.

    That is not at all what you want here, so define the labels in advance. Note that encode adds a label for "."

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str14 V5_MOTIFS
    "."             
    "A renforcer"   
    "Suffisant"     
    "Trop léger"   
    "Inexistant"    
    "Je ne sais pas"
    end
    
    label def motifs 0 "Je ne sais pas" 1 "Inexistant" 2 "Trop léger" 3 "A renforcer" 4 "Suffisant"
    encode V5_MOTIFS, label(motifs) gen(wanted)
    
    list , sep(0)
    
    list, nola sep(0)
    Code:
    . list , sep(0)
    
         +---------------------------------+
         |      V5_MOTIFS           wanted |
         |---------------------------------|
      1. |              .                . |
      2. |    A renforcer      A renforcer |
      3. |      Suffisant        Suffisant |
      4. |     Trop léger       Trop léger |
      5. |     Inexistant       Inexistant |
      6. | Je ne sais pas   Je ne sais pas |
         +---------------------------------+
    
    . 
    . list, nola sep(0)
    
         +-------------------------+
         |      V5_MOTIFS   wanted |
         |-------------------------|
      1. |              .        5 |
      2. |    A renforcer        3 |
      3. |      Suffisant        4 |
      4. |     Trop léger        2 |
      5. |     Inexistant        1 |
      6. | Je ne sais pas        0 |
         +-------------------------+

    Comment


    • #3
      Najiba.
      you'd better creating a numerical variable instead of tweaking the string:
      Code:
      . gen wanted=0 if V5_MOTIFS=="A renforcer"
      When you have -replace- -wanted- with the appropriate codes for each category of the string variable, you can -label- it and, finally, get rid of the string variable.

      PS: crossed in the cyberspace with Nick's helpful reply.
      Kind regards,
      Carlo
      (Stata 19.0)

      Comment


      • #4
        Thak you very much to both of you it worked perfectly.
        Best regards,
        Najiba

        Comment

        Working...
        X