Announcement

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

  • Replace command

    Hello am new to stata, the task that am trying to achieve is to replace a numeric value with a string, but it keeps on saying type mismatch. However the type of the variable is str8.
    I want to replace AB which is 1 with a string value. Am using "replace AB="my string" if "var"="value"

  • #2
    Welcome to Statalist.

    It seems to me what you want is
    Code:
    replace AB = "my string" if XY==1
    making the assumption that XY is a variable containing numeric values that you want to use to determine the replacement value for the string variable AB.

    If that advice doesn't solve your problem, please review the Statalist FAQ linked to from the top of the page, as well as from the Advice on Posting link on the page you used to create your post. Note especially sections 9-12 on how to best pose your question. You have left too much to the imagination of the reader in describing your problem.
    Last edited by William Lisowski; 08 Jan 2019, 10:34.

    Comment


    • #3
      Guessing too, as it is hard to believe that you typed what you said, but possibly

      Code:
       
       replace AB = "my string" if AB == "1"

      Comment


      • #4
        To my knowledge Stata does not allow replacing string variable with numeric values and numeric variable with string values.


        Code:
        . set obs 1
        number of observations (_N) was 0, now 1
        
        . gen str = "String"
        
        . replace str = 1 if str=="String"
        type mismatch
        r(109);
        
        . replace str = 1
        type mismatch
        r(109);
        
        . clear
        
        . set obs 1
        number of observations (_N) was 0, now 1
        
        . gen numb = 1
        
        . replace numb="String" if numb==1
        type mismatch
        r(109);
        
        . replace numb="String"
        type mismatch
        r(109);

        Comment


        • #5
          The "encode/decode" commands should do the trick. Type "help encode" in Stata for documentation and examples.

          Comment

          Working...
          X