Announcement

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

  • Sum up variable from certain observation

    Hi,

    My data is as following:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input double TermSpread float(time Year DTS_arma31_fcst)
    1.13 748 2007            .
     1.1 749 2007            .
    1.15 750 2007            .
    1.12 751 2007            .
    1.18 752 2007  -.001226591
      .9 753 2007  -.001226591
     .98 754 2007  -.006479398
    1.04 755 2007    .02269019
     .93 756 2007  -.008195256
     .68 757 2007  -.006479398
     .65 758 2008   .008105395
     .67 759 2008     .0201164
     .68 760 2008  .0012419632
     .59 761 2008  -.003047682
     .61 762 2008 -.0021897529
      .6 763 2008   .006389538
     .67 764 2008  -.003047682
     .73 765 2008 -.0004738948
     .62 766 2008  -.007337327
     .55 767 2008  -.006479398
      .6 768 2008   .008105395
     .59 769 2008    .00467368
      .8 770 2008  -.005621469
    1.17 771 2008 -.0004738948
     1.3 772 2008   -.01934833
    1.31 773 2008    -.0330752
    1.31 774 2008  -.012484902
    1.27 775 2008 -.0021897529
    1.41 776 2008  -.001331824
    1.57 777 2008  .0020998924
    1.71 778 2008   -.01334283
    end
    I would like Stata to take the last TermSpread with missing values of DTS_arma31_fcst and from there add up DTS_arma31_fcst. So in this case, take the TermSpread of 1.12 and sum up -.001226591, then take the outcome and sum up the next DTS_arma31_fcst, and so on.

    How can I do this?

    Thanks

  • #2
    Dear Juan,

    Thank you for your question. I am, however, still a little unsure what you mean. Do you just want a summation of the DTS_arma31_fcst variable? Do you want a summation of DTS_arma31_fcst with the value of TermSpread? I think some clarification would be helpful to find a solution to your question.

    Best,
    Salvatore

    Comment


    • #3
      Originally posted by Salvatore Viola View Post
      Dear Juan,

      Thank you for your question. I am, however, still a little unsure what you mean. Do you just want a summation of the DTS_arma31_fcst variable? Do you want a summation of DTS_arma31_fcst with the value of TermSpread? I think some clarification would be helpful to find a solution to your question.

      Best,
      Salvatore
      Hi Salvatore,

      I want Stata to take the TermSpread of 1.12 and sum up -.001226591, then take the outcome and sum up the next DTS_arma31_fcst, and so on. So it's basically generate a new variable where the start point is 1.12 and from there calculate the cumulative value of that 1.12 + DTS_arma31_fcst. So it has to ignore the TermSpread after the 1.12 and just take the cumulative value of 1.12 + the next DTS_arma31_fcst.
      Last edited by Juan Gonzalex; 06 Oct 2022, 05:28.

      Comment


      • #4
        Dear Juan,

        There is a pretty simple way to do this. It does require that you manually add 1.12, however, there are other (more complicated) options if you wanted it to do that automatically.

        Code:
        gen sumDTS = sum(DTS_arma31_fcst) + 1.12
        The code creates a new variable which makes a running sum of the DTS variable. I have named it sumDTS.

        I hope this helps to answer your question.

        Best,
        Salvatore

        Comment


        • #5
          Perhaps this?

          Code:
          gen init = TermSpread[_n-1] if missing(DTS_arma31_fcst[_n-1]) & !missing(DTS_arma31_fcst)
          replace init = init[_n-1] if missing(init)
          gen sumDTS = sum(DTS_arma31_fcst) + init
          drop init

          Comment


          • #6
            Originally posted by Hemanshu Kumar View Post
            Perhaps this?

            Code:
            gen init = TermSpread[_n-1] if missing(DTS_arma31_fcst[_n-1]) & !missing(DTS_arma31_fcst)
            replace init = init[_n-1] if missing(init)
            gen sumDTS = sum(DTS_arma31_fcst) + init
            drop init
            Exactly what I wanted, thank you very much

            Comment

            Working...
            X