Announcement

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

  • Convert dollar values and percents to numbers

    Hello,

    I have data where some of the values are in dollars or percents and they are string variables in Stata (see price, sold, and ifallsold below). I would like to replace them with numeric variables so I can perform mathematical functions on them. Could someone please let me know the easiest/fastest way to do this?

    Many thanks,
    Alyssa


    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str15 crop str6 price str15 unitsize int(totalbrought totalsold) byte(balance notes) str7 sold float total str8 ifallsold float Month str8 Day
    "AAAAAA (TOTALS)" ""       ""                .  .  . . ""     2309.5 "2,742.50" 9 "Saturday"
    "APPLES"          "$4.00"  "bag"             .  .  0 . "."         0 "$0.00"    9 "Saturday"
    "SPROUTS"         "$5.00"  "bag"             .  .  0 . "."         0 "$0.00"    9 "Saturday"
    "ARUGULA"         "$5.00"  "bag"            54 25 29 . "46%"     125 "$270.00"  9 "Saturday"
    "ASPARAGUS"       "$4.00"  "bu"              .  .  0 . "."         0 "$0.00"    9 "Saturday"
    "BASIL"           "$2.00"  "bu/geniv"        .  .  0 . "."         0 "$0.00"    9 "Saturday"
    "BASIL"           "$2.00"  "bu/thai"         .  .  0 . "."         0 "$0.00"    9 "Saturday"
    "BASIL"           "$2.00"  "bu/lemon"        .  .  0 . "."         0 "$0.00"    9 "Saturday"
    "BASIL"           "$2.00"  "bu/blk opal"     .  .  0 . "."         0 "$0.00"    9 "Saturday"
    "BEANS"           "$3.00"  "qt/pole"         .  .  0 . "."         0 "$0.00"    9 "Saturday"
    "BEANS"           "$3.00"  "qt/string"       .  .  0 . "."         0 "$0.00"    9 "Saturday"
    "BEANS"           "$3.00"  "qt/wax"          .  .  0 . "."         0 "$0.00"    9 "Saturday"
    "BEANS"           "$3.00"  "qt/yellow"       .  .  0 . "."         0 "$0.00"    9 "Saturday"
    "BEANS"           "$3.00"  "qt/purple"       .  .  0 . "."         0 "$0.00"    9 "Saturday"
    "BEANS"           "$3.00"  "qt/mixed"       21 21  0 . "100%"     63 "$63.00"   9 "Saturday"
    "BEANS"           "$3.00"  "qt/pinto"        .  .  0 . "."         0 "$0.00"    9 "Saturday"
    "BEANS"           "$3.00"  "qt/lima"         .  .  0 . "."         0 "$0.00"    9 "Saturday"
    "BEETS"           "$2.00"  "bunch"           8  8  0 . "100%"     16 "$16.00"   9 "Saturday"
    "BEETS W/GRN"     "$3.00"  "bunch"          14 14  0 . "100%"     42 "$42.00"   9 "Saturday"
    "BEET GREENS"     "$2.00"  "bunch"           .  .  0 . "."         0 "$0.00"    9 "Saturday"
    "BOK CHOI"        "$2.00"  "head"            .  .  0 . "."         0 "$0.00"    9 "Saturday"
    "BRAISING MIX"    "$3.00"  "bunch"           .  .  0 . "."         0 "$0.00"    9 "Saturday"
    "BROCCOLI"        "$1.00"  "head (small)"    .  .  0 . "."         0 "$0.00"    9 "Saturday"
    "BROCCOLI"        "$2.00"  "large"           .  .  0 . "."         0 "$0.00"    9 "Saturday"
    "BROC GREENS"     "$3.00"  "bunch"           1  1  0 . "100%"      3 "$3.00"    9 "Saturday"

  • #2
    There are many options, but you may wish to try this:

    Code:
    split price, parse($) gen(myvar)
    destring myvar2, replace
     
    split sold, parse(%) gen(mysold)
    destring mysold1, replace
    Hopefully that helps.
    Best regards,

    Marcos

    Comment


    • #3
      Also, this alternative:

      Code:
      destring price, gen(price2) ignore("$")
      destring sold, gen(sold2) ignore("%")
      list price* sold*
       
           +-------------------------------+
           | price   price2   sold   sold2 |
           |-------------------------------|
        1. |              .              . |
        2. | $4.00        4      .       . |
        3. | $5.00        5      .       . |
        4. | $5.00        5    46%      46 |
        5. | $4.00        4      .       . |
           |-------------------------------|
        6. | $2.00        2      .       . |
        7. | $2.00        2      .       . |
        8. | $2.00        2      .       . |
        9. | $2.00        2      .       . |
       10. | $3.00        3      .       . |
           |-------------------------------|
       11. | $3.00        3      .       . |
       12. | $3.00        3      .       . |
       13. | $3.00        3      .       . |
       14. | $3.00        3      .       . |
       15. | $3.00        3   100%     100 |
           |-------------------------------|
       16. | $3.00        3      .       . |
       17. | $3.00        3      .       . |
       18. | $2.00        2   100%     100 |
       19. | $3.00        3   100%     100 |
       20. | $2.00        2      .       . |
           |-------------------------------|
       21. | $2.00        2      .       . |
       22. | $3.00        3      .       . |
       23. | $1.00        1      .       . |
       24. | $2.00        2      .       . |
       25. | $3.00        3   100%     100 |
           +-------------------------------+
      Best regards,

      Marcos

      Comment


      • #4
        Hi Marcos,

        Thank you for your answers. I decided to go with your second option as it seemed more straightforward to me.

        Thanks again,
        alyssa

        Comment


        • #5
          Alyssa Beavers Thank you for the feed back.
          Best regards,

          Marcos

          Comment

          Working...
          X