Announcement

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

  • Split numeric variable based on a decimal

    I have a variable named "ga," which means gestational age. The data is represented in weeks and days. For example, 31.6 means 31 weeks and 6 days. I want to convert the variable into weeks. I am struggling with the appropriate code. Split didn't work since the data is numeric.

    Here is a sample of my data:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input double ga
    31.6
    31.6
    31.5
    31.5
    30.6
    31.4
    27.1
    27.4
    26.3
    27.1
    26.3
    26.2
    29.4
    29.3
    30.4
    end
    ------------------ copy up to and including the previous line ------------------


  • #2
    You can use int() and mod() to get the dividend and remainder. These are numerical operation so there's no need to work with strings.

    Code:
    gen ga_weeks = int(ga)
    gen ga_days = mod(ga, 1) * 10
    
    gen int total_days = ga_weeks * 7 + ga_days
    
    list, abbrev(20)
    Result

    Code:
         +----------------------------------------+
         |   ga   ga_weeks   ga_days   total_days |
         |----------------------------------------|
      1. | 31.6         31         6          223 |
      2. | 31.6         31         6          223 |
      3. | 31.5         31         5          222 |
      4. | 31.5         31         5          222 |
      5. | 30.6         30         6          216 |
         |----------------------------------------|
      6. | 31.4         31         4          221 |
      7. | 27.1         27         1          190 |
      8. | 27.4         27         4          193 |
      9. | 26.3         26         3          185 |
     10. | 27.1         27         1          190 |
         |----------------------------------------|
     11. | 26.3         26         3          185 |
     12. | 26.2         26         2          184 |
     13. | 29.4         29         4          207 |
     14. | 29.3         29         3          206 |
     15. | 30.4         30         4          214 |
         +----------------------------------------+

    Comment


    • #3
      I understood the question a bit differently. I.e., I think Al wants the result in (possibly) fractional weeks, like this:

      Code:
      * Number before decimal = weeks.
      * Number after decimal = days.
      * Wanted:  gestational age in weeks.
      generate ga_weeks = int(ga) + (mod(ga, 1) * 10)/7
      list, clean noobs
      Result:
      Code:
      . list, clean noobs
      
            ga   ga_weeks  
          31.6   31.85714  
          31.6   31.85714  
          31.5   31.71428  
          31.5   31.71428  
          30.6   30.85714  
          31.4   31.57143  
          27.1   27.14286  
          27.4   27.57143  
          26.3   26.42857  
          27.1   27.14286  
          26.3   26.42857  
          26.2   26.28572  
          29.4   29.57143  
          29.3   29.42857  
          30.4   30.57143
      --
      Bruce Weaver
      Email: [email protected]
      Version: Stata/MP 18.5 (Windows)

      Comment


      • #4
        Leonardo's solution gave me days which I converted into fractional weeks. My intention was to get fractional weeks as Bruce explained, so post #3 was a direct approach. Thank you, both!

        Comment

        Working...
        X