Announcement

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

  • Generating change in levels as well as percentages from stata

    Hi All,

    I have a dataset which resembles the following:

    x y
    1 2
    2 4
    3 6


    I have two variables here, x and y. I wish to calculate first the percentage change from x to y. Let us store this in another variable, z.

    Code:
    g z= ((y-x)/x)*100
    I then calculate the change in levels, and store it in z1.

    Code:
    g z1= (y-x)
    I now wish to export x, z and z1 to excel. I do this by:

    export excel using "Example.xlsx", replace firstrow(variables)


    What I wish to do is to not have two variables z and z1, but a single variable, say z2, which contains information on both levels and percentanges (say levels as they are, and percentages in brackets under the values in levels). Of course, this can be done on excel directly, but is there a way of doing this in Stata?

    Many thanks,
    CS


  • #2
    Code:
    clear
    input x y
    1 2
    2 4
    3 6
    end
    g z=strofreal((y-x)) +" ("+strofreal(((y-x)/x)*100)+")"
    
    . list,noobs div
    
      +-----------------+
      | x | y |       z |
      |---+---+---------|
      | 1 | 2 | 1 (100) |
      | 2 | 4 | 2 (100) |
      | 3 | 6 | 3 (100) |
      +-----------------+

    Comment


    • #3
      Ali Atia Thanks a lot!!

      Comment


      • #4
        Originally posted by Ali Atia View Post
        Code:
        clear
        input x y
        1 2
        2 4
        3 6
        end
        g z=strofreal((y-x)) +" ("+strofreal(((y-x)/x)*100)+")"
        
        . list,noobs div
        
        +-----------------+
        | x | y | z |
        |---+---+---------|
        | 1 | 2 | 1 (100) |
        | 2 | 4 | 2 (100) |
        | 3 | 6 | 3 (100) |
        +-----------------+
        Interesting command, I had not used it before. What is the function of the + symbol here? I couldn't find it in the help? Many thanks.

        Comment


        • #5
          + concatenates the strings on either side of it, which allows you to combine the output of functions like strofreal with plain text like " (" and ")". See the helpfile for strcat():

          Code:
          Function
          
              strcat(s1,s2)
                 Description:  there is no strcat() function; instead the addition operator is used to concatenate strings
          
                               "hello " + "world" = "hello world"
                               "a" + "b" = "ab"
                               "Café " + "de Flore" = "Café de Flore"
                 Domain s1:    strings
                 Domain s2:    strings
                 Range:        strings

          Comment


          • #6
            Thanks a lot. Very well explained.

            Comment

            Working...
            X