Announcement

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

  • New variable that sums

    I am currently using a data set with a format similar to the following:
    Party X1 X2 X3 X4 X5 X6 X7 X8 X9
    Labour 1.5 6.2 4.1 9.0 2.2 0.5 0.9 1.1 8.2
    Liberal 3.4 3.9 4.9 1.4 5.2 0.4 9.1 12.2 0.3
    Conservative 3.6 8.1 0.2 3.1 0.4 6.2 4.3 1.8 7.5
    I need to create a new variable for each political party which tells me the number X-values greater than 3. What command or formula would be needed to achieve this?

    I could of course do it by hand, but the size of the dataset means it would take a very long time.

    Any help would be massively appreciated.

    Thank you
    Jonny

  • #2
    Welcome to the Stata Forum/ Statalist,

    please read the FAQ. There you’ll find advice about sharing data/command/output. In short, you are recommended to use code delimiters or - dataex - command. If you version in not the newest, you may - findit dataex - and install it.

    That being said, please check - egen - command.
    Best regards,

    Marcos

    Comment


    • #3
      Please read and act on FAQ Advice #12 to use dataex. It's best not to expect people who answer to work at producing data examples themselves.

      What you ask for requires only a loop. The principles are explained in https://www.stata-journal.com/sjpdf....iclenum=pr0046 If you don't have missing values the code can be simplified.

      Code:
       
      
      clear 
      input str12 Party    X1    X2    X3    X4    X5    X6    X7    X8    X9
      Labour    1.5    6.2    4.1    9.0    2.2    0.5    0.9    1.1    8.2
      Liberal    3.4    3.9    4.9    1.4    5.2    0.4    9.1    12.2    0.3
      Conservative    3.6    8.1    0.2    3.1    0.4    6.2    4.3    1.8    7.5
      end 
      
      gen above3 = X1 > 3 & X1 < . 
      
      quietly forval j = 2/9 { 
         replace above3 = above3 + (X`j' > 3 & X`j' < .) 
      }
      That said, it seems likely that your data layout is ill-advised. For many purposes you need a transposed dataset, such as

      Code:
       
      reshape long X, i(Party) j(ID) 
      reshape wide X, i(ID) j(Party) string 
      rename X*  * 
      
      list 
      
           +----------------------------------+
           | ID   Conser~e   Labour   Liberal |
           |----------------------------------|
        1. |  1        3.6      1.5       3.4 |
        2. |  2        8.1      6.2       3.9 |
        3. |  3         .2      4.1       4.9 |
        4. |  4        3.1        9       1.4 |
        5. |  5         .4      2.2       5.2 |
           |----------------------------------|
        6. |  6        6.2       .5        .4 |
        7. |  7        4.3       .9       9.1 |
        8. |  8        1.8      1.1      12.2 |
        9. |  9        7.5      8.2        .3 |
           +----------------------------------+

      Comment

      Working...
      X