Announcement

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

  • Generate some conditional sum of variables


    Hi all! I am a freshman for the State software and now have a question to ask you.
    I want to calculate the sum of WithdrawJan WithdrawFeb WithdrawMar WithdrawApr WithdrawMay WithdrawJun WithdrawJUL by the year 2005, 2006, 2007 and 2008. I want my final outcome is a single value and in a single column. Such as:
    WUDSpriID eFactspriID Year Month Withdraw
    ID1 ID1 2005 Jan XXX
    ID1 ID1 2005 Feb XXX
    ID1 ID1 2005 Mar XXX
    ID1 ID1 2005 Apr XXX
    ID1 ID1 2005 May XXX
    ID1 ID1 2005 Jun XXX
    ID1 ID1 2005 Jul XXX

    ID1 ID1 2006 Jan XXX
    ID1 ID1 2006 Feb XXX
    ID1 ID1 2006 Mar XXX
    ID1 ID1 2006 Apr XXX
    ID1 ID1 2006 May XXX
    ID1 ID1 2006 Jun XXX
    ID1 ID1 2006 Jul XXX

    ID1 ID1 2007 Jan XXX
    ID1 ID1 2007 Feb XXX
    ID1 ID1 2007 Mar XXX
    ID1 ID1 2007 Apr XXX
    ID1 ID1 2007 May XXX
    ID1 ID1 2007 Jun XXX
    ID1 ID1 2007 Jul XXX

    My first step is to generate Withdraw_Jan = sum(WithdrawJan) if Year = 2005 and apply the command to 2006 2007 2008. But the stata is always shows a invalid syntax error. Could you please help me to fix that error? And if it is allowed, Could you please give me a suggestion how to transform the dataset below to the dataset format I want above?
    Thank you very much!

    Here is my data set:
    Click image for larger version

Name:	2018-07-17 下午9.55.42.png
Views:	2
Size:	488.4 KB
ID:	1453908

  • #2
    I believe the following will do what you want:
    Code:
    collapse (sum) Withdraw*, by(WUDSpriID eFactsprID Year)
    reshape long Withdraw, i(WUDSpriID eFactsprID Year) j(Month) string
    This code is not tested because you did not provide example data in a usable form; so the code may contain errors. Please read the Forum FAQ, with particular attention to #12 for excellent advice about the most helpful ways to show example data (and also code and Stata outputs). In particular, you will learn that screenshots are discouraged. They are often unreadable (though yours is readable on my computer). Even when readable, they do not reveal aspects of the data set that are sometimes crucial to getting the code correct. And, in any case, there is no way to import data into Stata from a screenshot to test out code.

    In the future, when showing data examples, please use the -dataex- command to do so. If you are running version 15.1 or a fully updated version 14.2, it is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    When asking for help with code, always show example data. When showing example data, always use -dataex-.

    Comment


    • #3
      Thank you very much for your help! These two codes actually totally solve my questions about dataset. And I apologize for my fault about the informal problem format of the forum. I will read the instruction you give me and follow them at another question time. Also, I want to ask you a quick question, when I use the "reshape" code, with the "j(month)", Stata automatically make the month alphabetically, like "April first, August next". But I obviously want it "January first, February next". Could you please give me a hint to fix that?

      Comment


      • #4
        Originally posted by Clyde Schechter View Post
        When asking for help with code, always show example data. When showing example data, always use -dataex-.
        Perhaps you could provide example data, as Clyde recommends?

        Comment

        Working...
        X