Announcement

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

  • Filtering String Variable Data to Isolate Whole Numbers Only

    See Example Data Set:

    HTML Code:
    tab damage_repair_costs
    Damage Repair Costs Freq.
    $ 420 800
    $690.00 15
    $500.00 3
    $550 180
    $900 4
    $799.00 72
    40 2
    528.00 1
    655 6
    978.00 7
    Amount Too Small 1
    So I'm trying to create a copy of the variable "Damage Repair Costs" that ignores everything that isn't just a calculable number; this includes the removal of the unnecessary decimals. (Note: there is an intentional space in the first row.) I don't want to change the original data set, just create a copy variable with all of the desired transformations.

    I've tried:

    HTML Code:
    gen damage_repair_costs_transform = damage_repair_costs
    destring damage_repair_costs_transform, gen(damage_costs_final) ignore("$" "." ".00" ",")
    replace damage_costs_final = . if damage_repair_costs == "Amount Too Small"
    But I'm having two issues.

    1. Under the command line,

    HTML Code:
    destring damage_repair_costs_transform, gen(damage_costs_final) ignore("$" "." ".00" ",")
    I get the following :

    HTML Code:
    damage_repair_costs_transform: contains characters not specified in ignore(); no generate
    Additionally, I have an interesting caveat here. I want to be able to ignore spaces (e.g. $ 420), but not treating them exactly the same. Where there's a space in this first data point, I want to replace "Amount Too Small" with a "." -- but both have spaces.

    2. Under the command line,

    HTML Code:
    replace damage_costs_final = . if damage_repair_costs == "Amount Too Small"
    I get the following:

    HTML Code:
    type mismatch
    r(109);
    What am I doing wrong here? How can I get this data to be reduced to just whole numbers?

    Please keep in mind that this question does not reflect exactly what data I am truly working with, but is a fairly accurate recreation of it for what it is that I am trying to learn to do.


  • #2
    Code:
    clonevar wanted = damagerepaircosts
    replace wanted = "." if trim(itrim(wanted)) == "Amount Too Small"
    destring wanted, replace ignore("$" " ")
    replace wanted = int(wanted)

    Comment

    Working...
    X