Advertisement
scottmeup

Stata month comparison

Apr 27th, 2018
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. drop *
  2. input str9 period_start_string str9 period_end_string
  3. "01Nov2000" "01Jan2000"
  4. "01Dec2000" "01Feb2000"
  5. "01Jan2000" "01Mar2000"
  6. "01Feb2000" "01Apr2000"
  7. end
  8.  
  9. *Select only the month from the input
  10. gen psd_month_only_str = substr(period_start_string, 3, 3)
  11. gen ped_month_only_str = substr(period_end_string, 3, 3)
  12.  
  13. *Create date format variable from month only variable above
  14. gen psd_month_only_date = date(psd_month_only_str, "M")
  15. gen ped_month_only_date = date(ped_month_only_str, "M")
  16.  
  17. *Create month format variable from above date variable taken from month only input
  18. gen psd_month_only_month = month(psd_month_only_date)
  19. gen ped_month_only_month = month(ped_month_only_date)
  20.  
  21. *check if month only data start > end in MONTH format
  22. if psd_month_only_month > ped_month_only_month{
  23. gen smonth_gt_e_date = 1
  24. }
  25. else{
  26. gen smonth_gt_e_date = 0
  27. }
  28.  
  29.  
  30. *begin working with whole date
  31. *take string variable for whole date, create date format variable
  32. gen psd_date = date(period_start_string, "DMY")
  33. gen ped_date = date(period_end_string, "DMY")
  34. format psd_date %d
  35. format ped_date %d
  36.  
  37. *take date format variable for whole date, create month format variable
  38. gen psd_month = month(psd_date)
  39. gen ped_month = month(ped_date)
  40. *format psd_month %d
  41. *format ped_month %d
  42.  
  43.  
  44. gen psd_mofd = mofd(psd_date)
  45. gen ped_mofd = mofd(ped_date)
  46. *format psd_mofd %d
  47. *format ped_mofd %d
  48.  
  49.  
  50. *check if start > end in STRING format
  51. if period_start_string > period_end_string{
  52. gen s_gt_e_string = 1
  53. }
  54. else{
  55. gen s_gt_e_string = 0
  56. }
  57.  
  58. *check if start > end in DATE format
  59. if psd_date > ped_date{
  60. gen s_gt_e_date = 1
  61. }
  62. else{
  63. gen s_gt_e_date = 0
  64. }
  65.  
  66. *check if start > end in MONTH format
  67. if psd_month > ped_month{
  68. gen s_gt_e_month = 1
  69. }
  70. else{
  71. gen s_gt_e_month = 0
  72. }
  73.  
  74. *check if start > end in MOFD format
  75. if psd_mofd > ped_mofd{
  76. gen s_gt_e_mofd = 1
  77. }
  78. else{
  79. gen s_gt_e_mofd = 0
  80. }
  81.  
  82.  
  83. describe
  84. list
  85.  
  86. *test one value at a time only
  87. if "01nov1960" > "01jan1960"{
  88. display "True"
  89. }
  90. else{
  91. display "False"
  92. }
  93.  
  94.  
  95. if "01jan1960" > "01nov1960"{
  96. display "True"
  97. }
  98. else{
  99. display "False"
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement