Announcement

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

  • Code Help

    Dear All, I need your help to write a code in Stata. It looks like a simple code, but somehow I am unable to write it. Please see the example in excel Code Help.xlsx . The outcome is var1 + var2. However, the output becomes the next observation of var1. I would appreciate your help.

    Looking forward to your response.

    Moeen

  • #2
    Moeen:
    spreadsheets are deprecated on this forum due to the risk of dowloading active contents.
    Please consider sharing an excerpt/example of your dataset via -dataex-. Thanks.
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      Adding to #2, please ensure you give an extract of your actual data. The spreadsheet you give has "A", "B", "C", "D", etc, and yet you talk of arithmetic operations. This is not helpful.

      Also, please show the code you are currently using, so others can replicate your problem and suggest alternatives.

      Comment


      • #4
        Please see my data as dataex. The outcome is var1 + var2. However, the output becomes the next observation of var1. I would appreciate your help in writing the code for the output variable. Thanks so much.

        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input int(var1 var2 output)
          0 180 180
        180 180 360
        360 195 555
        555 195 750
        750 195 945
        end

        Comment


        • #5
          Moeen:
          the issue seems to rest on the way -var1- and -var2- were created.
          I get your very same results;
          Code:
          . egen output2=rowtotal(var*)
          
          . list
          
               +--------------------------------+
               | var1   var2   output   output2 |
               |--------------------------------|
            1. |    0    180      180       180 |
            2. |  180    180      360       360 |
            3. |  360    195      555       555 |
            4. |  555    195      750       750 |
            5. |  750    195      945       945 |
               +--------------------------------+
          
          .
          Kind regards,
          Carlo
          (Stata 19.0)

          Comment


          • #6
            Thanks so much, Carlo. Actually, the problem is "var1". When we total the first row, 0+180 = 180. This total 180 becomes the next observation for var1. It is a cyclical process. I want to know how to code it. I would appreciate your help.

            Comment


            • #7
              Moeen Butt you are actually simply creating a cumulative sum. If all you care about is output, then you don't even need var1, you can simply do the following
              Code:
              gen wanted_output = sum(var2)
              so you get


              Code:
              list , noobs sep(0)
                +---------------------------------+
                | var1   var2   output   wanted~t |
                |---------------------------------|
                |    0    180      180        180 |
                |  180    180      360        360 |
                |  360    195      555        555 |
                |  555    195      750        750 |
                |  750    195      945        945 |
                +---------------------------------+
              p.s. if this does actually solve your problem, this might be my new favourite example of the XY problem.
              Last edited by Hemanshu Kumar; 27 Sep 2022, 02:26.

              Comment


              • #8
                In addition to other helpful comments: "Code help" is a poor title. Some large fraction of posts here ask for help with code! Please use a more specific title for your next question.

                Comment


                • #9
                  Hemanshu Kumar, Thanks so much. It does solve my problem. I really appreciate your help.

                  Moeen

                  Comment


                  • #10
                    Nick Cox, I will be careful about the title next time. Thank you.

                    Comment

                    Working...
                    X