Announcement

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

  • Replacing variables and type mismatch

    Hi,

    I am facing problems with replacing variables.

    I have three variables.

    Code:
    . des dist d1 d2
    
    Variable      Storage   Display    Value
        name         type    format    label      Variable label
    --------------------------------------------------------------------------------------------------
    dist            byte    %10.0g                dist
    d1              str9    %9s                   d1
    d2              str16   %16s                  d2

    Since "dist" is a byte variable, I converted it to a string variable:

    Code:
    . tostring dist, replace
    dist was byte now str1

    Now,

    Code:
    . des dist d1 d2
    
    Variable      Storage   Display    Value
        name         type    format    label      Variable label
    --------------------------------------------------------------------------------------------------
    dist            str1    %9s                   dist
    d1              str9    %9s                   d1
    d2              str16   %16s                  d2

    I want to replace the values in "dist" with those from "d1" and "d2":

    Code:
     replace dist = d1 if dist == .
    


    However, the type mismatch problem still exists:

    Code:
    . replace dist = d3 if dist == .
    type mismatch
    r(109);

    I’ve provided a small dataset for your reference.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str1 dist str9 d1 str16 d2
    "." "" ""                
    "." "" ""                
    "." "" ""                
    "." "" ""                
    "." "" ""                
    "." "" ""                
    "." "" ""                
    "." "" ""                
    "." "" ""                
    "." "" ""                
    "." "" ""                
    "." "" ""                
    "." "" ""                
    "." "" ""                
    "." "" "Lahaul and Spiti"
    "." "" ""                
    "." "" "Lahaul and Spiti"
    "." "" "Hamirpur"        
    "." "" ""                
    "." "" ""                
    end



    Appreciate your help.

  • #2
    Code:
    replace dist = d1 if dist == "."
    

    Comment


    • #3
      Thank you, Clyde Schechter for your reply.

      However, it does not solve the problem.

      When I imported the data, The variable type is as follows:

      Code:
       * Example generated by -dataex-. For more info, type help dataex
      clear
      input byte dist
      .
      .
      .
      .
      .
      .
      .
      .
      .
      .
      end
      


      And the variable type is the following:

      Code:
      . des dist
      
      Variable      Storage   Display    Value
          name         type    format    label      Variable label
      --------------------------------------------------------------------------------------------------
      dist            byte    %10.0g                dist
      I then converted it to a string:

      Now, when I tried with that replace command, it is able to only replace values in the "d1" variable.

      Code:
      . tostring dist, replace
      dist was byte now str1
      
      . replace dist = d1 if dist == "."
      variable dist was str1 now str9
      (131 real changes made) // Actually 5 changes take place
      
      . replace dist = d2 if dist == "."
      (0 real changes made)
      
      . replace dist = d3 if dist == "."
      (0 real changes made)
      However, d2 and d2 have observations, but that command does not replace the values in d2 and d3 variables.

      Here are the frequency distributions for d2 and d3:

      Code:
       . tab d2
      
                    d2 |      Freq.     Percent        Cum.
      -----------------+-----------------------------------
              Hamirpur |          5       45.45       45.45
      Lahaul and Spiti |          6       54.55      100.00
      -----------------+-----------------------------------
                 Total |         11      100.00
      
      . tab d3
      
                  d3 |      Freq.     Percent        Cum.
      ---------------+-----------------------------------
      Ambedkar Nagar |          2       50.00       50.00
           Chandauli |          2       50.00      100.00
      ---------------+-----------------------------------
               Total |          4      100.00
      


      I'm not sure why the values in d2 and d3 are not being replaced. Any insights would be appreciated!

      Comment


      • #4
        So here's what's going on. You need to try to see the data the way Stata does and not think about it in abstract ways like ". means missing." When you -tostring d1-, . is converted to ".". That's because Stata sees . in this context as just the character ., not as numeric missing value. So after that operation you have a data set that looks like this:
        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input str1 dist str9 d1 str16 d2
        "." "" ""                
        "." "" ""                
        "." "" ""                
        "." "" ""                
        "." "" ""                
        "." "" ""                
        "." "" ""                
        "." "" ""                
        "." "" ""                
        "." "" ""                
        "." "" ""                
        "." "" ""                
        "." "" ""                
        "." "" ""                
        "." "" "Lahaul and Spiti"
        "." "" ""                
        "." "" "Lahaul and Spiti"
        "." "" "Hamirpur"        
        "." "" ""                
        "." "" ""                
        end
        Next you run -replace dist = d1 if dist == "."-. Since dist == "." in every observation (at least in this example) dist will be replaced by the value of d1 in every observation. Now, the value of d1 is, in this example, always "". Not ".", "". Your data set now looks like this:

        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input str1 dist str9 d1 str16 d2
        "" "" ""                
        "" "" ""                
        "" "" ""                
        "" "" ""                
        "" "" ""                
        "" "" ""                
        "" "" ""                
        "" "" ""                
        "" "" ""                
        "" "" ""                
        "" "" ""                
        "" "" ""                
        "" "" ""                
        "" "" ""                
        "" "" "Lahaul and Spiti"
        "" "" ""                
        "" "" "Lahaul and Spiti"
        "" "" "Hamirpur"        
        "" "" ""                
        "" "" ""                
        end
        The key thing to notice here is that dist no longer contains "." values. It contains "" values only. So when you now try -replace dist = d2 if dist == "."- nothing is going to happen precisely because dist no longer has any observations containing "." . What you need to do is -replace dist = d2 if dist == ""-. Your example data did not include a d3 variable, but exactly the same reasoning applies there. Once you have done your -replace dist = d1...- command, "." is gone forever, having been overwritten with "".

        Comment


        • #5
          Thank you very much for the detailed explanation—it resolved my issue!
          I had noticed that after the first replacement with 'd1', the 'dist' variable no longer contained ".", but rather "". However, I didn’t realize I needed to adjust the syntax in the next step to account for this change.

          Comment

          Working...
          X