Announcement

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

  • Type mismatch even for same type variables?!

    Hi,

    I'm using Stata/SE 12.0 with Windows 10 and I've been blocking for a while on something. The only post I found on the subject was from Paula de Souza but what was suggested there does not seem to work in my case.

    I have merged 2 files and what I want is that when the variable gdp_o has missing values, it would be replaced by GDP values but I keep getting the "type mismatch" error. GDP was a string14, I have done destring but then GDP becomes a "double, %10.0g" type while gdp_o is a "float, %9.0g" type. So when I want to replace, I get:

    Result # of obs.
    -----------------------------------------
    not matched 21,638
    from master 20,750 (_merge==1)
    from using 888 (_merge==2)

    matched 587,218 (_merge==3)
    -----------------------------------------

    . order Countryrep Coderep Codepar year GDP gdp_o gdp_d

    . replace GDP ="." if GDP ==".." | GDP =="_"
    (18449 real changes made)

    . destring GDP, replace ignore(" ")
    GDP has all characters numeric; replaced as double
    (39200 missing values generated)

    .
    . format gdp_o %10.0g

    . format gdp_d %10.0g

    . replace gdp_o =GDP if gdp_o == "."
    type mismatch

    r(109);

    Then I tried
    compress GDP

    compress GDP
    . format gdp_o %10.0g

    . recast float GDP
    GDP: 565006 values would be changed; not changed

    . recast float GDP
    GDP: 565006 values would be changed; not changed

    . recast double gdp_o gdp_d /// to convert gdp_o in double

    . recast double gdp_o gdp_d
    . format gdp_o gdp_d %10.0g

    > format gdp_o gdp_d %10.0g*/
    . recast float GDP, force
    GDP: 564053 values changed
    . replace gdp_o =GDP if gdp_o == "."
    type mismatch





    It would be great if someone could tell me why I keep having this mismatch message, even when I see in the variables manager table that the 2 variables are the same type.
    Thanks a lot!



    Last edited by Amarylis Durand; 18 Apr 2016, 23:32.

  • #2
    The type mismatch is not because var A mismatched with var B, it is because you are using string manipulation on a numeric var or vice versa. See below example

    Code:
    . list
    
         +--------------+
         | var1    var2 |
         |--------------|
      1. |    1     one |
      2. |    .      .  |
      3. |    3   three |
         +----
    
    . gen missvar1 = 1 if var1=="."
    type mismatch
    r(109);
    . gen missvar1 = 1 if var1==.
    (2 missing values generated)
    
    
    . gen missvar2 = 1 if var2==.
    type mismatch
    r(109);
    . gen missvar2 = 1 if var2=="."
    (2 missing values generated)

    Comment


    • #3
      Jorrit pointed out your mistake. Specifically, gdp_o is numeric. So its values can't be strings. Indeed.you yourself gave it a numeric display format and recast it to double. Neither would have been possible if it were a string variable.

      Comment


      • #4
        Hi Jorrit and Nick,

        It works, thank you so much!

        Here it is:

        replace GDP ="." if GDP ==".." | GDP =="_"
        destring GDP, replace ignore( )
        replace gdp_o =GDP if gdp_o == .

        Comment

        Working...
        X