Announcement

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

  • Problem trying to convert a variable from string to numeric using destring

    Hi All. I have a dataset containing Brazilian Zip codes (variable cep4, a string variable) and their respective geographical latitudes and longitudes (variables nr_latitude and nr_longitude), both string variables.

    I need to convert the latitudes and the longitudes to numeric because another dataset of mine has addresses with numeric latitudes and longitudes.

    My goal: to use ssc command “geonear” (from Robert Picard) to calculate the distances in km between the addresses in this file with addresses located in another file of mine using the latitudes and longitudes.

    What I did:

    Code:
    destring nr_latitude, gen(nr_latitude1)
    nr_latitude: contains nonnumeric characters; no generate

    Code:
    tab nr_latitude if !regexm(nr_latitude,"[0-9]+")
    no observations

    Can someone help?

    Thank you.


    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str2 uf str8 cep4 str19(nr_latitude nr_longitude)
    "SP" "01524000" "-23.5670659" "-46.6160286"
    "SP" "01524000" "-23.5670659" "-46.6160286"
    "SP" "01524000" "-23.5670659" "-46.6160286"
    "SP" "01524000" "-23.5670659" "-46.6160286"
    "SP" "01551000" "-23.5715848" "-46.6130740"
    "SP" "01552000" "-23.5706894" "-46.6109932"
    "SP" "01552000" "-23.5706894" "-46.6109932"
    "SP" "01552000" "-23.5706894" "-46.6109932"
    "SP" "01552000" "-23.5706894" "-46.6109932"
    "SP" "01552000" "-23.5706894" "-46.6109932"
    "SP" "01552000" "-23.5706894" "-46.6109932"
    "SP" "01552001" "-23.5706894" "-46.6109932"
    "SP" "02012021" "-23.5156456" "-46.6441301"
    "SP" "02012021" "-23.5156456" "-46.6441301"
    "SP" "02012021" "-23.5156456" "-46.6441301"
    "SP" "02012021" "-23.5156456" "-46.6441301"
    "SP" "02012021" "-23.5156456" "-46.6441301"
    "SP" "02017011" "-23.5001828" "-46.6314917"
    "SP" "02017011" "-23.5001828" "-46.6314917"
    "SP" "02022021" "-23.5036864" "-46.6358349"
    "SP" "02022021" "-23.5036864" "-46.6358349"
    "SP" "02022901" "-23.5041962" "-46.6282736"
    "SP" "04001004" "-23.5763219" "-46.6559680"
    "SP" "04001084" "-23.5729385" "-46.6526272"
    "SP" "04001084" "-23.5729385" "-46.6526272"
    "SP" "04005903" "-23.5789612" "-46.6572739"
    "SP" "04005903" "-23.5789612" "-46.6572739"
    "SP" "04005903" "-23.5789612" "-46.6572739"
    "SP" "04022002" "-23.5942722" "-46.6459906"
    "SP" "04357080" "-23.6363054" "-46.6536028"
    "SP" "04626910" "-23.6244082" "-46.6613678"
    "SP" "05092040" "-23.5073122" "-46.7117229"
    "SP" "05092040" "-23.5073122" "-46.7117229"
    "SP" "05508000" "-23.5677154" "-46.7294510"
    "SP" "05508000" "-23.5677154" "-46.7294510"
    "SP" "05508000" "-23.5677154" "-46.7294510"
    "SP" "05508000" "-23.5677154" "-46.7294510"
    "SP" "05508000" "-23.5677154" "-46.7294510"
    "SP" "05508030" "-23.5564321" "-46.7216280"
    "SP" "05581001" "-23.5742611" "-46.7270934"
    "SP" "06029900" "-23.5474158" "-46.7664053"
    "SP" "06194050" "-23.5233016" "-46.8087289"
    "SP" "06194060" "-23.5260936" "-46.7888272"
    "SP" "06194140" "-23.5223700" "-46.8082135"
    "SP" "06236100" "-23.5084949" "-46.7910646"
    "SP" "06442000" "-23.5185917" "-46.8622552"
    "SP" "06442030" "-23.5150000" "-46.8747793"
    "SP" "06442130" "-23.5113691" "-46.8729420"
    "SP" "06442130" "-23.5113691" "-46.8729420"
    "SP" "07034041" "-23.4768708" "-46.5259523"
    "DF" "07063000" "-23.4533938" "-46.5527050"
    "SP" "07180000" "-23.4580932" "-46.4938106"
    "SP" "07180000" "-23.4580932" "-46.4938106"
    "SP" "07184000" "-23.4388947" "-46.4761250"
    "SP" "08650010" "-23.5692309" "-46.3397248"
    "SP" "08773380" "-23.5174712" "-46.1914745"
    "SP" "09071100" "-23.6516263" "-46.5461862"
    "SP" "09540230" "-23.6312417" "-46.5726764"
    "SP" "09791260" "-23.7394041" "-46.5315032"
    "SP" "09890900" "-23.6975132" "-46.5632972"
    end

  • #2

    Code:
    tab nr_latitude if missing(real(nr_latitude))
    to see what kind of values are problematic.


    See e.g. https://journals.sagepub.com/doi/abs...867X1801800413 Section 2.7 and more generally

    Note: Your regular expression didn't allow minus signs or decimal points.
    Last edited by Nick Cox; 14 Feb 2024, 14:41.

    Comment


    • #3
      Thanks Nick. After running your tab command line I see a possible problem: there are 8 latitudes and 8 longitudes with commas. I will fix that and see if destring will work.

      Comment


      • #4
        Now destring worked. Thanks.

        Comment

        Working...
        X