Announcement

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

  • Removing decimal without rounding

    Hi,

    I need to use a year-quarter variable in a regression model, but it won't run due to the decimal place. Is there a way to drop the decimal without rounding the number? The variable is a float saved as 2008.1, 2008.2, 2008.3, 2008.4 etc.

    Code:
    . dataex YEARQ
    
    ----------------------- copy starting from the next line -----------------------
    
    
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float YEARQ
    2019.4
    2010.4
    2015.3
    2008.1
    2016.1
    2008.3
    2012.3
    2012.3
    2008.1
    2012.3
    2019.2
    2008.1
    2010.3
    2015.2
    2015.3
    2015.4
    2018.4
    2010.2
    2009.2
    2008.1
    2008.1
    2018.3
    2008.3
    2019.2
    2010.4
    2012.1
    2019.4
    2011.1
    2008.2
    2017.1
    2012.1
    2008.3
    2018.1
    2015.4
    2014.2
    2012.1
    2019.4
    2008.1
    2008.1
    2018.4
    2016.1
    2009.2
    2017.1
    2017.2
    2016.1
    2017.4
    2013.4
    2018.3
    2015.4
    2008.1
    2019.4
    2008.1
    2009.4
    2017.1
    2009.4
    2010.2
    2010.3
    2015.4
    2012.4
    2019.2
    2012.3
    2013.3
    2014.1
    2016.3
    2015.1
    2008.4
    2016.1
    2016.4
    2008.3
    2014.3
    2011.1
    2009.1
    2014.4
    2018.4
    2008.3
    2012.2
    2011.2
    2019.3
    2018.1
    2017.2
    2014.1
    2015.3
    2019.4
    2012.1
    2018.1
    2015.3
    2018.3
    2008.1
    2019.3
    2018.2
    2015.3
    2013.1
    2018.2
    2018.3
    2019.4
    2019.4
    2019.3
    2013.3
    2018.1
    2018.1
    end

    Thank you!

  • #2
    I have two guesses on what you want here, the year in a variable as an integer and a quarterly date in a variable as an integer.

    The second is trickier as fractions .1 .2 .3 .4 are held as binary approximations thereof and none is exact. But this seems to work:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float YEARQ
    2019.4
    2010.4
    2015.3
    2008.1
    2016.1
    2008.3
    2012.3
    2012.3
    2008.1
    2012.3
    2019.2
    2008.1
    2010.3
    2015.2
    2015.3
    2015.4
    2018.4
    2010.2
    2009.2
    2008.1
    2008.1
    2018.3
    2008.3
    2019.2
    2010.4
    2012.1
    2019.4
    2011.1
    2008.2
    2017.1
    2012.1
    2008.3
    2018.1
    2015.4
    2014.2
    2012.1
    2019.4
    2008.1
    2008.1
    2018.4
    2016.1
    2009.2
    2017.1
    2017.2
    2016.1
    2017.4
    2013.4
    2018.3
    2015.4
    2008.1
    2019.4
    2008.1
    2009.4
    2017.1
    2009.4
    2010.2
    2010.3
    2015.4
    2012.4
    2019.2
    2012.3
    2013.3
    2014.1
    2016.3
    2015.1
    2008.4
    2016.1
    2016.4
    2008.3
    2014.3
    2011.1
    2009.1
    2014.4
    2018.4
    2008.3
    2012.2
    2011.2
    2019.3
    2018.1
    2017.2
    2014.1
    2015.3
    2019.4
    2012.1
    2018.1
    2015.3
    2018.3
    2008.1
    2019.3
    2018.2
    2015.3
    2013.1
    2018.2
    2018.3
    2019.4
    2019.4
    2019.3
    2013.3
    2018.1
    2018.1
    end
    
    duplicates drop
    sort YEARQ
    
    gen year = floor(YEARQ)
    gen qdate = yq(year, round(10 * mod(YEARQ, 1)))
    format qdate %tq
    
    list, sepby(year)
    
         +------------------------+
         |  YEARQ   year    qdate |
         |------------------------|
      1. | 2008.1   2008   2008q1 |
      2. | 2008.2   2008   2008q2 |
      3. | 2008.3   2008   2008q3 |
      4. | 2008.4   2008   2008q4 |
         |------------------------|
      5. | 2009.1   2009   2009q1 |
      6. | 2009.2   2009   2009q2 |
      7. | 2009.4   2009   2009q4 |
         |------------------------|
      8. | 2010.2   2010   2010q2 |
      9. | 2010.3   2010   2010q3 |
     10. | 2010.4   2010   2010q4 |
         |------------------------|
     11. | 2011.1   2011   2011q1 |
     12. | 2011.2   2011   2011q2 |
         |------------------------|
     13. | 2012.1   2012   2012q1 |
     14. | 2012.2   2012   2012q2 |
     15. | 2012.3   2012   2012q3 |
     16. | 2012.4   2012   2012q4 |
         |------------------------|
     17. | 2013.1   2013   2013q1 |
     18. | 2013.3   2013   2013q3 |
     19. | 2013.4   2013   2013q4 |
         |------------------------|
     20. | 2014.1   2014   2014q1 |
     21. | 2014.2   2014   2014q2 |
     22. | 2014.3   2014   2014q3 |
     23. | 2014.4   2014   2014q4 |
         |------------------------|
     24. | 2015.1   2015   2015q1 |
     25. | 2015.2   2015   2015q2 |
     26. | 2015.3   2015   2015q3 |
     27. | 2015.4   2015   2015q4 |
         |------------------------|
     28. | 2016.1   2016   2016q1 |
     29. | 2016.3   2016   2016q3 |
     30. | 2016.4   2016   2016q4 |
         |------------------------|
     31. | 2017.1   2017   2017q1 |
     32. | 2017.2   2017   2017q2 |
     33. | 2017.4   2017   2017q4 |
         |------------------------|
     34. | 2018.1   2018   2018q1 |
     35. | 2018.2   2018   2018q2 |
     36. | 2018.3   2018   2018q3 |
     37. | 2018.4   2018   2018q4 |
         |------------------------|
     38. | 2019.2   2019   2019q2 |
     39. | 2019.3   2019   2019q3 |
     40. | 2019.4   2019   2019q4 |
         +------------------------+
    Last edited by Nick Cox; 29 Jan 2024, 08:38.

    Comment


    • #3
      Thank you so much, Dr. Cox!

      Comment

      Working...
      X