Announcement

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

  • Problems with changing variable that contains a decimal separator from string into numeric

    Hi all,

    I'm trying to destring a string variable (variable v) with the following code:

    command destring, replace
    destring v, generate (ESG_combined_score) force

    However, State reports: v: contains nonnumeric characters; ESG_combined_score generated as byte, as a result almost all observations from variable v are changed into missing values.
    I think the problem is that variable v contains many observations with a decimal separator. I tried to fix the problem with the following code:

    destring v, replace force ignore(,)

    but this only removes the decimal and changes an observation from 57,89 into 5789.

    Below is a sample of my dataset:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str21 isindatatype double year str45 name str5 v str11 cusip
    "US00081T1088(TRESGCS)" 2016 "ACCO BRANDS - ESG Combined Score"            "57,89" "00081T108"
    "US00081T1088(TRESGCS)" 2017 "ACCO BRANDS - ESG Combined Score"            "60,04" "00081T108"
    "US00081T1088(TRESGCS)" 2018 "ACCO BRANDS - ESG Combined Score"            "70,07" "00081T108"
    "US00081T1088(TRESGCS)" 2019 "ACCO BRANDS - ESG Combined Score"            "74,89" "00081T108"
    "US00081T1088(TRESGCS)" 2020 "ACCO BRANDS - ESG Combined Score"            "74,04" "00081T108"
    "US00182C1036(TRESGCS)" 2016 "ANI PHARMACEUTICALS - ESG Combined Score"    "19,25" "00182C103"
    "US00182C1036(TRESGCS)" 2017 "ANI PHARMACEUTICALS - ESG Combined Score"    "21,63" "00182C103"
    "US00182C1036(TRESGCS)" 2018 "ANI PHARMACEUTICALS - ESG Combined Score"    "18,12" "00182C103"
    "US00182C1036(TRESGCS)" 2019 "ANI PHARMACEUTICALS - ESG Combined Score"    "19,99" "00182C103"
    "US00182C1036(TRESGCS)" 2020 "ANI PHARMACEUTICALS - ESG Combined Score"    "21,78" "00182C103"
    "US00183L1026(TRESGCS)" 2016 "ANGI A - ESG Combined Score"                 "28,71" "00183L102"
    "US00183L1026(TRESGCS)" 2017 "ANGI A - ESG Combined Score"                 "23,31" "00183L102"
    "US00183L1026(TRESGCS)" 2018 "ANGI A - ESG Combined Score"                 "19,39" "00183L102"
    "US00183L1026(TRESGCS)" 2019 "ANGI A - ESG Combined Score"                 "18,56" "00183L102"
    "US00183L1026(TRESGCS)" 2020 "ANGI A - ESG Combined Score"                 "20,24" "00183L102"
    "US00287Y1091(TRESGCS)" 2016 "ABBVIE - ESG Combined Score"                 "74,88" "00287Y109"
    "US00287Y1091(TRESGCS)" 2017 "ABBVIE - ESG Combined Score"                 "70,79" "00287Y109"
    "US00287Y1091(TRESGCS)" 2018 "ABBVIE - ESG Combined Score"                 "50,58" "00287Y109"
    "US00287Y1091(TRESGCS)" 2019 "ABBVIE - ESG Combined Score"                 "56,9"  "00287Y109"
    "US00287Y1091(TRESGCS)" 2020 "ABBVIE - ESG Combined Score"                 "48,59" "00287Y109"
    "US00404A1097(TRESGCS)" 2016 "ACADIA HEALTHCARE CO. - ESG Combined Score"  "19,41" "00404A109"
    "US00404A1097(TRESGCS)" 2017 "ACADIA HEALTHCARE CO. - ESG Combined Score"  "29,55" "00404A109"
    "US00404A1097(TRESGCS)" 2018 "ACADIA HEALTHCARE CO. - ESG Combined Score"  "47"    "00404A109"
    "US00404A1097(TRESGCS)" 2019 "ACADIA HEALTHCARE CO. - ESG Combined Score"  "53,83" "00404A109"
    "US00404A1097(TRESGCS)" 2020 "ACADIA HEALTHCARE CO. - ESG Combined Score"  "52,47" "00404A109"
    "US00430H1023(TRESGCS)" 2016 "ACCELERATE DIAGNOSTICS - ESG Combined Score" "30,41" "00430H102"
    "US00430H1023(TRESGCS)" 2017 "ACCELERATE DIAGNOSTICS - ESG Combined Score" "32,17" "00430H102"
    "US00430H1023(TRESGCS)" 2018 "ACCELERATE DIAGNOSTICS - ESG Combined Score" "37,23" "00430H102"
    "US00430H1023(TRESGCS)" 2019 "ACCELERATE DIAGNOSTICS - ESG Combined Score" "32,87" "00430H102"
    "US00430H1023(TRESGCS)" 2020 "ACCELERATE DIAGNOSTICS - ESG Combined Score" "26,84" "00430H102"
    "US00461U1051(TRESGCS)" 2016 "ACLARIS THERAPEUTICS - ESG Combined Score"   "11,09" "00461U105"
    "US00461U1051(TRESGCS)" 2017 "ACLARIS THERAPEUTICS - ESG Combined Score"   "24,6"  "00461U105"
    "US00461U1051(TRESGCS)" 2018 "ACLARIS THERAPEUTICS - ESG Combined Score"   "22,72" "00461U105"
    "US00461U1051(TRESGCS)" 2019 "ACLARIS THERAPEUTICS - ESG Combined Score"   "21,42" "00461U105"
    "US00461U1051(TRESGCS)" 2020 "ACLARIS THERAPEUTICS - ESG Combined Score"   "36,34" "00461U105"
    "US00484M6012(TRESGCS)" 2016 "ACORDA THERAPEUTICS - ESG Combined Score"    "23,68" "00484M601"
    "US00484M6012(TRESGCS)" 2017 "ACORDA THERAPEUTICS - ESG Combined Score"    "24,94" "00484M601"
    "US00484M6012(TRESGCS)" 2018 "ACORDA THERAPEUTICS - ESG Combined Score"    "27,45" "00484M601"
    "US00484M6012(TRESGCS)" 2019 "ACORDA THERAPEUTICS - ESG Combined Score"    "37,7"  "00484M601"
    "US00484M6012(TRESGCS)" 2020 "ACORDA THERAPEUTICS - ESG Combined Score"    "37,3"  "00484M601"
    "US00507V1098(TRESGCS)" 2016 "ACTIVISION BLIZZARD - ESG Combined Score"    "53,16" "00507V109"
    "US00507V1098(TRESGCS)" 2017 "ACTIVISION BLIZZARD - ESG Combined Score"    "43,41" "00507V109"
    "US00507V1098(TRESGCS)" 2018 "ACTIVISION BLIZZARD - ESG Combined Score"    "51,78" "00507V109"
    "US00507V1098(TRESGCS)" 2019 "ACTIVISION BLIZZARD - ESG Combined Score"    "59,86" "00507V109"
    "US00507V1098(TRESGCS)" 2020 "ACTIVISION BLIZZARD - ESG Combined Score"    "35,62" "00507V109"
    "US00508Y1029(TRESGCS)" 2016 "ACUITY BRANDS - ESG Combined Score"          "30,4"  "00508Y102"
    "US00508Y1029(TRESGCS)" 2017 "ACUITY BRANDS - ESG Combined Score"          "41,05" "00508Y102"
    "US00508Y1029(TRESGCS)" 2018 "ACUITY BRANDS - ESG Combined Score"          "41,33" "00508Y102"
    "US00508Y1029(TRESGCS)" 2019 "ACUITY BRANDS - ESG Combined Score"          "62,56" "00508Y102"
    "US00508Y1029(TRESGCS)" 2020 "ACUITY BRANDS - ESG Combined Score"          "55,18" "00508Y102"
    "US00724F1012(TRESGCS)" 2016 "ADOBE (NAS) - ESG Combined Score"            "62,58" "00724F101"
    "US00724F1012(TRESGCS)" 2017 "ADOBE (NAS) - ESG Combined Score"            "75,6"  "00724F101"
    "US00724F1012(TRESGCS)" 2018 "ADOBE (NAS) - ESG Combined Score"            "78,43" "00724F101"
    "US00724F1012(TRESGCS)" 2019 "ADOBE (NAS) - ESG Combined Score"            "67,3"  "00724F101"
    "US00724F1012(TRESGCS)" 2020 "ADOBE (NAS) - ESG Combined Score"            "77,67" "00724F101"
    "US00738A1060(TRESGCS)" 2016 "ADTRAN - ESG Combined Score"                 "52,79" "00738A106"
    "US00738A1060(TRESGCS)" 2017 "ADTRAN - ESG Combined Score"                 "47,36" "00738A106"
    "US00738A1060(TRESGCS)" 2018 "ADTRAN - ESG Combined Score"                 "53,51" "00738A106"
    "US00738A1060(TRESGCS)" 2019 "ADTRAN - ESG Combined Score"                 "35,93" "00738A106"
    "US00738A1060(TRESGCS)" 2020 "ADTRAN - ESG Combined Score"                 "50,83" "00738A106"
    "US00751Y1064(TRESGCS)" 2016 "ADV.AUTO PARTS - ESG Combined Score"         "33,27" "00751Y106"
    "US00751Y1064(TRESGCS)" 2017 "ADV.AUTO PARTS - ESG Combined Score"         "54,78" "00751Y106"
    "US00751Y1064(TRESGCS)" 2018 "ADV.AUTO PARTS - ESG Combined Score"         "62,67" "00751Y106"
    "US00751Y1064(TRESGCS)" 2019 "ADV.AUTO PARTS - ESG Combined Score"         "43,49" "00751Y106"
    "US00751Y1064(TRESGCS)" 2020 "ADV.AUTO PARTS - ESG Combined Score"         "52,08" "00751Y106"
    "US00771V1089(TRESGCS)" 2016 "AERIE PHARMACEUTICALS - ESG Combined Score"  "23,28" "00771V108"
    "US00771V1089(TRESGCS)" 2017 "AERIE PHARMACEUTICALS - ESG Combined Score"  "20,16" "00771V108"
    "US00771V1089(TRESGCS)" 2018 "AERIE PHARMACEUTICALS - ESG Combined Score"  "28"    "00771V108"
    "US00771V1089(TRESGCS)" 2019 "AERIE PHARMACEUTICALS - ESG Combined Score"  "47,22" "00771V108"
    "US00771V1089(TRESGCS)" 2020 "AERIE PHARMACEUTICALS - ESG Combined Score"  "56,57" "00771V108"
    "US00773T1016(TRESGCS)" 2016 "ADVANSIX - ESG Combined Score"               "29,14" "00773T101"
    "US00773T1016(TRESGCS)" 2017 "ADVANSIX - ESG Combined Score"               "47,45" "00773T101"
    "US00773T1016(TRESGCS)" 2018 "ADVANSIX - ESG Combined Score"               "48,11" "00773T101"
    "US00773T1016(TRESGCS)" 2019 "ADVANSIX - ESG Combined Score"               "51,71" "00773T101"
    "US00773T1016(TRESGCS)" 2020 "ADVANSIX - ESG Combined Score"               "57,44" "00773T101"
    "US00846U1016(TRESGCS)" 2016 "AGILENT TECHS. - ESG Combined Score"         "88,42" "00846U101"
    "US00846U1016(TRESGCS)" 2017 "AGILENT TECHS. - ESG Combined Score"         "87,48" "00846U101"
    "US00846U1016(TRESGCS)" 2018 "AGILENT TECHS. - ESG Combined Score"         "89,44" "00846U101"
    "US00846U1016(TRESGCS)" 2019 "AGILENT TECHS. - ESG Combined Score"         "88,66" "00846U101"
    "US00846U1016(TRESGCS)" 2020 "AGILENT TECHS. - ESG Combined Score"         "88,65" "00846U101"
    "US00847X1046(TRESGCS)" 2016 "AGIOS PHARMACEUTICALS - ESG Combined Score"  "20,9"  "00847X104"
    "US00847X1046(TRESGCS)" 2017 "AGIOS PHARMACEUTICALS - ESG Combined Score"  "38,55" "00847X104"
    "US00847X1046(TRESGCS)" 2018 "AGIOS PHARMACEUTICALS - ESG Combined Score"  "39,56" "00847X104"
    "US00847X1046(TRESGCS)" 2019 "AGIOS PHARMACEUTICALS - ESG Combined Score"  "51,73" "00847X104"
    "US00847X1046(TRESGCS)" 2020 "AGIOS PHARMACEUTICALS - ESG Combined Score"  "57,55" "00847X104"
    "US00971T1016(TRESGCS)" 2016 "AKAMAI TECHS. - ESG Combined Score"          "44,73" "00971T101"
    "US00971T1016(TRESGCS)" 2017 "AKAMAI TECHS. - ESG Combined Score"          "60,98" "00971T101"
    "US00971T1016(TRESGCS)" 2018 "AKAMAI TECHS. - ESG Combined Score"          "52,16" "00971T101"
    "US00971T1016(TRESGCS)" 2019 "AKAMAI TECHS. - ESG Combined Score"          "57,48" "00971T101"
    "US00971T1016(TRESGCS)" 2020 "AKAMAI TECHS. - ESG Combined Score"          "58,29" "00971T101"
    "US00972D1054(TRESGCS)" 2016 "AKEBIA THERAPEUTICS - ESG Combined Score"    "17,75" "00972D105"
    "US00972D1054(TRESGCS)" 2017 "AKEBIA THERAPEUTICS - ESG Combined Score"    "20,97" "00972D105"
    "US00972D1054(TRESGCS)" 2018 "AKEBIA THERAPEUTICS - ESG Combined Score"    "23,39" "00972D105"
    "US00972D1054(TRESGCS)" 2019 "AKEBIA THERAPEUTICS - ESG Combined Score"    "28,32" "00972D105"
    "US00972D1054(TRESGCS)" 2020 "AKEBIA THERAPEUTICS - ESG Combined Score"    "31,91" "00972D105"
    "US01741R1023(TRESGCS)" 2016 "ALLEGHENY TECHS. - ESG Combined Score"       "38,06" "01741R102"
    "US01741R1023(TRESGCS)" 2017 "ALLEGHENY TECHS. - ESG Combined Score"       "44,21" "01741R102"
    "US01741R1023(TRESGCS)" 2018 "ALLEGHENY TECHS. - ESG Combined Score"       "52,08" "01741R102"
    "US01741R1023(TRESGCS)" 2019 "ALLEGHENY TECHS. - ESG Combined Score"       "52,77" "01741R102"
    "US01741R1023(TRESGCS)" 2020 "ALLEGHENY TECHS. - ESG Combined Score"       "57,69" "01741R102"
    end
    Can someone please help me which code changes variable v from string into a numeric variable, while keeping the same values for each firm?
    I'm using Stata 17 on a macOs Catalina version 10.15.7.

    Thanks in advance!

  • #2
    If I understand this correctly, you need the dpcomma option of destring.

    Comment


    • #3
      Thanks Nick! I used the following code and it worked:

      destring, dpcomma replace

      Comment

      Working...
      X