Announcement

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

  • How to remove the less than sign while halving the value

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str5 var1 float var2
    "<0.50" .25
    "<1.10" .55
    "<0.50" .25
    "<1.00"  .5
    "<0.50" .25
    "<1.20"  .6
    "<0.50" .25
    "<1.00"  .5
    "<0.30" .15
    "0.50"   .5
    end

    the var2 is the target var from var1, how to remove the "<" sign and at the same time divide the value after the "<" sign by 2
    i can remove the "<" sign by the command as follows:
    gen var3 = ""
    replace var3 = regexr(var1, "^<", "")

    but how to divide the value by 2 at the same time

    Thank you very much!

  • #2
    Code:
    destring var1, replace
    gen var2 = var / 2

    Comment


    • #3
      Thank you for your reply, but it may not work, because there are many values that do not contain less than signs, and the values that do not contain less than signs do not need to be divided by 2

      Comment


      • #4
        Code:
        . destring var1, generate(var3) ignore("<")
        var1: character < removed; var3 generated as double
        
        . replace var3 = var3/2 if substr(var1,1,1)=="<"
        (9 real changes made)
        
        . list, clean noobs
        
             var1   var3   var2  
            <0.50    .25    .25  
            <1.10    .55    .55  
            <0.50    .25    .25  
            <1.00     .5     .5  
            <0.50    .25    .25  
            <1.20     .6     .6  
            <0.50    .25    .25  
            <1.00     .5     .5  
            <0.30    .15    .15  
             0.50     .5     .5

        Comment


        • #5
          @William Lisowski @Jackson Monroe thank you very much ,
          first remove the "<" sign and then divide by 2, in steps

          Comment

          Working...
          X