Announcement

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

  • Converting data to numeric

    Hi All,

    I have the following dataset:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str3 countrycode str11 ICPprice
    "AFG" ".."         
    "AFR" "39.07982148"
    "ALB" "53.83712687"
    "DZA" "45.23508552"
    "ASM" ".."         
    "AGO" "79.82878433"
    "AIA" "97.41360309"
    "ATG" "72.63896318"
    "ARG" ".."         
    "ARM" "16.70962173"
    "ABW" "89.65779112"
    "ASI" "45.7226766" 
    "AUS" "256.9700959"
    "AUT" "145.4994032"
    "AZE" "15.04243425"
    "BHS" "120.6793739"
    "BHR" "67.57625401"
    "BGD" "29.83023965"
    "BRB" "132.7680104"
    "BLR" "10.59945195"
    "BEL" "174.5160786"
    "BLZ" "37.99164549"
    "BEN" "53.02015854"
    "BMU" "355.4386983"
    "BTN" "39.07347158"
    "BOL" "25.30105996"
    "BON" ".."         
    "BIH" "43.01438147"
    "BWA" "89.23311743"
    "BRA" "117.9836807"
    "BRN" "70.15991845"
    "BGR" "42.90681599"
    "BFA" "43.5967976" 
    "BDI" "26.65219202"
    "KHM" "47.38566384"
    "CMR" "44.08840314"
    "CAN" "168.9584085"
    "CPV" "73.1905077" 
    "CAR" "88.3268803" 
    "CYM" "157.1553039"
    "CAF" "36.61278971"
    "TCD" "59.85437146"
    "CHL" "92.5468105" 
    "CHN" "56.09210426"
    "COL" "47.56931717"
    "CIS" "28.59475849"
    "COM" "57.68924757"
    "COD" "41.17055857"
    "COG" "46.86545695"
    "COK" ".."         
    "CRI" "50.16387827"
    "CIV" "45.50147089"
    "HRV" "74.43327593"
    "CUB" "14.37221151"
    "CUW" "73.52015638"
    "CYP" "106.0648896"
    "CZE" "109.8475817"
    "DNK" "237.5890292"
    "DJI" "58.2275213" 
    "DMA" "70.54655363"
    "DOM" "52.03502938"
    "ECU" "45.25883349"
    "EGY" "16.29471443"
    "EGZ" "16.29471443"
    "SLV" "40.66085571"
    "GNQ" "63.79955057"
    "ERI" ".."         
    "EST" "102.4000865"
    "ETH" "37.05321506"
    "EUO" "139.2542548"
    "FJI" "106.5018517"
    "FIN" "199.2948515"
    "FRA" "180.2187364"
    "PYF" ".."         
    "GAB" "78.95394141"
    "GMB" "36.15728215"
    "GEO" "19.17526845"
    "DEU" "164.9744304"
    "GHA" "41.87313989"
    "GRC" "128.2970257"
    "GRD" "65.12945599"
    "GUM" ".."         
    "GTM" "43.67221252"
    "GIN" "16.17318219"
    "GNB" "50.96175835"
    "GUY" ".."         
    "HTI" "39.51590747"
    "HND" "45.00246283"
    "HKG" "105.7193129"
    "HUN" "70.54577253"
    "ISL" "118.0340736"
    "IND" "29.76020198"
    "IDN" "47.42345554"
    "IRN" "45.40700063"
    "IRQ" "36.43007027"
    "IRL" "177.7538649"
    "ISR" "147.568598" 
    "ITA" "152.2785446"
    "JAM" "62.75631689"
    "JPN" "199.2481682"
    end

    I wish to convert the ICPprice variable to numeric values. Unfortunately, when I use the encode command, the resulting variable does not inherit the numeric values of the variable, but rather, creates new values (presumably by sort order) instead. Any help on this is much appreciated.


    Best,
    CS


  • #2
    Yes, that is exactly what the -encode- command and that is exactly why it should not be used for this purpose.

    To convert string variables that look to human eyes like numbers and get their numeric values, you need to use the -destring- command. -help destring-.

    Comment


    • #3
      Thank you, Clyde Schechter

      Comment

      Working...
      X