Announcement

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

  • Dividing a variable by another one.

    Hi All,

    I have the following table, which is output from the data that I have, I would like to be able to divide the column with name (1) by the total. I'm not sure if I must create a new data set using this output and then do what I need (which I can do that and have no problem), or can I do it in the same date file?
    agegrp 0 1 Total
    1 144,704 1,266 145,970
    2 62,841 980 63,821
    3 165,049 5,398 170,447
    4 383,228 21,983 405,211
    5 330,988 23,963 354,951
    6 189,314 16,627 205,941

    Regards,

  • #2
    This cannot possibly be a dataset, as the names 0 and 1 are not valid Stata variable names. Where does this output come from, i.e. what did you type (exactly) to get this output?

    Maybe all you need to do is adding the row option to a tabulate call.

    Best
    Daniel

    Comment


    • #3
      Hi Daniel,

      Sorry for that, the 0 and 1 are the obs of the variable called Died (0 died, 1 alive).

      Thanks

      Comment


      • #4
        That does not answer the question of what exactly you typed, and what exactly you want to do here. It is hard to give better advice then.

        If you are only interested in the row percentages and you started from

        Code:
        tabulate agegrp died
        then, as suggested

        Code:
        tabulate agegr died ,row
        will do the trick.

        If you have four variables, agegr, died, alive and total, you might be happy with

        Code:
        generate newvar = alive/total
        If you situation is still different and you need more help, please explain in more detail.

        Best
        Daniel

        Comment


        • #5
          Thank you Daniel. What if I want the percentage to six digit (for example: 0.008673)

          Regards,

          Comment


          • #6
            You can use proportion - but this is a rather clumsy solution:

            Code:
            sysuse auto
            proportion rep78 , over(foreign) cformat(%9.8f)

            Comment


            • #7
              I still need to assume you have two variables and started with a tabulate call, as you still have not verified this is the case.

              There might be implemented ways of doing this easier, but here is one that involves using Mata

              Code:
              sysuse nlsw88 ,clear
              
              tabulate occupation union ,row matcell(X)
              
              m : 
                  X = st_matrix("X")
                  X = X, (X[., 1] :/ rowsum(X))
                  st_matrix("X", X)
              end
              
              matlist X
              Best
              Daniel

              Comment


              • #8
                Hi Daniel, Yes I have two variables (age group and Died).

                Thank you for your help, really appreciate it. Is it possible to divided those who are died (1) by the sum of the row (as I get rate of those who are still alive (0) by the sum of the row).

                Regards,

                Comment


                • #9
                  Thank you Svend for your help. I really appreciate it.

                  Regards,

                  Comment


                  • #10
                    Is it possible to divided those who are died (1) by the sum of the row (as I get rate of those who are still alive (0) by the sum of the row).
                    This is what the code I posted does.

                    Best
                    Daniel

                    Comment


                    • #11
                      Hi Daniel.

                      You code divide those who are alive (0) by the sum of the row..

                      Regards

                      Comment


                      • #12
                        Change

                        Code:
                        X = X, (X[., 1] :/ rowsum(X))
                        to

                        Code:
                        X = X, (X[., 2] :/ rowsum(X))

                        Comment


                        • #13
                          Thank you Daniel,

                          Is it possible to graph the output (the proportion of those died in the y-axis and the age group in the x-axis).

                          Regards,

                          Comment

                          Working...
                          X