Announcement

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

  • having problem in recoding

    Dear Statalist,

    I am facing problem in decoding string variable. For example, the following command has been called to recode.

    recode Var=1 if var1== 1-2.

    because my variable is in string format which is attached here. When I tried using the above said command i have got a "type mismatch" error. So kindly can anyone help me to solve the problem.

    Click image for larger version

Name:	Capture.PNG
Views:	1
Size:	11.5 KB
ID:	1599058

  • #2
    Karthich:

    Code:
    . replace Var="1" if Var=="1-2"
    (1 real change made)
    
    . list
    
         +-----+
         | Var |
         |-----|
      1. |   1 |
         +-----+
    
    .
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      If var1 is a string variable, then you need to compare it to a string constant.
      Code:
      ... if var1=="1-2"
      With that said, your recode command is incorrect. Perhaps what you want is
      Code:
      generate int Var = .
      replace Var = 1 if var1=="1-2"
      replace Var = 2 if ...
      ...
      Alternatively
      Code:
      encode var1, generate(Var)
      label list Var
      will create a numeric variable with values 1, 2, ... and with value labels assigned so they are displayed as the strings were displayed. But the assignment of values to strings will perhaps not be what you have in mind.

      Code:
      label define Var 1 "1-2" 2 "1-3" ...
      encode var1, generate(Var) label(Var)
      label list Var
      will allow you to specify the value you want associated with each string.

      Comment


      • #4
        Dear Carlo and William,

        Thank you so much for your timely help. Your suggestions worked very well.

        Thanks and regards

        Karthick.V

        Comment

        Working...
        X